如何解决git合并冲突,从命令行,使用给定的策略,只有一个文件? [英] How to resolve git merge conflict, from command line, using a given strategy, for just ONE file?
问题描述
假设我做了一个git合并,并在一些文件中发生冲突,比如 package.json
或 pom.xml $ c $
我想
- 自动为命令行解析特定文件的合并冲突 ,而
- 让我自己手动解决所有其他冲突(如果有的话),以及
- 确保我不会从任何分支中丢失有价值的变更。 由于2),我可以使用
git merge -Xours
,因为这可以解决所有冲突。我只想解决一个特定文件中的冲突。
由于3),我不能使用
git checkout --ours package.json
,因为这可能会损失输入分支的一些更改。
用例是:auto-merging脚本,它将以预定义的方式解决琐碎的知名冲突,并在其他文件中存在其他冲突时失败。
我经常遇到的典型情况是有两个在
package.json
中有版本字段的分支,如下所示: $ git diff
diff --cc package.json
索引75c469b,f709434..0000000
--- a / package.json
+++ b / package.json
@@@ -1,6 -1,6 +1,12 @@@
{
name:test,
++<<<<<<< HEAD
+version:2.0.1,
++ |||||||合并共同祖先
++版本:1.0.0,
++ =======
+版本:1.0.2,
++>>>>>>> master
是否可以使用给定的策略来解决JUST ONE文件的冲突?例如:
git-resolve-conflict --ours package.json
解决方案这可以使用
git-merge-file
,尽管它的API是相当低级的。
为了有一个易于使用的命令,您可以使用以下bash脚本(要导入到
.bashrc
,或者您也可以使用npm install -g git-resolve-conflict
)安装它:
git-resolve-conflict(){
STRATEGY =$ 1
FILE_PATH =$ 2
if [-z$ FILE_PATH] || [-z$ STRATEGY];然后
回显用法:git-resolve-conflict< strategy>< file>
echo
echo示例:git-resolve-conflict --ours package.json
echo示例:git-resolve-conflict --union package.json
回声例子:git-resolve-conflict - 他们的package.json
返回
fi
git show:1:$ FILE_PATH> ./tmp.common
git show:2:$ FILE_PATH> ./tmp.ours
git show:3:$ FILE_PATH> ./tmp.theb
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.the $ b $
(注意:我不断更新脚本到我的GitHub repo ,检查回购的最新改进: https://github.com/jakub-g/git-resolve-conflict/blob/base/lib/git-resolve-conflict.sh ) 强>
在问题中描述的特定示例中,用法如下所示:
git-resolve-conflict --oursour package.json
:
https:// github.com/jakub-g/git-resolve-conflict
红利:
如果在su中有多个
package.json
文件bfolders,并且希望为所有处于冲突状态的解决合并问题:用于$(git diff --name-only --diff-filter = U | grep package.json $);做git-resolve-conflict --ours $ ITEM;完成
Let's say I did a git merge and have a conflict in some file, say
package.json
orpom.xml
, and perhaps some other files.I would like to
- automatically resolve merge conflicts for certain files from command line, while
- letting myself resolve manually all the other conflicts, if any, and
- making sure I don't lose valuable changes from any branch.
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.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.
The typical case I often have is having two branches with
version
field inpackage.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
解决方案This can be achieved using
git-merge-file
although its API is rather low-level.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 withnpm 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 }
(Note: I keep updating the script in my GitHub repo, check the repo for the latest improvements: 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
Bonus:
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屋!