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:
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:
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:
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:
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:
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:
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:
Step 8: Verify HEAD Pointer
Lastly, ensure the position of HEAD is moved to the newly created current working branch:
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.