简体   繁体   English

一个分支修改了一个文件,另一个分支移动了文件(但是git认为已经删除,新建了一个文件)如何解决合并冲突

[英]How to resolve merge conflict when one branch has modified a file and the other has moved it (but git thinks it has been deleted and a new file made)

I am trying to pull master-dev into my branch.我正在尝试将 master-dev 拉入我的分支。 My branch has moved a file (git sees this as the file being deleted and a new file made), but the same file has been modified in master-dev.我的分支移动了一个文件(git 将其视为正在删除的文件并创建了一个新文件),但同一文件已在 master-dev 中进行了修改。

I have moved it from 'a/myfile.py' to 'b/myfile.py'.我已将其从“a/myfile.py”移至“b/myfile.py”。

I want to get the changes from master-dev, but have the file in the new location.我想从 master-dev 获取更改,但将文件放在新位置。

There may have also been changes made after moving the file in my branch, if this is the case, I would like to see both sets of changes and then pick the best change.在我的分支中移动文件后也可能进行了更改,如果是这种情况,我希望看到两组更改,然后选择最佳更改。

I have checked out my branch, and then run git pull origin master-dev .我检查了我的分支,然后运行git pull origin master-dev It reported merge conflicts, so I ran git mergetool , which I have configured to use MELD.它报告了合并冲突,所以我运行了git mergetool ,我已将其配置为使用 MELD。

I have resolved a few other conflicts, but it has now reached the file in question, and git mergetool has prompted:我已经解决了其他几个冲突,但是现在已经到了有问题的文件, git mergetool提示:

Deleted merge conflict for 'a/myfile.py':
  {local}: deleted
  {remote}: modified file
Use (m)odified or (d)eleted file, or (a)bort?

But unlike the other conflicts it has not opened meld, so I cannot see what the changes are in {remote} ie master-dev .但与其他冲突不同,它没有打开 meld,所以我看不到{remote}master-dev有什么变化。 I also do not know if the file was changes as well as moved.我也不知道文件是否被更改和移动过。

Can I tell git to delete this file, but then compare the changes between the {remote} and the new file?我可以告诉 git 删除这个文件,然后比较{remote}和新文件之间的变化吗? Or is there another solution to this problem?或者这个问题有另一种解决方案吗?

Is there a manual solution to this?是否有手动解决方案? What steps should I take to ensure that I do this cleanly without loosing any potential good changes from either branch?我应该采取什么步骤来确保在不丢失任何一个分支的任何潜在良好更改的情况下干净地执行此操作?

Thanks.谢谢。

You can use the standard patch utility to directly apply the diff to the renamed file:您可以使用标准patch实用程序直接将差异应用到重命名的文件:

# 3 dots :
git diff my_branch...master-dev -- a/myfile.py |\
    patch b/myfile.py

If that works, "solving" the conflict consists in deleting the old file ( (d) option)如果可行,“解决”冲突包括删除旧文件( (d)选项)


About the 3 dots notation in git diff , quoting git help diff :关于git diff中的 3 点符号,引用git help diff

git diff [<options>] <commit>...<commit> [--] [<path>…]

This form is to view the changes on the branch containing and up to the second <commit> , starting at a common ancestor of both <commit> .此表单用于查看包含和直到第二个<commit>的分支上的更改,从两个<commit>的共同祖先开始。 git diff A...B is equivalent to git diff $(git merge-base AB) B . git diff A...B相当于git diff $(git merge-base AB) B

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决在第一个分支中删除并在第二个分支中重命名的文件的合并冲突? - How to resolve a merge conflict on a file that has been deleted in 1st branch and renamed in 2nd branch? 在两个分支中移动文件时,Git合并失败 - Git merge fails when file has been moved in both branches 合并冲突,但 git 存储库已被删除 - Merge conflict but the git repository has been deleted Git 在重新创建符号链接后认为符号链接目录中的文件已被删除,我该如何解决? - Git thinks a file within a symlinked directory has been deleted after recreating the symlink, how can I fix it? git diff认为文件的最后一行在文件结束后被移动了? - git diff thinks the last line of the file has been moved after the end of file? git 显然一直说文件已被修改,但实际上它没有 - git apparently keeps saying that a file has been modified when it has not 当分支中的已删除文件与具有该文件的主文件合并时,为什么Git不显示合并冲突 - Why Git is not showing merge conflicts when a deleted file in branch is merged with master that has that file git 中的文件索引已在 Git 中删除 - index of a file in git has been deleted in Git Git 无法识别我的文件已被修改 - Git is not recognising that my file has been modified git如何检测文件是否已被修改? - How does git detect that a file has been modified?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM