Git合并重命名冲突

4
我已经从现有分支创建了一个新的分支:
git checkout master
git checkout -b test

然后在新分支中,我将一个文件改名:

git mv foo.txt fooOld.txt
git mv fooNew.txt foo.txt
git commit -am "Rename file"

同时,其他人在主分支上编辑了fooNew.txt并推送了更改:

git co master
echo "Some changes" >> fooNew.txt
git commit -am "Do some important changes"
git push origin master

现在当我尝试从主分支拉取更改时,出现错误:

CONFLICT (modify/delete): fooNew.txt deleted in HEAD and modified in master.

如何合并这两个分支,以便最终得到一个包含主分支上对fooNew.txt所做更改的foo.txt文件?


1
你在执行 git pull 命令之前,是否已经将 test 合并到了 master 分支中?如果没有,请先将 master 合并到 test 分支中,解决冲突后再合并回 master 分支。如果已经合并过了,则需要先将 master 分支还原到之前的提交状态。 - JScoobyCed
2
你试过变基吗?git pull --rebase origin master - iltempo
谢谢,我已经尝试过 git pull --rebase origin master 并先将测试分支合并到主分支,但仍然遇到了相同的问题,即冲突只能通过保存修改后的 foo.txt 文件或删除它来解决。我还尝试过将主分支与测试分支进行 rebase,但没有成功... - tempestSA
我放弃了并进行了合并,保存了修改后的文件,然后手动合并以拉取fooNew.txt和foo.txt之间的所有更改(完成后删除fooNew.txt)。但是我非常想知道是否有不涉及手动合并的解决方案。 - tempestSA
有趣的阅读:https://dev59.com/d2gu5IYBdhLWcg3wbGil - VonC
1个回答

0

使用您的方法,我没有遇到任何问题。

$ git checkout -b test
Switched to a new branch 'test'

$ git mv foo.txt fooOld.txt

$ git mv fooNew.txt foo.txt

$ git commit -am "Rename file"
[test bf82f6d] Rename file
 1 files changed, 0 insertions(+), 0 deletions(-)
 rename fooNew.txt => fooOld.txt (100%)

$ git checkout master
Switched to branch 'master'

$ echo "Some changes" >> fooNew.txt

$ git commit -am "Do some important changes"
[master 653c0df] Do some important changes
 1 files changed, 1 insertions(+), 0 deletions(-)

$ git checkout test
Switched to branch 'test'

$ git merge master
Auto-merging fooOld.txt
Merge made by the 'recursive' strategy.
 fooOld.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接