git,

Move Git branch pointer to different commit w/o checkout

Oct 14, 2022 · 1 min read · Post a comment

Working with Git on a daily basis could lead to some special scenarios now and then. This post is all about manipulating the Git branch pointer to different commit w/o checking out.

Prerequisites

  • Git

Solution(s)

Move the current checked out branch pointer to another commit

git reset --soft <commit-SHA1>

Note(s): Try replacing --soft with --hard if facing any issues, or you don’t care if you lose any changes.

Move branch pointer of a non-checked out branch

git branch -f <branch_name> <commit-SHA1>

Note(s) about <commit-SHA1>:

  • represents the commit ID → SHA1 hash e.g. 1100365e469885fd1ecbe471f0cee1a60495f8ef.
  • if left out will point to whatever commit the current HEAD points to.
  • could be branch name as well e.g. main.

Alternative command to achieve the same thing:

git update-ref -m "reset: Reset <branch_name> to <commit-SHA1>" refs/heads/<branch_name> <commit-SHA1>

Conclusion

To find more neat Git commands and hacks, simply browse the Git category. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.

git