git - 当本地被删除但文件存在于远程时合并冲突
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4319486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git - merge conflict when local is deleted but file exists in remote
提问by binarycreations
I am very new to git and wondered how I should go about a merge where in the local repo I have deleted several files on the master branch but these files exist within the remote master branch.
我对 git 很陌生,想知道我应该如何进行合并,我在本地仓库中删除了 master 分支上的几个文件,但这些文件存在于远程 master 分支中。
After doing git-merge it shows the conflicts that have occured.
执行 git-merge 后,它会显示发生的冲突。
Using git gui it shows that the local file is deleted, while the remote branch file has contents.
使用 git gui 显示本地文件被删除,而远程分支文件有内容。
How do you stop these files from being conflicted? Is there a simple way using git gui?
你如何阻止这些文件发生冲突?有没有使用 git gui 的简单方法?
Many thanks
非常感谢
回答by Cascabel
You should resolve the conflicts as you see fit. If the file really is supposed to be removed, and you will be publishing that change to origin, remove it again:
您应该按照您认为合适的方式解决冲突。如果确实应该删除该文件,并且您要将更改发布到原点,请再次删除它:
git rm path/to/file
If the file should in fact be tracked still, add it (the version in the work tree will be the version from origin):
如果实际上应该仍然跟踪文件,请添加它(工作树中的版本将是原始版本):
git add path/to/file
After doing either of those to resolve the conflict, commit the merge.
在执行其中任何一项以解决冲突后,提交合并。
回答by Joseph Ravenwolfe
As an added tip in addition to the accepted answer, in a "deleted by us", if you would like to see the changes that were made to the deleted file so that you may apply those changes elsewhere you can use:
作为除了接受的答案之外的附加提示,在“由我们删除”中,如果您想查看对已删除文件所做的更改,以便您可以在其他地方应用这些更改,您可以使用:
git diff ...origin/master -- path/to/file
If it is a "deleted by them"scenario, and you would like to see the changes so you can apply them elsewhere, you can use:
如果它是“被他们删除”的情况,并且您希望看到更改以便可以在其他地方应用它们,则可以使用:
git diff origin/master... -- path/to/file
回答by Joel Freeman
In Git GUI, you select the conflicted file and then right-click on the main text area where the conflicted text is shown.
在 Git GUI 中,您选择冲突的文件,然后右键单击显示冲突文本的主文本区域。
In the context menu that appears, you can choose to go with "Remote" or go with "Local". So if a file is remotely deleted, you can choose "Remote" to propagate the delete locally, and vice versa.
在出现的上下文菜单中,您可以选择“远程”或“本地”。因此,如果文件被远程删除,您可以选择“远程”在本地传播删除,反之亦然。
Took me a month to figure it out...it would be nice if Git GUI actually had documentation...
我花了一个月才弄明白……如果 Git GUI 真的有文档就好了……
回答by Dorian Marchal
The best-rated answer focuses on the way to resolve the conflict.
评分最高的答案侧重于解决冲突的方式。
Before that, you probably want to know what the remote changed in the locally removed files.
在此之前,您可能想知道远程在本地删除的文件中更改了什么。
To do that, you can see the changes with:
为此,您可以通过以下方式查看更改:
git diff --base
From https://git-scm.com/docs/git-diff#Documentation/git-diff.txt--1--base
来自https://git-scm.com/docs/git-diff#Documentation/git-diff.txt--1--base
Compare the working tree with the "base" version [...]. The index contains these stages only for unmerged entries i.e. while resolving conflicts.
将工作树与“基本”版本进行比较 [...]。该索引仅包含未合并条目的这些阶段,即在解决冲突时。
回答by borjab
In EGit I also found problems. My solution was:
在EGit中我也发现了问题。我的解决方案是:
- Used the Git Staging view.
- Double clicked on each files shown on unstaged changes to open comparator
- Click on the "Copy all from left to right" icon
- Save file (it will disappear from the unstaged list)
- 使用 Git 暂存视图。
- 双击未暂存更改中显示的每个文件以打开比较器
- 单击“从左到右全部复制”图标
- 保存文件(它将从 unstaged 列表中消失)