Git - Moving around
Moving around in Git
HEAD
First we have to talk about “HEAD”. HEAD is the symbolic name for the currently checked out commit – it’s essentially what commit you’re working on top of.
HEAD always points to the most recent commit which is reflected in the working tree. Most git commands which make changes to the working tree will start by changing HEAD.
Normally HEAD points to a branch name (like bugFix). When you commit, the status of bugFix is altered and this change is visible through HEAD.

This would result in the following:

Detatching HEAD

This would result in the following:

Relative Refs
Specifying commits by their hash isn’t the most convenient thing ever, which is why Git has relative refs.
With relative refs, you can start somewhere memorable (like the branch bugFix or HEAD) and work from there.
Relative commits are powerful, but we will introduce two simple ones here:
- Moving upwards one commit at a time with the Caret (^) operator.
- Moving upwards a number of times with The ~ operator. ~<num>
Refer to the parent commit using the Caret (^) operator

This would result in the following:

Using HEAD as a relative ref:

This would result in the following:

The ^ and ~ modifiers can make moving around a commit tree very powerful:

This would result in:

These modifiers can be chained together!

The same movement as before, but all in one command.
Move up in the commit tree using the ~ operator
Say you want to move a lot of levels up in the commit tree. It might be tedious to type ^ several times, so Git also has the tilde (~) operator.
The tilde operator (optionally) takes in a trailing number that specifies the number of parents you would like to ascend.

This would result in the following:

Branch forcing
Let’s actually use relative refs for something.
One of the most common ways to use relative refs is to move branches around. You can directly reassign a branch to a commit with the -f option. So something like:
git branch -f main HEAD~3
moves (by force) the main branch to three parents behind HEAD.

This would result in the following:
