Вручную создайте маркеры, такие как git merge с конфликтом из diff - PullRequest
0 голосов
/ 02 октября 2018

Когда возникает конфликт 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

Есть ли способ конвертировать этот формат в формат, указанный выше смаркеры конфликта на месте для всех изменений?(Не имеет значения, перезаписывает ли он файл.)

1 Ответ

0 голосов
/ 03 октября 2018

Вот один способ - следующие команды превратят diff, который выглядит следующим образом:

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 b -D temp                                          # Clean up

Конечно, из этого можно сделать скрипт или псевдоним.Обратите внимание, что он обрабатывает поэтапные и нематериальные изменения одинаково.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...