这是一个merge解决方案,通过重命名和编辑遇到合并冲突,并使用mergetool识别正确的3个合并源文件来解决它。
演练:
创建文件.txt:
$ git init
Initialized empty Git repository in /tmp/git-rename-and-modify-test/.git/
$ echo "A file." > file.txt
$ git add file.txt
$ git commit -am "file.txt added."
[master (root-commit) 401b10d] file.txt added.
1 file changed, 1 insertion(+)
create mode 100644 file.txt
创建一个稍后将编辑的分支:
$ git branch branch-with-edits
Branch branch-with-edits set up to track local branch master.
在master上创建重命名和编辑:
$ git mv file.txt renamed-and-edited.txt
$ echo "edits on master" >> renamed-and-edited.txt
$ git commit -am "file.txt + edits -> renamed-and-edited.txt."
[master def790f] file.txt + edits -> renamed-and-edited.txt.
2 files changed, 2 insertions(+), 1 deletion(-)
delete mode 100644 file.txt
create mode 100644 renamed-and-edited.txt
切换到分支,并在那里编辑:
$ git checkout branch-with-edits
Switched to branch 'branch-with-edits'
Your branch is behind 'master' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
$
$ echo "edits on branch" >> file.txt
$ git commit -am "file.txt edited on branch."
[branch-with-edits 2c4760e] file.txt edited on branch.
1 file changed, 1 insertion(+)
尝试合并master:
$ git merge master
CONFLICT (modify/delete): file.txt deleted in master and modified in HEAD. Version HEAD of file.txt left in tree.
Automatic merge failed; fix conflicts and then commit the result.
请注意,冲突很难解决 - 文件已重命名。中止,模仿重命名:
$ git merge --abort
$ git mv file.txt renamed-and-edited.txt
$ git commit -am "Preparing for merge; Human noticed renames files were edited."
[branch-with-edits ca506da] Preparing for merge; Human noticed renames files were edited.
1 file changed, 0 insertions(+), 0 deletions(-)
rename file.txt => renamed-and-edited.txt (100%)
再次尝试合并:
$ git merge master
Auto-merging renamed-and-edited.txt
CONFLICT (add/add): Merge conflict in renamed-and-edited.txt
Recorded preimage for 'renamed-and-edited.txt'
Automatic merge failed; fix conflicts and then commit the result.
太棒了!合并会导致“正常”冲突,可以使用 mergetool 解决:
$ git mergetool
Merging:
renamed-and-edited.txt
Normal merge conflict for 'renamed-and-edited.txt':
{local}: created file
{remote}: created file
$ git commit
Recorded resolution for 'renamed-and-edited.txt'.
[branch-with-edits 2264483] Merge branch 'master' into branch-with-edits