如何从命令行使用给定策略仅针对一个文件解决 git 合并冲突? [英] How to resolve git merge conflict, from command line, using a given strategy, for just one file?

查看:38
本文介绍了如何从命令行使用给定策略仅针对一个文件解决 git 合并冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我做了一个 git 合并并且在一些文件中发生了冲突,比如 package.jsonpom.xml,也许还有一些其他文件.

Let's say I did a git merge and have a conflict in some file, say package.json or pom.xml, and perhaps some other files.

我愿意

  1. 从命令行自动解决某些文件的合并冲突,同时
  2. 让自己手动解决所有其他冲突(如果有),以及
  3. 确保我不会丢失任何分支中有价值的更改.

因为 2),我不能使用 git merge -Xours 因为这将解决所有冲突.我只想解决一个特定文件中的冲突.

Because of 2), I can't use git merge -Xours as this would resolve all conflicts. I want to resolve only conflicts in one particular file.

因为 3),我不能使用 git checkout --ours package.json 因为这可能会丢失输入分支的一些更改.

Because of 3), I can't use git checkout --ours package.json as this might lose some changes from the input branch.

用例例如:自动合并脚本,它将以预定义的方式解决琐碎的众所周知的冲突,如果其他文件中存在其他冲突,则失败.

The use case is for instance: auto-merging script that will resolve trivial well-known conflicts in a predefined way, and fail if there are some other conflicts in other files.

我经常遇到的典型情况是在 package.json 中有两个带有 version 字段的分支,如下所示:

The typical case I often have is having two branches with version field in package.json diverged like below:

$ git diff
diff --cc package.json
index 75c469b,f709434..0000000
--- a/package.json
+++ b/package.json
@@@ -1,6 -1,6 +1,12 @@@
  {
    "name": "test",
++<<<<<<< HEAD
 +  "version": "2.0.1",
++||||||| merged common ancestors
++  "version": "1.0.0",
++=======
+   "version": "1.0.2",
++>>>>>>> master

是否可以使用给定的策略解决仅一个文件的冲突?类似的东西:

Is it possible to resolve the conflict for just one file, using given strategy? Something like:

git-resolve-conflict --ours package.json

推荐答案

这可以使用 git-merge-file 虽然它的 API 相当低级.

This can be achieved using git-merge-file although its API is rather low-level.

为了有一个简单易用的命令,你可以使用下面的bash脚本(要导入到.bashrc,也可以用npm install -g git-resolve-conflict):

In order to have an easy-to-use command, you can use the following bash script (to be imported to .bashrc, or you can also install it with npm install -g git-resolve-conflict):

git-resolve-conflict() {
  STRATEGY="$1"
  FILE_PATH="$2"

  if [ -z "$FILE_PATH" ] || [ -z "$STRATEGY" ]; then
    echo "Usage:   git-resolve-conflict <strategy> <file>"
    echo ""
    echo "Example: git-resolve-conflict --ours package.json"
    echo "Example: git-resolve-conflict --union package.json"
    echo "Example: git-resolve-conflict --theirs package.json"
    return
  fi

  git show :1:"$FILE_PATH" > ./tmp.common
  git show :2:"$FILE_PATH" > ./tmp.ours
  git show :3:"$FILE_PATH" > ./tmp.theirs

  git merge-file "$STRATEGY" -p ./tmp.ours ./tmp.common ./tmp.theirs > "$FILE_PATH"
  git add "$FILE_PATH"

  rm ./tmp.common
  rm ./tmp.ours
  rm ./tmp.theirs
}

(注意:我一直在更新我的 GitHub 存储库中的脚本,检查存储库以获得最新改进:https://github.com/jakub-g/git-resolve-conflict/blob/base/lib/git-resolve-conflict.sh)

在问题中描述的特定示例中,用法如下:

In the particular example as described in the question, the usage would be as follows:

git-resolve-conflict --ours package.json

有关分步研究,请参阅:

For a step by step study see:

https://github.com/jakub-g/git-resolve-conflict

奖励:

如果您在子文件夹中有多个 package.json 文件,并且想要解决所有的合并 它们处于冲突状态:

If you have multiple package.json files in subfolders, and want to resolve the merge for all of them which are in conflicted state:

for ITEM in $(git diff --name-only --diff-filter=U | grep package.json$); do git-resolve-conflict --ours $ITEM; done

这篇关于如何从命令行使用给定策略仅针对一个文件解决 git 合并冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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