发生git merge
冲突时,将显示如下标记:
<<<<<<< HEAD:file.txt
Hello world
=======
Goodbye
>>>>>>> master:file.txt
想象一下,如果我有file.txt
,并且我手动编辑它
Hello world
自
Goodbye
当我做一个 git diff(使用默认寻呼机(时,我得到以下内容:
diff --git a/file.txt b/file.txt
index 802992c..2b60207 100644
--- a/file.txt
+++ b/file.txt
@@ -1 +1 @@
-Hello world
+Goodbye
有没有办法将此格式转换为上面给出的格式,并为所有更改设置冲突标记?(它是否覆盖文件并不重要。
这是一种方法 - 以下命令将变成如下所示的差异:
diff --git a/file.txt b/file.txt
index 2b34ae8..a27a6bd 100644
--- a/file.txt
+++ b/file.txt
@@ -1,16 +1,16 @@
Line 1
+Addition here
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
-Line 8
+Line 8 Change here, and deletion on line 15
Line 9
Line 10
Line 11
Line 12
Line 13
Line 14
-Line 15
Line 16
放入如下所示的文件中:
Line 1
<<<<<<< HEAD
=======
Addition here
>>>>>>> temp
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
<<<<<<< HEAD
Line 8
=======
Line 8 Change here, and deletion on line 15
>>>>>>> temp
Line 9
Line 10
Line 11
Line 12
Line 13
Line 14
<<<<<<< HEAD
Line 15
=======
>>>>>>> temp
Line 16
命令和说明是:
branch=$(git symbolic-ref --short HEAD) # Get the current branch name
git checkout --orphan temp # Change to a branch with no parents, so to force a merge conflict
git add -A # Add everything there
git commit -m 'temp' # Commit
git checkout "$branch" # Go back to your previous branch
(git merge --allow-unrelated-histories temp || true) # Merge the unrelated branches, which causes a conflict with your changes. This is done in a subshell with the '|| true' so that it doesn't return an error code - I have this in a git alias, so this is necessary for me so the command isn't aborted halfway.
git add -A # Add your conflict markers as additions to the file
GIT_EDITOR=true git merge --continue # Commit. When --continue is used it doesn't accept the flag --no-edit, so to stop git opening an editor for the commit message, use the command 'true' as the editor.
git reset HEAD^ # Get rid of the last commit but keep its changes (the markers). This is needed because there is no way to get out of a conflicting merge commit but keep the conflict markers.
git branch -D temp # Clean up
当然,你可以用它制作一个脚本或别名。请注意,它将分阶段更改和非暂存更改视为相同。
这是一种生成 git 合并冲突文件的方法。这不是这个问题的确切答案,因为它依赖于您有权访问a/file.txt
和b/file.txt
。
但是,如果您有另一个文件,则可以生成一个文件,并使用patch
生成差异。
然后可能是这个git merge-file
命令将产生您需要的输出:
touch /tmp/empty && git merge-file -L a/file.txt -L B -L b/file.txt -p a/file.txt /tmp/empty b/file.txt
<<<<<<< a/file.txt
Hello world
=======
Goodbye
>>>>>>> b/file.txt