How to Move Git Branch Pointer to Different Commit

John Wachira Feb 02, 2024
  1. Move Git Branch Pointer to Different Commit While Checked Out in the Destination Branch
  2. Move Git Branch Pointer to Different Commit While Not Checked Out in the Destination Branch
How to Move Git Branch Pointer to Different Commit

This article illustrates how we can move a Git branch pointer to a different commit. We will see how we can move the pointer while checked out and not-checked out on the destination branch.

Let’s start with the simpler one.

Move Git Branch Pointer to Different Commit While Checked Out in the Destination Branch

The example below shows a feature branch in our repository. Here is the commit history.

commit history

We want to move the branch pointer from the 4ee91ac commit to the e65841a commit (i.e., HEAD@ {2}). How do we go about this?

Since we are checked out in the feature branch, our destination, we can run the git reset command, as shown below.

$ git reset --hard e65841a

Output:

HEAD is now at e65841a Update README.md

This will move our ref to the specified commit. Pretty straightforward, right?

Let’s move to the next scenario.

Move Git Branch Pointer to Different Commit While Not Checked Out in the Destination Branch

Let’s switch to the master branch. How can we move the branch pointer for the feature branch while checked out in the master branch?

There are two methods available. We will start with the easiest one.

the git branch Command

Here is the basic syntax for the command for this scenario.

$ git branch -f <branch-name> <commit-hash>

In this scenario, we want to move the pointer to the afcc8bb commit. How do we go about it?

As shown below, we can move the pointer for our feature branch with the git branch command.

$ git branch -f feature afcc8bb

This should move the pointer to the specified commit. Let’s confirm our case.

git branch -f feature

It is as simple as that. Let’s check out the other method.

the git update-ref Command

As shown below, we can use the git update-ref command to move the branch pointer.

$ git update-ref -m "reset: Reset <branch-name> to <sha1-commit-hash>" refs/heads/<branch-name> <sha1-commit-hash>

We want to move the pointer further to the 124bfa9 commit. Here is how we will do it.

First, let’s switch back to the master branch. We will then run the command, as shown below.

$ git update-ref -m "reset: Reset feature to 124bfa9" refs/heads/feature 124bfa9

The command above will move the pointer and add a message to the reflog for our feature branch. Let’s confirm our case.

git update-ref

There you have it.

In a nutshell, Git allows us to move the branch pointer while both checked out and not checked out in the destination branch. We have covered different commands for both scenarios.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Branch