列出影响文件的合并提交 [英] List merge commits affecting a file

查看:98
本文介绍了列出影响文件的合并提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到影响或涉及给定文件的所有合并提交。

对于背景,合并时有人错误地解决了冲突,并且团队没有注意到几天。此时,许多其他不相关的合并已经发生(我们中的一些人一直宁愿不使用rebase,或者事情会更简单)。我需要找到错误的合并提交,因此可以检查以确定还有哪些可能已被还原(当然,以及确定和惩罚有罪)。

该场景如下:

  $ echo第一个> a.txt&& git add a.txt&& git commit -m'第一次提交'
$ git branch branch1
$ echoSecond:main-branch>> a.txt&& git commit -a -m'提交到master'
$ git tag a1
$ echoThird:main>> a.txt&& git commit -a -m'其他提交到master'
$ git checkout branch1
$ echo第二:在分支1上>> a.txt&& git commit -a -m'提交分支'
$ git tag b1

.. 。现在,master和branch1中的a.txt存在冲突。

  $ git checkout master 
$ git合并branch1
自动合并a.txt
CONFLICT(内容):合并a.txt中的冲突
自动合并失败;修复冲突,然后提交结果。
$ cat a.txt
First
<< <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< HEAD:a.txt
第二:主分支
第三:主
=======
第二:在分支1
>>> >>>> branch1:a.txt
$ vi a.txt
#...
$ cat a.txt
第一个
第二个:在分支1上
$ git添加a.txt
$ git commit -m'合并提交'

... In换句话说,决议是采取他们的。图表现在如下所示:

  $ git log --graph --branches --format ='%h%s% d'
* fefb623合并提交(refs / heads / master)
| \
| * 93e065e在分支上提交(refs / tags / b1,refs / heads / branch1)
* | cf4d12b在master
* |上的其他提交6fade67提交给master(refs / tags / a1)
| /
* 07545ea第一次提交

此时,a.txt的错误版本位于master上。 a1中的版本是我们想要的,但是b1版本已经提交。到目前为止,我已经尝试过:

  $ git log --decorate --oneline a.txt 
93e065e(refs / tags / b1,refs / heads / branch1)提交分支
07545ea第一次提交

好的,所以a1和merge commit都不会出现。

  $ git log --decorate --oneline --follow a.txt 
...

这会向我显示两个分支上的所有内容,但仍省略了合并提交

  $ git log --oneline --graph --decorate a1..master 
...

这当然是不在a1中的所有东西,它在这个玩具的例子中起作用,但在我的真实案例给了我最近完成的所有合并(并没有指出哪一个触及了a.txt)。

我可以合理化a1从文件历史中消失,因为合并选择忽略这种变化(至少在我认为git关心的意义上)。但是,如何找到影响a.txt的所有合并提交?这甚至可能没有手动检查所有candiate合并提交?

解决方案

你的情况的问题是合并提交是空的,因为你只从一个分支中获取东西,所以git通过删除那些空提交来简化历史记录。

所以要在日志中显示你的合并提交,你需要通过告诉 - simplify-merges 来告诉git改变历史简化。如果你想了解为什么你必须使用这个,请继续在 git log 手册页中,但我已经不在了:)无论如何,在这种情况下,合并提交正确地显示与a.txt过滤,所以这就是你想要的。



最后,你想显示差异两次,与每个父母, -m



所以这给了

  git log -U -m  - 简化合并 -  merges  -  a.txt 

包含输出

  commit a7340d6e91deedff6f52c8ec8da932245f73e5f6(from d577e6c4dcbff5485ded666c89f38 
合并:d577e6c fe3c4d2

合并提交

diff - git a / a.txt b / a.txt
index 319137b..1aa6dc4 100644
--- a / a.txt
+++ b / a.txt
@ @ -1,3 +1,2 @@
第一个
- 第二个:主要分支
- 第三个:主要
+第二个:在分支1上


I want to find all the merge commits which affect or involve a given file.

For background, someone mis-resolved a conflict when merging, and it wasn't noticed by the team for a few days. At that point, a lot of other unrelated merges had been committed (some of us have been preferring to not use rebase, or things would be simpler). I need to locate the "bad" merge commit, so it can be checked to identify what else might have been reverted (and, of course, to identify and punish the guilty).

The scenario is like this:

$ echo First > a.txt && git add a.txt && git commit -m 'First commit'
$ git branch branch1
$ echo "Second: main-branch" >> a.txt && git commit -a -m 'Commit on master'
$ git tag a1
$ echo "Third: main" >> a.txt && git commit -a -m 'Other commit on master'
$ git checkout branch1
$ echo "Second: on branch1" >> a.txt && git commit -a -m 'Commit on branch'
$ git tag b1

...So now there are conflicting changes to a.txt in master and branch1.

$ git checkout master
$ git merge branch1 
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Automatic merge failed; fix conflicts and then commit the result.
$ cat a.txt 
First
<<<<<<< HEAD:a.txt
Second: main-branch
Third: main
=======
Second: on branch1
>>>>>>> branch1:a.txt
$ vi a.txt
# ...
$ cat a.txt 
First
Second: on branch1
$ git add a.txt
$ git commit -m 'Merge commit'

...In other words, the resolution is "take theirs". The graph now looks like this:

$ git log --graph --branches --format='%h %s %d' 
*   fefb623 Merge commit  (refs/heads/master)
|\  
| * 93e065e Commit on branch  (refs/tags/b1, refs/heads/branch1)
* | cf4d12b Other commit on master 
* | 6fade67 Commit on master  (refs/tags/a1)
|/  
* 07545ea First commit 

At this point wrong version of a.txt is on master. The version in a1 is what we want, but the b1 version was committed. So far I've tried:

$ git log --decorate --oneline a.txt
93e065e (refs/tags/b1, refs/heads/branch1) Commit on branch
07545ea First commit

Ok, so neither a1 nor the merge commit appear.

$ git log --decorate --oneline --follow a.txt
...

This shows me everything on both branches, but still omits the merge commits.

$ git log --oneline --graph --decorate a1..master
...

This is of course "everything in master not in a1", which works in this toy example but in my real case gives me all the merges done recently (and no indication of which one touched a.txt).

I can rationalize the disappearance of a1 from the file's history, since the merge chose to omit that change (at least in the sense that git cares about, I think). But how do I locate all the merge commits affecting a.txt? Is this even possible without manually inspecting all the candiate merge commits?

解决方案

The problem in your case is that the merge commits are empty, because you take stuff only from one branch, so git simplifies history by removing those "empty" commits.

So to have your merge commits shown in the log, you need to tell git to change history simplification, by telling --simplify-merges. If you want to understand why you have to use this, go ahead in the git log man page, but I'm out of it :) Anyway in this case the merge commit is correctly shown with a.txt filtering, so that's what you want.

Finally, you want to show diff two times, with each parent, with -m.

So this gives

git log -U -m --simplify-merges --merges -- a.txt

with output

commit a7340d6e91deedff6f52c8ec8da932245f73e5f6 (from d577e6c4dcbff5485ded666c89f38
Merge: d577e6c fe3c4d2

    merge commit

diff --git a/a.txt b/a.txt
index 319137b..1aa6dc4 100644
--- a/a.txt
+++ b/a.txt
@@ -1,3 +1,2 @@
 First
-Second: main-branch
-Third: main
+Second: on branch1

这篇关于列出影响文件的合并提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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