Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the opposite of 'git checkout HEAD^'?

I'm walking through git history with command git checkout. From master I went to e.g commit with hash a7040f35a1e.

git checkout a7040f35a1e

Then I went to the previous commit:

git checkout HEAD^

How can I return back? I mean how can I go to following commit? Is it possible with the word HEAD + or something?

like image 762
areim Avatar asked Oct 17 '25 08:10

areim


2 Answers

Git doesn't have an operator for "the following commit", as a single commit may have multiple "children". If you want to return to the previous commit, do so explicitly - git checkout a7040f35a1e.

like image 87
Mureinik Avatar answered Oct 19 '25 21:10

Mureinik


There isn't any specific way to get to the following commit, but there are a few things you can use:

  • Use git checkout -. This is shorthand for git checkout @{-1} which means “checkout the last thing I checked out”. This solves the specific case of getting back to where you just were, but not the general case of getting to the next commit from where you are.
  • Use the name of the commit, tag, or branch, e.g. after git checkout master^ you could use git checkout master.

This is probably true because of the way Git stores relationships between commits: each commit knows the ID of its parent commit(s), so it's easy to follow that relationship from x to x^, but the commits don't know the IDs of their children, so there's no efficient way to do the reverse lookup.

There's a lot of information about ways of referring to commits in the Git documentation. Checkout git help revisions.

like image 32
georgebrock Avatar answered Oct 19 '25 20:10

georgebrock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!