Open In App

How To Move Branch Pointer To Different Commit Without Checkout?

Last Updated : 25 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Git pros frequently utilize this strong approach to rebase branches, correct errors, and modify branch histories: shifting the branch pointer to a different commit without checking out. With this approach, you can modify the commit that a branch points to without affecting the status of your working directory. When you need to make corrections to the branch history without disrupting your ongoing work, this might be really helpful.

Approach 1: Using 'git branch -f' Command:

This is the most simple way to move a branch pointer. The git branch -f command forcefully updates the branch to point to a new commit.

  • Identify the target commit hash to which you want to move the branch pointer.
  • Execute the command:
git branch -f <branch-name> <commit-hash>

Example:

To move the branch feature to commit 1e50717, run:

git branch -f feature 1e50717
Annotation-2024-07-16-010631
Using 'git branch -f' Command

Approach 2: Using git update-ref Command

The git update-ref command updates the reference of a branch directly. This method is slightly more low-level than using git branch -f.

  • Identify the target commit hash.
  • Execute the command:
git update-ref refs/heads/<branch-name> <commit-hash>

Example:

To move the branch feature to commit 1e50717, run:

git update-ref refs/heads/abcBranch 069b95e

Output:

Annotation-2024-07-23-135141
Using git update-ref Command



Next Article
Article Tags :

Similar Reads