In git, I can specify the previous revision by saying HEAD^ or HEAD~1. What about going the other way? Suppose I’m on revision X, and I do git checkout X^. How do I go back?
Something like git checkout X+?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can’t really do precisely that. History in git is a directed acyclic graph – each commit contains references to its parents, but parents don’t have references to their children.
The problem here should become obvious when you think about a commit you created multiple branches from. Which “next commit” do you mean? With parents, you can number off (a normal merge commit has a first and second parent), but how do you do that with children? Even if you know what branch you’re trying to be on (e.g. you’ve checked out
master~4and now you want to look atmaster~3) it’s not well-defined – you could be in a situation like this:That said, in simple cases, you could do something like this:
Clearly that’ll work fine with linear history. With merges…
rev-listworks from present to past in the history, following it backward. I believe it follows the first parent first, so the thing printed last will be the commit afterHEADfound by following all last parents.Edit: That does assume that you know which branch you want to move forward toward. If you don’t… well, you’re pretty much stuck looking on all refs for commits which have the current HEAD as parent – probably grepping the output of
git rev-parse:Then grab the other SHA1 from the line (use awk, whatever). You’ll have to manually inspect or make an arbitrary choice if there are multiple results though…