Combining git navigation commands
Heres a quick git tip:
Everyone who uses git probably knows the commands git checkout
and git reset
. But for a long time, I did not really know what exactly they do.
The key is understanding that there are three different aspects of your git repo that can change: If you do a git checkout <branch>
, the branch will change, but the head and working tree will change along with it. On the other hand, if you do a git reset <branch>
, only the head will change. And then there is the option to do git reset --hard <branch>
, which is just like the regular reset, except that it also changes the working tree.
By combining these basic commands, you can achieve any state you want. Here is a full table:
branch | head | working tree | |
---|---|---|---|
- | A | A | A |
git reset --hard $B && git reset HEAD@{1} |
A | A | B |
git reset $B |
A | B | A |
git reset --hard $B |
A | B | B |
git checkout $B && git reset --hard HEAD@{1} |
B | A | A |
git checkout $B && git reset HEAD@{1} |
B | A | B |
git checkout $B && git reset --hard HEAD@{1} && git reset HEAD@{1} |
B | B | A |
git checkout $B |
B | B | B |