Reset and Switch

What is HEAD

HEAD is a pointer that represents the current commit. It can be used as a commit id, and you can also refer to commits relative to HEAD. For example, using HEAD~X (like HEAD~1, HEAD~2...) lets you refer to X commits before HEAD.

You can see the <commit-id> where HEAD is with:

git rev-parse HEAD  # for  you could use HEAD~X here

Switch to a commit (move HEAD + files)

git switch --detach <commit-id>

Reset to a commit (move HEAD only)

git reset <commit-id>

For example, here is what you can do if you want to be in the exact same state your collegue was before pushing a commit bbbb on top of a commit aaaa, with his changes in the working tree:

git switch --detach bbbb && git reset HEAD~1

git reset --hard can be used to also reset files, but be careful, unlike git switch --detach, unpushed commits will be lost, and pushed commits will need to be pulled again.

Last updated