Referring to the previous/next commit in git?

前端 未结 3 1126
野的像风
野的像风 2021-01-01 11:31

I have seen git commands use a syntax such as HEAD~, but I haven\'t been able to find this syntax in the Git Reference Manual.

Here is what I have under

相关标签:
3条回答
  • 2021-01-01 12:08

    Inspired by @cexbrayat's answer, I find it useful to think of it this way:

    How to refer to something in a commit's ancestry, where a commit can have multiple parents:

    • ^n specifies which parent

    • ~n specifies which generation

    Both default to one.

    0 讨论(0)
  • 2021-01-01 12:17

    You have a very clear explanation of how this works in the chapter on Acenstry References in Pro Git:

    • ~ is used to get the first parent.
    • ^ can be used to get the other parents (^2, for example, for a merge).

    But you don't have a simple way to reference the next commit, even if there are more convoluted ways to get it.

    0 讨论(0)
  • 2021-01-01 12:21

    To simply answer the question from title (since that's what got me here from Google):

    To checkout the previous commit:

    git checkout HEAD^
    

    To checkout the next commit (assuming there's no branching):

    git checkout `git log --reverse --ancestry-path HEAD..master | head -n 1 | cut -d \  -f 2`
    
    0 讨论(0)
提交回复
热议问题