Finding a branch point with Git?

前端 未结 22 1325
小鲜肉
小鲜肉 2020-11-22 10:11

I have a repository with branches master and A and lots of merge activity between the two. How can I find the commit in my repository when branch A was created based on mast

相关标签:
22条回答
  • 2020-11-22 10:53

    The following command will reveal the SHA1 of Commit A

    git merge-base --fork-point A

    0 讨论(0)
  • 2020-11-22 10:57

    I seem to be getting some joy with

    git rev-list branch...master
    

    The last line you get is the first commit on the branch, so then it's a matter of getting the parent of that. So

    git rev-list -1 `git rev-list branch...master | tail -1`^
    

    Seems to work for me and doesn't need diffs and so on (which is helpful as we don't have that version of diff)

    Correction: This doesn't work if you are on the master branch, but I'm doing this in a script so that's less of an issue

    0 讨论(0)
  • 2020-11-22 10:57

    You can examine the reflog of branch A to find from which commit it was created, as well as the full history of which commits that branch pointed to. Reflogs are in .git/logs.

    0 讨论(0)
  • 2020-11-22 10:58

    A simple way to just make it easier to see the branching point in git log --graph is to use the option --first-parent.

    For example, take the repo from the accepted answer:

    $ git log --all --oneline --decorate --graph
    
    *   a9546a2 (HEAD -> master, origin/master, origin/HEAD) merge from topic back to master
    |\  
    | *   648ca35 (origin/topic) merging master onto topic
    | |\  
    | * | 132ee2a first commit on topic branch
    * | | e7c863d commit on master after master was merged to topic
    | |/  
    |/|   
    * | 37ad159 post-branch commit on master
    |/  
    * 6aafd7f second commit on master before branching
    * 4112403 initial commit on master
    

    Now add --first-parent:

    $ git log --all --oneline --decorate --graph --first-parent
    
    * a9546a2 (HEAD -> master, origin/master, origin/HEAD) merge from topic back to master
    | * 648ca35 (origin/topic) merging master onto topic
    | * 132ee2a first commit on topic branch
    * | e7c863d commit on master after master was merged to topic
    * | 37ad159 post-branch commit on master
    |/  
    * 6aafd7f second commit on master before branching
    * 4112403 initial commit on master
    

    That makes it easier!

    Note if the repo has lots of branches you're going to want to specify the 2 branches you're comparing instead of using --all:

    $ git log --decorate --oneline --graph --first-parent master origin/topic
    
    0 讨论(0)
提交回复
热议问题