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
vs git switch --detach
git reset --hard
vs git switch --detach
git reset --hard
: Modifies your current branch history (by moving the branch pointer) and destroys uncommitted local changes. Can be used to change commits history and pushed with git push -f
git switch --detach
: Does not modify your branch history but puts you in a state where new commits are "detached" from any branch. It's for exploration or temporary work without altering your main branches.
Last updated