git 如何确定特定分支的源分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6374564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:11:50  来源:igfitidea点击:

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

gitgit-branch

提问by Arne Claassen

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

我在 git 中有一个分支,想从它最初分支的分支和提交的分支中找出。

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.

Github 似乎知道,因为当您执行拉取请求时,它通常会自动设置它应该进入的分支,但我无法弄清楚如何从命令行手动执行此操作。

Let me add a concrete example:

让我补充一个具体的例子:

master -- ongoing development
2.2    -- stable maintenance

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

feature创建了一个功能分支(在B下面的提交中)并处理 ( B', C'& E') 并与源分支合并以获取CD

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

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

现在我想合并feature回它的源,但我不确定它最初是一个分支master还是2.2. 为了将功能合并到正确的源中,是否有一种编程方式来确定源分支是master还是2.2

采纳答案by Frosty

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

Git 仅跟踪单个存储库中的“上游”信息,并且此信息不是静态的,也不是在同一存储库的不同克隆之间共享。

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 的输出可能会给你一个线索:

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 或 git log 来查看:

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

回答by Igor Tverdovskiy

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).

使用 --contains,仅显示包含命名提交的分支(换句话说,其提示提交是命名提交的后代的分支)。使用--merged,只会列出合并到命名提交中的分支(即可以从命名提交访问其提示提交的分支)。使用 --no-merged 只会列出未合并到命名提交中的分支。如果缺少参数,则默认为 HEAD(即当前分支的尖端)。

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

考虑到之前的提交已经推送到远程分支,可以使用 --contains 键过滤可用的远程分支。

# 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:

如果您有 2 个未推送的提交,则相应地更改数量以达到已推送的提交:

# 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.

当然,为了安全起见,您可以限制迭代量。

回答by Seth Robertson

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).

第一个将向您显示分支并为您提供合并信息。第二个将帮助您了解两个特定分支(它们最后分歧的地方)之间的关系。