How do I find the next commit in git? (child/child

2019-01-02 20:01发布

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?

14条回答
与风俱净
2楼-- · 2019-01-02 20:18

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

git log --reverse --ancestry-path 894e8b4e93d8f3^..master

where 894e8b4e93d8f3 is the first commit you want to show.

查看更多
低头抚发
3楼-- · 2019-01-02 20:21

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.

查看更多
若你有天会懂
4楼-- · 2019-01-02 20:23

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.

查看更多
其实,你不懂
5楼-- · 2019-01-02 20:25

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.

查看更多
孤独寂梦人
6楼-- · 2019-01-02 20:27

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 through gitk --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, or git fsck --lost-found to make refs and apply techniques in the other answers.

查看更多
看淡一切
7楼-- · 2019-01-02 20:28

what I found is

git rev-list --ancestry-path commit1..commit2

where I set commit1 as the current commit and commit2 to the current head. This returns me a list of all commits which build a path between commit1 and commit2.

The last line of the output is the child of commit1 (on the path to commit2).

查看更多
登录 后发表回答