Navigate to Other Commits
To go to a specific commit in the reflog history:
Like the terminal saves a history of all the commands executed in the past, Git also saves a log of where our HEAD and branch references have been for the past few months. This information cna be viewed using the command git reflog
.
git checkout HEAD@{5}
git checkout@{yesterday}
To get to the immediate parent of the current commit:
git checkout HEAD^
git checkout HEAD~
To get back some commits in ancestry:
git checkout HEAD~3
git checkout HEAD~n
To go to some nth parent of the current commit:
The following syntax is useful only for merge commits, which have more than one parent — the first parent of a merge commit is from the branch we were on when you merged (frequently main
), while the second parent of a merge commit is from the branch that was merged (say, topic
):
git checkout HEAD^2
git checkout HEAD^n
So, to get back to the 2nd parent of 3 commits behind HEAD, we can use:
git checkout HEAD~3^2
And to get back 3 commits behind the 2nd parent of HEAD, we can use:
git checkout HEAD^2~3
Source: Git - Revision Selection