Finding a branch point with Git?

前端 未结 22 1352
小鲜肉
小鲜肉 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:50

    In general, this is not possible. In a branch history a branch-and-merge before a named branch was branched off and an intermediate branch of two named branches look the same.

    In git, branches are just the current names of the tips of sections of history. They don't really have a strong identity.

    This isn't usually a big issue as the merge-base (see Greg Hewgill's answer) of two commits is usually much more useful, giving the most recent commit which the two branches shared.

    A solution relying on the order of parents of a commit obviously won't work in situations where a branch has been fully integrated at some point in the branch's history.

    git commit --allow-empty -m root # actual branch commit
    git checkout -b branch_A
    git commit --allow-empty -m  "branch_A commit"
    git checkout master
    git commit --allow-empty -m "More work on master"
    git merge -m "Merge branch_A into master" branch_A # identified as branch point
    git checkout branch_A
    git merge --ff-only master
    git commit --allow-empty -m "More work on branch_A"
    git checkout master
    git commit --allow-empty -m "More work on master"
    

    This technique also falls down if an integration merge has been made with the parents reversed (e.g. a temporary branch was used to perform a test merge into master and then fast-forwarded into the feature branch to build on further).

    git commit --allow-empty -m root # actual branch point
    git checkout -b branch_A
    git commit --allow-empty -m  "branch_A commit"
    git checkout master
    git commit --allow-empty -m "More work on master"
    git merge -m "Merge branch_A into master" branch_A # identified as branch point
    git checkout branch_A
    git commit --allow-empty -m "More work on branch_A"
    
    git checkout -b tmp-branch master
    git merge -m "Merge branch_A into tmp-branch (master copy)" branch_A
    git checkout branch_A
    git merge --ff-only tmp-branch
    git branch -d tmp-branch
    
    git checkout master
    git commit --allow-empty -m "More work on master"
    

提交回复
热议问题