Git

Move Branch Pointer to Different Commit Without Checkout

Git is a fundamental tool used by developers to manage their large development projects’ source code files. Git developers interact with the GitHub hosting service through the local branches. More specifically, the Git branch is a movable pointer that points to commits. While saving changes to the Git current working repository, the default branch, for instance, “master” points to the most recent commit and moves forward after each commit.

This write-up will explain moving a branch pointer to a different branch without checkout.

How to Move Branch Pointer to Different Commit Without Checkout?

In order to move the branch pointer to a different commit without checkout, follow the provided steps:

  • Go to the desired repository.
  • View the Git log of the current branch.
  • Switch to another branch and check its Git reference log.
  • Utilize the “$ git branch -f <branch-name>” command for moving the branch pointer.
  • View the current position of the branch pointer.

Step 1: Navigate to Particular Directory
Move to the Git local directory by executing the “cd” command:

$ cd "C:\Git\test_repo"

Step 2: Check Git Log:
To view the Git log to check the current position of the branch pointer, utilize the “git log” command along with the “–oneline” flag:

$ git log --oneline

As you can see, the branch pointer is pointing to the “delete” commit:

Step 3: Switch to Another Branch
Next, switch to the desired local branch with the help of the “$ git switch” command:

$ git switch master

Step 4: View Git Log of Current Branch
Check the Git reference log to view the current position of the HEAD pointer in the current working branch:

$ git log --oneline

Here, from the below output you can see the HEAD is pointing to “master” branch:

Step 5: Move Branch Pointer
To move a branch pointer to different commit without checkout, utilize the “$ git branch” command along with the branch name:

$ git branch -f dev

Here, the “-f” option represents the “force” which will determines where the branch HEAD will be pointing:

Step 6: View Branch Pointer
Next, check the position of the HEAD using the given-below command:

$ git show HEAD

As you can see the position of the HEAD is shifted to another branch name “dev” as well as “master” branch:

Step 7: Switch to Another Branch
After that, switch to the “dev” branch to view the changes in that particular branch:

$ git switch dev

Step 8: Verify HEAD Pointer
Lastly, ensure the position of HEAD is moved to the newly created current working branch:

$ git show HEAD

It can be observed that the position of the HEAD is pointing to the “master” branch as well as “dev” branch:

We have efficiently explained the procedure of moving branch pointer to different commits without checkout.

Conclusion

To move the branch pointer to a different commit without checkout, first, navigate to the particular repository and check the Git log of the current branch. Then, switch to another branch and view its Git reference log to get where the HEAD is pointing. After that, move the branch pointer to a different commit with the help of the “$ git branch -f <branch-name>” command. Lastly, view the position of HEAD by using the “$ git show HEAD” command. This post has explained the method for moving a branch pointer to a different Git commit without checkout.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.