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

时间:2015-09-14 15:33:26

标签: git git-checkout

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?

4 个答案:

答案 0 :(得分:5)

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.

答案 1 :(得分:2)

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.

答案 2 :(得分:1)

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.

答案 3 :(得分:0)

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

$ git checkout a7040f35a1e