Git冲突(重命名/重命名)

时间:2013-02-06 15:28:22

标签: git git-merge merge-conflict-resolution

合并分支后,我收到conflict (rename/rename)一堆文件,其中file~HEADfile~my_test_branch已创建。如何解决这些问题?

由于

1 个答案:

答案 0 :(得分:18)

鉴于以下测试设置:

git init resolving-rename-conflicts
cd resolving-rename-conflicts
echo "this file we will rename" > will-be-renamed.txt
git add -A
git commit -m "initial commit"
git checkout -b branch1
git rename will-be-renamed.txt new-name-1.txt
git commit -a -m "renamed a file on branch1"
git checkout -b branch2 master
git rename will-be-renamed.txt new-name-2.txt
git commit -a -m "renamed a file on branch2"
git checkout master

然后合并branch1和branch2

git merge --no-ff branch1
git merge --no-ff branch2

收率:

CONFLICT (rename/rename): Rename "will-be-renamed.txt"->"new-name-1.txt" in branch "HEAD" rename "will-be-renamed.txt"->"new-name-2.txt" in "branch2"
Automatic merge failed; fix conflicts and then commit the result.

git status

On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
  (use "git add/rm <file>..." as appropriate to mark resolution)

    added by us:        new-name-1.txt
    added by them:      new-name-2.txt
    both deleted:       will-be-renamed.txt

no changes added to commit (use "git add" and/or "git commit -a")

如果您想保留一个文件,请说new-name-2.txt

git add new-name-2.txt
git rm new-name-1.txt will-be-renamed.txt
git commit

当然,在选择一个文件或另一个文件时,您可能会对按名称引用此文件的文件进行其他更改。此外,如果对文件进行了其他非重命名更改,在分支上进行预翻译或重命名,则需要手动区分和合并这些更改以将其保留在您保留的文件中。

如果您想保留两个文件:

git add new-name-1.txt new-name-2.txt
git rm will-be-renamed.txt
git commit