用Git找到一个分支点? [英] Finding a branch point with Git?

查看:115
本文介绍了用Git找到一个分支点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个分支机构master和A的仓库,两者之间有很多合并活动。我的仓库基本上是这样的:
$ b在我的仓库中,当我根据主人创建分支A时,如何找到提交信息?

$ b

   -  X  -  A  -  B  -  C  -  D  -  F(主)
\\ / \ /
\\ \\ / \ /
G - H - I - J(分行A)

我正在寻找修订版A,它不是 git merge-base(--all)找到的。

解决方案

我正在寻找同样的东西,并且我发现了这个问题。感谢您提出这个问题!



然而,我发现我在这里看到的答案似乎并没有给出您要求的答案(或者我正在寻找) - 他们似乎给了 G 提交,而不是 A 提交。



因此,我创建了以下树(按时间顺序分配的字母),所以我可以测试一下:

  A  -  B  -  D  -  F  -  G < - master分支(在G)
\\
C - E - - '< - topicbranch(still at E)

这看起来有点不同于你的,因为我想确保我得到(指这张图,不是你的)B,但不是A(而不是D或E)。这里是附加到SHA前缀和提交消息的字母(我的repo可以从注意到这一点):

  [别名] 
最老祖先=!zsh -c'diff -u<(git rev-list - -first-parent$ {1:-master})<(git rev-list --first-parent$ {2:-HEAD})| sed -ne \s / ^ // p \|头-1' -

可以通过下面的命令行来进行:

  git config --global alias.oldest-ancestor'!zsh -c'\''diff -u <( git rev-list --first-parent$ {1:-master})<(git rev-list --first-parent$ {2:-HEAD})| sed -nes / ^ // p|头部-1''''''

注意: zsh 可以很容易地就是 bash ,但是 sh will not 工作 - 在 sh 中不存在<()语法。 (再次感谢你,@conny,让我意识到它在本页另一个答案的评论!)



注意:上面的替代版本:



感谢 liori 指出上述内容在比较相同的分支时可能会下降,并提出一个替代差异表单它会从混合中删除sed格式,并使这个更安全(即,即使您将master与master进行比较,也会返回结果(即最近的提交)):

作为.git-config行:

  [别名] 
最老祖先=!zsh -c '(git rev-list --first-parent$ {1:-master})<(git rev-line-format =''--new-line-format =''< list --first-parent$ {2:-HEAD})|头部-1' -

从shell:

  git config --global alias.oldest-ancestor'!zsh -c'\''diff --old-line-format ='' - 新行格式=''<(git rev-list --first-parent$ {1:-master})<(git rev-list --first-parent$ {2:-HEAD})|头-1'\'' - '

所以,在我的测试树中一段时间,对不起,它回来了),现在适用于主题和主题(分别提交G和B)。再次感谢liori替代形式。






所以,这就是我和[liori]提出的。它似乎为我工作。它还允许额外的几个别名,可能证明很方便:

  git config --global alias.branchdiff'!sh -c git diff`git old-ancestor` ..'
git config --global alias.branchlog'!sh -cgit log`git old-ancestor` ..''

Happy git-ing!


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 master?

My repository basically looks like this:

-- X -- A -- B -- C -- D -- F  (master) 
          \     /   \     /
           \   /     \   /
             G -- H -- I -- J  (branch A)

I'm looking for revision A, which is not what git merge-base (--all) finds.

解决方案

I was looking for the same thing, and I found this question. Thank you for asking it!

However, I found that the answers I see here don't seem to quite give the answer you asked for (or that I was looking for) -- they seem to give the G commit, instead of the A commit.

So, I've created the following tree (letters assigned in chronological order), so I could test things out:

A - B - D - F - G   <- "master" branch (at G)
     \   \     /
      C - E --'     <- "topic" branch (still at E)

This looks a little different than yours, because I wanted to make sure that I got (referring to this graph, not yours) B, but not A (and not D or E). Here are the letters attached to SHA prefixes and commit messages (my repo can be cloned from here, if that's interesting to anyone):

G: a9546a2 merge from topic back to master
F: e7c863d commit on master after master was merged to topic
E: 648ca35 merging master onto topic
D: 37ad159 post-branch commit on master
C: 132ee2a first commit on topic branch
B: 6aafd7f second commit on master before branching
A: 4112403 initial commit on master

So, the goal: find B. Here are three ways that I found, after a bit of tinkering:


1. visually, with gitk:

You should visually see a tree like this (as viewed from master):

or here (as viewed from topic):

in both cases, I've selected the commit that is B in my graph. Once you click on it, its full SHA is presented in a text input field just below the graph.


2. visually, but from the terminal:

git log --graph --oneline --all

(Edit/side-note: adding --decorate can also be interesting; it adds an indication of branch names, tags, etc. Not adding this to the command-line above since the output below doesn't reflect its use.)

which shows (assuming git config --global color.ui auto):

Or, in straight text:

*   a9546a2 merge from topic back to master
|\  
| *   648ca35 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

in either case, we see the 6aafd7f commit as the lowest common point, i.e. B in my graph, or A in yours.


3. With shell magic:

You don't specify in your question whether you wanted something like the above, or a single command that'll just get you the one revision, and nothing else. Well, here's the latter:

diff -u <(git rev-list --first-parent topic) \
             <(git rev-list --first-parent master) | \
     sed -ne 's/^ //p' | head -1
6aafd7ff98017c816033df18395c5c1e7829960d

Which you can also put into your ~/.gitconfig as (note: trailing dash is important; thanks Brian for bringing attention to that):

[alias]
    oldest-ancestor = !zsh -c 'diff -u <(git rev-list --first-parent "${1:-master}") <(git rev-list --first-parent "${2:-HEAD}") | sed -ne \"s/^ //p\" | head -1' -

Which could be done via the following (convoluted with quoting) command-line:

git config --global alias.oldest-ancestor '!zsh -c '\''diff -u <(git rev-list --first-parent "${1:-master}") <(git rev-list --first-parent "${2:-HEAD}") | sed -ne "s/^ //p" | head -1'\'' -'

Note: zsh could just as easily have been bash, but sh will not work -- the <() syntax doesn't exist in vanilla sh. (Thank you again, @conny, for making me aware of it in a comment on another answer on this page!)

Note: Alternate version of the above:

Thanks to liori for pointing out that the above could fall down when comparing identical branches, and coming up with an alternate diff form which removes the sed form from the mix, and makes this "safer" (i.e. it returns a result (namely, the most recent commit) even when you compare master to master):

As a .git-config line:

[alias]
    oldest-ancestor = !zsh -c 'diff --old-line-format='' --new-line-format='' <(git rev-list --first-parent "${1:-master}") <(git rev-list --first-parent "${2:-HEAD}") | head -1' -

From the shell:

git config --global alias.oldest-ancestor '!zsh -c '\''diff --old-line-format='' --new-line-format='' <(git rev-list --first-parent "${1:-master}") <(git rev-list --first-parent "${2:-HEAD}") | head -1'\'' -'

So, in my test tree (which was unavailable for a while, sorry; it's back), that now works on both master and topic (giving commits G and B, respectively). Thanks again, liori, for the alternate form.


So, that's what I [and liori] came up with. It seems to work for me. It also allows an additional couple of aliases that might prove handy:

git config --global alias.branchdiff '!sh -c "git diff `git oldest-ancestor`.."'
git config --global alias.branchlog '!sh -c "git log `git oldest-ancestor`.."'

Happy git-ing!

这篇关于用Git找到一个分支点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆