How do I determine the source branch of a particular branch?

我的梦境 提交于 2019-12-10 00:35:22

问题


I have a branch in git and want to figure out from what branch it originally was branched and at what commit.

Github seems to know, since when you do a pull request it usually automatically sets up what branch it should go into, but I can't figure out how to do this manually from the command line.

Let me add a concrete example:

master -- ongoing development
2.2    -- stable maintenance

A feature branch feature was created (at commit B below) and worked on (B', C' & E') and merged against the source branch to pick up C and D

 feature branch:    B'-C'-C--D--E'
                   /     /       
 source branch: A--B--C--D--E-- ...

Now I want to merge feature back into its source, but I am not sure whether it was originally a branch off master or 2.2. In order to merge the feature into the correct source, is there a programmatic way to find out if source branch is master or 2.2?


回答1:


Git only tracks "upstream" information in individual repositories and this information is not static nor shared between separate clones of the same repository.

The command to set this relationship from the command line is:

git branch --set-upstream <branch> [<start-point>]

Looking at the output of git-diff might give you a clue:

git diff <mybranch>..master # commits in master that are not in mybranch
git diff <mybranch>..2.2 # commits in 2.2 that are not in mybranch

It is likely that the one with fewer commits listed is the branch point (but that is not guaranteed, obviously.

You can also use gitk, or git log to take a look around:

gitk --all
git log --graph --color --decorate --oneline --all



回答2:


git show-branch [--all]
git merge-base

The first will show you branches and give you merge information. The second will help you understand the relationship between two specific branches (where they last diverged).




回答3:


git branch -r

With --contains, shows only the branches that contain the named commit (in other words, the branches whose tip commits are descendants of the named commit). With --merged, only branches merged into the named commit (i.e. the branches whose tip commits are reachable from the named commit) will be listed. With --no-merged only branches not merged into the named commit will be listed. If the argument is missing it defaults to HEAD (i.e. the tip of the current branch).

Considering that the previous commit has been already pushed to the remote branch, you can filter available remote branches using --contains key.

# git branch -r --contains HEAD~1
  origin/HEAD -> origin/master
  origin/master

or

git branch -r --contains refs/heads/<local_branch_name>~1

If you have 2 non-pushed commits yet than change the number accordingly in order to reach already pushed commit:

# git branch -r --contains HEAD~2
  origin/<parent_remote_branch>

If the amount of local non-pushed commits is unknown you may cycle through until you get the result, for example:

#!bin/bash

i=0;
res=; 
while [ -z "$res" ]; do 
    res=$(git branch -r --contains HEAD~$i); 
    echo "$i"; 
    i=$(($i+1)); 
done

echo "$res"

Of course you can limit iterations amount to be on safe side.



来源:https://stackoverflow.com/questions/6374564/how-do-i-determine-the-source-branch-of-a-particular-branch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!