ref^
refers to the commit before ref
, what about getting the commit after ref
?
For example, if I git checkout 12345
how do I check out the next commit?
Thanks.
PS Yes, git's a DAG node pointer struct tree whatever. How do I find the commit after this one?
To list all the commits, starting from the current one, and then its child, and so on - basically standard git log, but going the other way in time, use something like
where 894e8b4e93d8f3 is the first commit you want to show.
I know what you mean. It's frustrating to have plentiful syntax for going to previous commits, but none to go to the next ones. In a complex history, the problem of "what is the next commit" becomes rather hard, but then in complex merging the same hardness emerges with 'previous' commits as well. In the simple case, inside a single branch with a linear history (even just locally for some limited number of commits) it would be nice and make sense to go forward and backward.
The real problem with this, however, is that the children commits are not referenced, it's a backwards-linked list only. Finding the child commit takes a search, which isn't too bad, but probably not something git wants to put into the refspec logic.
At any rate, I came upon this question because I simply want to step forward in the history one commit at a time, doing tests, and sometimes you have to step forward and not backward. Well, with some more thought I came up with this solution:
Pick a commit ahead of where you're at. This could probably be a branch head. If you're at branch~10, then "git checkout branch~9" then "git checkout branch~8" to get the next after that, then "git checkout branch~7" and so on.
Decrementing the number should be really easy in a script if you need it. A lot easier than parsing git rev-list.
This post (http://www.jayway.com/2015/03/30/using-git-commits-to-drive-a-live-coding-session/#comment-282667) shows a neat way if doing it if you can create a well defined tag at the end of your commit stack. Essentially
git config --global alias.next '!git checkout `git rev-list HEAD..demo-end | tail -1`'
where "demo-end" is the last tag.Each commit stores a pointer to its parent (parents, in case of merge(standard) commit).
So, there is no way to point to a child commit (if there is one) from the parent.
Existing answers assumes you have a branch (or a ref) containing the commit you're looking for.
In my case, the commit I was looking for wasn't in
git rev-list --all
, since no branch contained that. I ended up looking throughgitk --reflog
manually.If you can't find your commit even in reflog, try
git fsck --full
to list dangling (i.e. not in any branch) commits, orgit fsck --lost-found
to make refs and apply techniques in the other answers.what I found is
where I set
commit1
as the current commit andcommit2
to the current head. This returns me a list of all commits which build a path betweencommit1
andcommit2
.The last line of the output is the child of commit1 (on the path to commit2).