git relative reference - ghdrako/doc_snipets GitHub Wiki

Ancestry references can be used to specify a commit in git - is denoted by two special characters -

  • ^ – Caret
  • ~ – Tilde

Parents commit

HEAD^
HEAD~
HEAD~1

Grandparents commit

HEAD^^HEAD~2
              9ec05ca
                 |
              db7e87a
                 |
              796ddb0
                /\
               /  \
         0c5975a  4c9749e
              \    /
               \  /
              1a56a81


HEAD^^^ --> ((HEAD^1)^1)^1
        --> ((db7e87a)^1)^1
        --> (796ddb0)^1
        --> 0c5975a

X~n means: The nth ancestor of X.

X^ means: The parent of X. This is equivalent to X~1.

If X has more than one parent, one needs to distinguish between them when using the ^ notation. So X^1 would be the first parent, X^2 would be the second parent, and so on. X^ is equivalent to X^1 (and also equivalent to X~1).

git branch -f main HEAD~3  # przenosi (na siłę) gałąź main trzy commity wstecz - za HEADa

Go forward

git reset HEAD@{1}

To go one commit forward in time. To go forward multiple commits, use HEAD@{2}, HEAD@{3}, etc.