git 显示提交的原始分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4535251/
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
Show the original branch for a commit
提问by Craig Walker
I've used git-blame to find a particular commit. Now I want to find the branch that it originally came from. (From there, I'll use the branch name to find the particular ticket)
我使用 git-blame 来查找特定的提交。现在我想找到它最初来自的分支。(从那里,我将使用分支名称来查找特定票证)
Let's define "original branch" as "the branch to which the commit was made before the branch was merged into any other branch".
让我们将“原始分支”定义为“在分支合并到任何其他分支之前提交的分支”。
回答by Francois G
Like the others said, if the branch you are looking for isn't local to the repository on which you are blaming this commit (e.g. a branch only in the personal repo of a distant developer), you are screwed.
就像其他人说的那样,如果您要查找的分支不是您将此次提交归咎于其上的存储库的本地分支(例如,仅在远程开发人员的个人存储库中的分支),那么您就完蛋了。
But assuming that sought-after branch is something you can see, and that of course you have the commit's hash, say d590f2...
, a partial answer is that you can do :
但是假设受欢迎的分支是你可以看到的,当然你有提交的哈希,比如d590f2...
,部分答案是你可以这样做:
$ git branch --contains d590f2
tests
* master
Then, just to confirm you have the culprit:
然后,只是为了确认你有罪魁祸首:
$ git rev-list tests | grep d590f2
Of course, if d590f2
has been merged in more than one branch, you will have to be more subtle than this.
当然,如果d590f2
已经合并到多个分支,你就得比这更微妙了。
回答by araqnid
That isn't really applicable in git. Branches are local concepts to each repository: one person's "local-stuff" branch can be quite separate from another person's "local-stuff" branch. If you do something like looking at your main integration branch, and your query commit, and removing all the merge bases between the two, you should be able to get a subtree of the commit history that may shed some illumination... or may not. e.g. if you trace up the link from the query commit towards "master" you should hopefully find merge commits with useful comments saying where the merge came from... but this information is just informational, not recorded in some way intended to be automatically retrieved.
这在 git 中并不真正适用。分支是每个存储库的本地概念:一个人的“local-stuff”分支可以与另一个人的“local-stuff”分支完全分开。如果您执行诸如查看主集成分支和查询提交并删除两者之间的所有合并基础之类的操作,您应该能够获得可能会产生一些启发的提交历史的子树……或者可能不会. 例如,如果您跟踪从查询提交到“master”的链接,您应该希望找到带有有用注释的合并提交,说明合并来自哪里......但这些信息只是提供信息,而不是以某种旨在自动检索的方式记录.
e.g. gitk some-commit...master
(which is almost short for gitk some-commit master --not $(git merge-base some-commit master)
)
例如gitk some-commit...master
(几乎是 的缩写gitk some-commit master --not $(git merge-base some-commit master)
)
回答by Mot
A Git branch is nothing else than a "named pointer to a commit" (that's a fundamental different concept than in other well-known VCS).
Git 分支只不过是一个“指向提交的命名指针”(这是一个与其他知名 VCS 中的根本不同的概念)。
This situation is clear, commit A is on branch-1
, commit B on branch-2
:
这种情况很清楚,commit A on branch-1
,commit B on branch-2
:
o A [branch-1]
|
o | B [branch-2]
| |
After merging is becomes unclear whether A (or B) originally was on branch-1
or branch-2
:
合并后变得不清楚A(或B)最初是在branch-1
还是branch-2
:
o [branch-1] [branch-2]
|
o merged
|\
| o A
| |
o | B
| |
Maybe you can guess on what Git branch the commit A was if you have tagged parent commits of A, e.g. release-1
and you know that this tag only was given for commits in branch-1
.
如果您标记了 A 的父提交,也许您可以猜测提交 A 是哪个 Git 分支,例如,release-1
并且您知道此标记仅用于branch-1
.
o [branch-1] [branch-2]
|
o merged
|\
| o A
| |
o | B
| |
| o <release-1]
| |
回答by CharlesB
I give a try, please comment since not totally sure, but I believe it does the job.
我试一试,请发表评论,因为不完全确定,但我相信它可以完成工作。
The following will work only if the branches still point at the tip of before being merged into master, which is the case if the branches were on the same repo:
仅当分支在合并到 master 之前仍然指向的尖端时,以下内容才起作用,如果分支位于同一个 repo 上就是这种情况:
o [master]
|
o merged branch "great-feature" into master
|\
| o A [great-feature]
| |
o | B
| |
If it it not the case (for example if you pulled from another repo) you can still recreate them by hand.
如果不是这种情况(例如,如果您从另一个存储库中提取),您仍然可以手动重新创建它们。
First get the branches where your commit are:
首先获取提交所在的分支:
$ git branch -a --contains=<sha-of-B>
*master
great-feature
then for each branch get the number of commits that separate their head to the commit: this is the number of lines that output git log for the specified range:
然后对于每个分支,获取将其头部与提交分开的提交数:这是指定范围内输出 git log 的行数:
$ git log --pretty=oneline <sha-of-B>..great-feature | wc -l
1
$ git log --pretty=oneline <sha-of-B>..master | wc -l
4
So B is nearest to great-feature, which means it was created in it.
所以B最接近great-feature,这意味着它是在其中创建的。
This could be made into a nice script, feel free to add it to the answer (I'm not good at this)
这可以制作成一个不错的脚本,随时将其添加到答案中(我不擅长这个)
回答by Ochko
First ensure you fetched changes from remotes
首先确保您从遥控器获取更改
$ git fetch --all
And,
和,
$ git branch -a --contains d590f2
Without -aoption you can't find commits existing only on remote branches
如果没有-a选项,您将无法找到仅存在于远程分支上的提交
回答by Eugen Konkov
When you on the branch the "original branch"was merged to. You may run:
当您在分支上时,“原始分支”已合并到。你可以运行:
git log <SHA>..HEAD --ancestry-path --merges
This command will show all merge
commits between <SHA>..HEAD
. You need last one.
该命令将显示所有merge
与提交<SHA>..HEAD
。你需要最后一个。
For example for c0118fa
commit (last but one) the "original branch"is redesign_interactions
例如,对于c0118fa
提交(最后一个),“原始分支”是redesign_interactions
* ccfd449 (HEAD -> develop) Require to return undef if no digits found
* 93dd5ff Merge pull request #4 from KES777/clean_api
|\
| * 39d82d1 Fix tc0118faests for debugging debugger internals
| * ed67179 Move &push_frame out of core
| * 2fd84b5 Do not lose info about call point
| * 3ab09a2 Improve debugger output: Show info about emitted events
| * a435005 Merge branch 'redesign_interactions' into clean_api
| |\
| | * a06cc29 Code comments
| | * d5d6266 Remove copy/paste code
| | * c0118fa Allow command to choose how continue interaction
| | * 19cb534 Emit &interact event
You should run:
你应该运行:
git log c0118fa..HEAD --ancestry-path --merges
And scroll down to find last commit. Which is:
并向下滚动以找到最后一次提交。这是:
commit a435005445a6752dfe788b8d994e155b3cd9778f
Merge: 0953cac a06cc29
Author: Eugen Konkov
Date: Sat Oct 1 00:54:18 2016 +0300
Merge branch 'redesign_interactions' into clean_api
回答by CharlesB
I found a simpler way to do it: it's in the message of the last commit of git log <sha>..HEAD --merges
!
我找到了一种更简单的方法:它在最后一次提交的消息中git log <sha>..HEAD --merges
!
This command shows the merges that have happened between master and the commit; the last commit output by this command is the first merge commit that included it. It usually contains the branch name, so even if the branch was deleted you can find its name.
此命令显示 master 和 commit 之间发生的合并;此命令的最后一次提交输出是包含它的第一个合并提交。它通常包含分支名称,因此即使删除了分支,您也可以找到它的名称。
To get only the name of the branch just type git log <sha>..HEAD --merges --oneline |tail -1
要仅获取分支的名称,只需键入 git log <sha>..HEAD --merges --oneline |tail -1
回答by Peter Kahn
It seems like this isn't a question that's possible to answer with 100% precision via git.
似乎这不是一个可以通过 git 100% 准确回答的问题。
git branch --contains --merge <sha1>
returns a list of all branches to which the commit was merge and the original branch. --no-mergedreturns all subsequent branches that include the commit because they branched after the merge point.
返回提交合并到的所有分支和原始分支的列表。 --no-merged返回包含提交的所有后续分支,因为它们在合并点之后分支。
So, you can get a list of each merge but not original branch and any branch deleted prior to command execution is lost (or your looking at reflogs)
因此,您可以获得每个合并但不是原始分支的列表,并且在命令执行之前删除的任何分支都将丢失(或者您正在查看引用日志)
Results
结果
git branch --contains <sha1 for "added feature/inital 1">
* develop
feature/inital
feature/subsequent1
git branch --contains <sha1 for "added feature/inital 1"> --merged
* develop
feature/inital
git branch --contains <sha1 for "added feature/inital 1"> --no-merged
feature/inital
Test Script
测试脚本
function mkbranch {
git checkout -b
git push --set-upstream origin
}
# Develop
mkbranch develop
for f in 1 2 3; do date > file${f}.txt; git add file${f}.txt; git commit -m "added develop $f"; done
git push
# Initial Feature Branch
mkbranch feature/inital
for f in 1 3; do date > file${f}.txt; git add file${f}.txt; git commit -m "modified feature/inital $f"; done
git push
# Merge
git checkout -b develop
git merge feature/inital
git push
# Next Feature Branch
mkbranch feature/subsequent1
for f in 1 3; do date > file${f}.txt; git add file${f}.txt; git commit -m "modified feature/subsequent1 $f"; done
git push
回答by ed209
This worked well enough for me to get the branch name from a detached head in a Jenkins workspace:
这对我来说非常有效,可以从 Jenkins 工作区中的分离头获取分支名称:
git show -s --pretty=%d
git show -s --pretty=%d