How to Move Git Branch Pointer to Different Commit
Read the tutorial to learn several techniques of moving branch pointer to different git commit without checkout and the essential tips one should encounter.
Moving the branch pointer of a checked-out branch can be done with the git reset command followed by the <kbd class="highlighted">--hard</kbd> option. However, this command cannot handle moving a non-checked-out branch to point at a different git commit.
Moving a branch pointer to another commit
If you want to move a non-checked-out branch to another commit, the easiest way is running the git branch command with the <kbd class="highlighted">-f</kbd> option, which determines where the branch <kbd class="highlighted">HEAD</kbd> should be pointing to:
move a branch to another commit, git branch
git branch -f <branch-name> <sha1-commit-hash>While git branch -f works on the current branch, it is generally safer to use git reset --hard for checked-out branches to avoid losing uncommitted changes.
To move a branch pointer, run the following command:
move a branch pointer run, git update-ref
git update-ref -m "reset: Reset <branch-name> to <sha1-commit-hash>" refs/heads/<branch-name> <sha1-commit-hash>The <kbd class="highlighted">git update-ref</kbd> command updates the object name stored in a ref safely.
The git branch Command
Branches are an essential part of an everyday Git workflow. The <kbd class="highlighted">git branch</kbd> command is designed to create, list, and delete branches but does not allow switching between them. It is often used alongside the git checkout and git merge commands. Generally, Git branches are pointers to a snapshot of changes. A newly created branch encapsulates changes when you want to fix bugs or add features. This helps maintain a clean commit history before merging.