How do I move forward and backward between commits in git?
I've experimented a bit and this seems to do the trick to navigate forwards (edit: it works well only when you have a linear history without merge commits):
git checkout $(git rev-list --topo-order HEAD..towards | tail -1)
where towards
is a SHA1 of the commit or a tag.
Explanation:
- the command inside
$()
means: get all the commits between currentHEAD
andtowards
commit (excludingHEAD
), and sort them in the precedence order (like ingit log
by default -- instead of the chronological order which is weirdly the default forrev-list
), and then take the last one (tail
), i.e. the one we want to go to. - this is evaluated in the subshell, and passed to
git checkout
to perform a checkout.
You can define a function accessible as a parameter-expecting alias in your .profile
file to navigate forward towards the particular commit:
# Go forward in Git commit hierarchy, towards particular commit
# Usage:
# gofwd v1.2.7
# Does nothing when the parameter is not specified.
gofwd() {
git checkout $(git rev-list --topo-order HEAD.."$*" | tail -1)
}
# Go back in Git commit hierarchy
# Usage:
# goback
alias goback='git checkout HEAD~'
I believe you can do:
git reset HEAD@{1}
To go one commit forward in time. To go forward multiple commits, use HEAD@{2}, HEAD@{3}, etc.
All you need to get clear, not detached head state is to reset, not checkout.
git reset HEAD@{1}