简体   繁体   English

如何找到Git存储库中存在冲突的所有合并?

[英]How can I find all the merges that had conflicts in a Git repository?

I am working on a research to study merges in open source projects. 我正在研究开源项目中的合并。

I recently asked How can I find all the commits that have more than one parent in a Git repository? 我最近问过如何在Git存储库中找到所有具有多个父级的提交? , and got very good answers. ,并得到了很好的答案。

Now I need to narrow my query to only find the commits that had conflicts. 现在我需要缩小查询范围,只查找有冲突的提交。

With conflicts I mean that the same file was modified in two contributing commits. 对于冲突,我的意思是在两个贡献提交中修改了相同的文件。

Also, it would be very useful if I can find a git or bash (grep, awk?) command that gives me only those commits in which the same line was modified by two contributors. 另外,如果我能找到一个git或bash(grep,awk?)命令,它只会给我两个贡献者修改同一行的提交,这将非常有用。

The idea is to find commits that cannot be auto-resolved. 我们的想法是找到无法自动解决的提交。

So, How can I find all the merges that had conflicts in a git repository? 那么,我怎样才能找到git存储库中存在冲突的所有合并?

Once a merge commit is created, the information how the merge was performed is unfortunately lost. 一旦创建了合并提交,遗憾的是丢失了如何执行合并的信息。 This is important because git merge accepts a number of options, such as strategy and per-strategy options, that affect appearance of conflicts. 这很重要,因为git merge接受了许多影响冲突外观的选项,例如策略和每策略选项。 However, if your goal is to cover the reasonable case of finding merges that would have produced conflicts with the default merge options, it is possible to brute-force it: go over merge commits, recreate their merges, and flag the ones where git reports. 但是,如果您的目标是覆盖查找可能与默认合并选项发生冲突的合并的合理情况,则可以强制执行:转换合并提交,重新创建合并,并标记git报告的合并。

Note: this script checks out many different commits, and runs git reset --hard and even git clean -fdx in every iteration. 注意:此脚本检出许多不同的提交,并在每次迭代中运行git reset --hard甚至git clean -fdx Be sure to only run it in a checkout that contains no important files unknown to git! 一定要只在结帐中运行它,其中不包含git未知的重要文件!

#!/bin/bash

old_branch=$(git symbolic-ref --short HEAD)
for commit in `git rev-list --merges HEAD`
do
  # find the parents of the merge commit
  parents=$(git log -1 --format=%P $commit)
  fst=${parents%% *}
  rest=${parents#* }
  # check out the first parent
  git checkout -q $fst
  # merge with the rest of them
  git merge --no-commit $rest >/dev/null 2>&1
  # if there are any conflicts, print the commit and abort the merge
  if git ls-files --unmerged | grep -q '^'; then
    echo $commit
    git merge --abort
  fi
  # get rid of changes so the next checkout doesnt complain
  git reset -q --hard
  git clean -fdxq
done
git checkout -q $old_branch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有合并冲突的情况下将一长串 git 合并转换为单个 rebase? - How can I convert a long series of git merges into a single rebase without merge conflicts? 如何找出导致冲突的 Git 提交? - How can I find out which Git commits cause conflicts? 如何以编程方式查找 Git 冲突周围的行号? - How can I programmatically find the line numbers around Git conflicts? 在解决所有合并后,如何摆脱Eclipse Git“冲突”图标? - How can I get rid of Eclipse Git “conflict” icons after all merges are resolved? 如何编辑包含数百次合并的 Git 提交并将冲突合并到过去? - How do I edit a Git commit that is hundreds of merges and merge conflicts into the past? 我如何提交git自动合并的所有内容? (暂时忽略冲突) - How do I commit everything that git automatically merges? (temporarily ignoring conflicts) 我如何找到我在git存储库中编写的所有代码行 - How can I find all lines of code I've written in a git repository Git 引发简单合并的冲突 - Git raising conflicts for simple merges 如何在Git存储库中找到所有具有多个父项的提交? - How can I find all the commits that have more than one parent in a Git repository? Git:如果A从B合并,并且B独立于A合并而没有冲突,是否可以使它们成为同一提交? - Git: if A merges from B, and B independently merges from A without conflicts, can they be made the same commit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM