找到git commit的直接祖先(父母?) [英] Find the direct ancestors (parents?) of a git commit

查看:115
本文介绍了找到git commit的直接祖先(父母?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始新的回购并添加一些提交:



 #(03/01/17 @ 10:50am)(tim @tim):〜
mkdir test&& cd测试&& git init

在/home/tim/test/.git/中初始化了空的Git存储库

 #(03/01/17 @ 11:17am)(tim @ tim):〜/ test @ master✔
touch自述文件&& git add --all&& git commit -amreadme

[master(root-commit)1b7f299] readme
1个文件已更改,0个插入(+),0个删除( - )
创建模式100644 readme



 #(03/01/17 @ 11:17am)(tim @ tim):〜/ test @master✔
touch howto&& git add --all&& git commit -amhowto

[master fd46c4c] howto
1个文件已更改,0个插入(+),0个删除( - )
创建模式100644 howto

 #(03/01/17 @ 11:19am)(tim @ tim):〜/ test @ master✔
touch la&& git add --all&& git commit -amadd la

[master 4680089] add la
1 file changed,0 insertions(+),0 deletions( - )
create mode 100644 la

 #(03/01/17 @ 11:20am)(tim @ tim):〜/ test @ master✔
ls
howto la readme

#(03 / 01/17 @ 11:20am)(tim @ tim):〜/ test @ master✔
echohello>>自述文件&&回声你好>> howto

#(03/01/17 @ 11:20am)(tim @ tim):〜/ test @master✗✗✗
git commit -amedit readme and howto
[master 8969440]编辑readme和howto
2个文件已更改,2个插入(+)



现在我们有以下提交:



 提交8969440d52e578113f609d948e6ffd06cec96fa9 
作者:Tim Richardson< tim @ x .COM>
日期:Wed Mar 1 11:20:54 2017 +0000

编辑自述文件和howto

提交4680089c7c1a0ead84f6b2973fd6d9e1356fd5c0
作者:Tim Richardson& tim @ x.com>
日期:Wed Mar 1 11:20:06 2017 +0000

add la

commit fd46c4cf593752ec8163d8db21042c8dd336f529
作者:Tim Richardson< tim @ x .COM>
日期:Wed Mar 1 11:18:09 2017 +0000

howto

提交1b7f299c5ad4fc50ce4913ab4cdbbdc761db0487
作者:Tim Richardson< tim @ x。 COM>
日期:Wed Mar 1 11:17:50 2017 +0000

自述



允许签出一个名为test的新分支并将其重置为初始提交:



 #(03/01 / 17 @ 11:26am)(tim @ tim):〜/ test @ master✔
git checkout -b测试
转换到一个新的分支'测试'

#(03 / 01 @ 17 @ 11:27am)(tim @ tim):〜/ test @test✔
git reset --hard 1b7f299c5ad4fc50ce4913ab4cdbbdc761db0487

HEAD现在在1b7f299自述文件



如果我樱桃选择提交8969440,它会失败,因为它依赖于fd46c4c和1b7f29而不是4680089:



 #(03/01/17 @ 11:27am)(tim @ tim):〜/ test @test✔
git cherry-pick 8969440
错误:无法应用8969440 ...编辑自述文件和howto
提示:解决冲突之后,标记更正的路径
提示:使用'git add<路径>'或'git rm< paths>'
hint:并用'git commit'提交结果



但是我可以从1b7f299c中挑选4680089c7而没有任何冲突,即使它不是git日志中的第一级后裔:



 #(03/01/17 @ 11:28am)(tim @ tim):〜/ test @test✗✗✗
git reset - 硬

HEAD现在在1b7f299自述

#(03/01/17 @ 12:10pm)(tim @ tim):〜/ test @test✔
git cherry-pick 4680089c7

[test de3878f] add la
日期:Wed Mar 1 11:20:06 2017 +0000
1个文件已更改,0个插入(+) ,0删除( - )
创建模式100644 la



看起来像这样的补丁:



  + --------- + 
| |
| 1b7f299 + -------- +
| | |
+ --------- + | + ---------- +
+ -----> + |
| | 8969440 |
+ --------- + | | |
| | | + ---------- +
| fd46c4c + -------- +
| |
+ --------- +


+ --------- +
| |
| 4680089 |
| |
+ --------- +



我的问题给出了一个更大的回购如何计算补丁方面的依赖关系图?


解决方案

标题中的问题的答案,要找到提交的父级:

  git log --pretty =%p  

%P / p>

但是这不是你所期待的。



假设我们已经提交了A和B,而B依赖于A.可能A是B的父亲。也有可能B是A之前的许多提交。



尝试樱桃选择B到当前分支(例如 master ),并发生冲突。运行 git status 查看哪些文件存在冲突。假设他们是 foo.c bar.c



运行 git log master..B - foo.c bar.c 并获得一组触发 foo.c bar.c



运行 git log B. .master - foo.c bar.c 并获得另一组提交。



通过提交消息和补丁来比较这两组依赖项在第一个集合中提交,不包括那些在第二个集合中具有等价提交的集合。樱桃挑选那些你真正需要的东西。

真实情况可能会更复杂。 A和B可能与相同错误的提交有关。如果你不挑选A或B,结果没有冲突,但是如果你不挑选它们,这个错误是无法修复的。



如果你制作一个良好的工作流程,例如将所有相关的提交压缩成一个,并在开始时跟踪提交的每个错误/功能,这样可以节省大量时间和精力。查找记录并逐一挑选提交或压缩提交比搜索缺失的依赖提交要容易得多。可能会有冲突,但您可以肯定这不是因为您错过了一些依赖提交。


Start a new repo and add some commits:

#( 03/01/17@10:50am )( tim@tim ):~
   mkdir test && cd test && git init

Initialised empty Git repository in /home/tim/test/.git/

.

#( 03/01/17@11:17am )( tim@tim ):~/test@master✔
   touch readme && git add --all && git commit -am "readme"   

[master (root-commit) 1b7f299] readme
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 readme

.

#( 03/01/17@11:17am )( tim@tim ):~/test@master✔
   touch howto && git add --all && git commit -am "howto" 

[master fd46c4c] howto
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 howto

.

#( 03/01/17@11:19am )( tim@tim ):~/test@master✔
   touch la && git add --all && git commit -am "add la"

[master 4680089] add la
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 la

.

#( 03/01/17@11:20am )( tim@tim ):~/test@master✔
   ls
howto  la  readme

#( 03/01/17@11:20am )( tim@tim ):~/test@master✔
   echo "hello" >> readme && echo "hello" >> howto

#( 03/01/17@11:20am )( tim@tim ):~/test@master✗✗✗
   git commit -am "edit readme and howto"
[master 8969440] edit readme and howto
 2 files changed, 2 insertions(+)

So now we have the following commits :

commit 8969440d52e578113f609d948e6ffd06cec96fa9
Author: Tim Richardson <tim@x.com>
Date:   Wed Mar 1 11:20:54 2017 +0000

    edit readme and howto

commit 4680089c7c1a0ead84f6b2973fd6d9e1356fd5c0
Author: Tim Richardson <tim@x.com>
Date:   Wed Mar 1 11:20:06 2017 +0000

    add la

commit fd46c4cf593752ec8163d8db21042c8dd336f529
Author: Tim Richardson <tim@x.com>
Date:   Wed Mar 1 11:18:09 2017 +0000

    howto

commit 1b7f299c5ad4fc50ce4913ab4cdbbdc761db0487
Author: Tim Richardson <tim@x.com>
Date:   Wed Mar 1 11:17:50 2017 +0000

    readme

lets checkout a new branch called test and reset it to the initial commit:

#( 03/01/17@11:26am )( tim@tim ):~/test@master✔
   git checkout -b test
Switched to a new branch 'test'

#( 03/01/17@11:27am )( tim@tim ):~/test@test✔
   git reset --hard 1b7f299c5ad4fc50ce4913ab4cdbbdc761db0487

HEAD is now at 1b7f299 readme

If i cherry pick commit 8969440 it fails because it relies on fd46c4c and 1b7f29 but not 4680089:

#( 03/01/17@11:27am )( tim@tim ):~/test@test✔
   git cherry-pick 8969440
error: could not apply 8969440... edit readme and howto
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

however I can cherry pick 4680089c7 from 1b7f299c without any conflicts even though it is not a first degree descendant on the git log:

#( 03/01/17@11:28am )( tim@tim ):~/test@test✗✗✗
   git reset --hard 

HEAD is now at 1b7f299 readme

#( 03/01/17@12:10pm )( tim@tim ):~/test@test✔
   git cherry-pick 4680089c7

[test de3878f] add la
 Date: Wed Mar 1 11:20:06 2017 +0000
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 la

So there is a dependency graph between the patches which looks like this:

+---------+
|         |
| 1b7f299 +--------+
|         |        |
+---------+        |      +----------+
                   +----->+          |
                   |      | 8969440  |
+---------+        |      |          |
|         |        |      +----------+
| fd46c4c +--------+
|         |
+---------+


+---------+
|         |
| 4680089 |
|         |
+---------+

My question is given a larger repo how can I calculate the dependency graph in terms of patches? How can I use git to tell which commits rely on others?

解决方案

The answer to the question in the title, to find the parent(s) of a commit:

git log --pretty=%p <commit>

%P for the full sha1.

But this is not what you are expecting.

Let's say we have commit A and B in your case, and B relies on A. It's possible that A is the parent of B. Also possible B is many commits ahead of A.

Try to cherry pick B to the current branch (master for example), and the conflict occurs. Run git status to see which files have conflicts. Let's say they are foo.c and bar.c.

Run git log master..B -- foo.c bar.c and get a set of commits which touch either foo.c or bar.c.

Run git log B..master -- foo.c bar.c and get another set of commits.

Compare these two sets by commit messages and patches to find the dependency commits in the first set, excluding those that have equivalent commits in the second. Cherry-pick those you really need one by one.

The real case could be more complex. A and B could be related commits of the same bug. To cherry-pick only A or only B results in no conflict, but the bug can not be fixed if you don't cherry-pick both.

If you make a good workflow, for example squashing all the related commits into one, and track each bug/feature with the commits in the very beginning, you could save a lot of time and efforts. It's much easier to find the record and cherry-pick the commits one by one or a squashed commit than to search for the missing dependency commits. There might be conflicts but you can be sure it's not because you miss some of the dependency commits.

这篇关于找到git commit的直接祖先(父母?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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