areim
areim

Reputation: 3749

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?

Upvotes: 4

Views: 2142

Answers (4)

georgebrock
georgebrock

Reputation: 30043

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.

Upvotes: 4

Šimon Tóth
Šimon Tóth

Reputation: 36433

Git keeps a history of the last checked out HEADs.

You can use git checkout HEAD@{1} to the previous one.

See git reflog for the full history.

Upvotes: 1

JB.
JB.

Reputation: 42104

You can return back by using the same command you used to get there initially:

$ git checkout a7040f35a1e

Upvotes: 0

Mureinik
Mureinik

Reputation: 311273

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.

Upvotes: 8

Related Questions