git rm 文件之后; commit——如何从远程分支取回文件?
我正在拉入我的 .emacs 目录,并遇到以下冲突:
CONFLICT (add/add): Merge conflict in elisp/dired-details+.el
Git 状态显示以下内容:
Unmerged paths:
# (use "git add/rm <file>..." as appropriate to mark resolution)
# both added: elisp/dired-details+.el
好的,所以 git 建议使用 git rm 。我想用我正在拉入的文件完全替换本地文件,所以这似乎有点明智......所以我做 git rm elisp/dired-details+.el
和 >git 合并
。我得到:
git merge:致命:您尚未完成合并(MERGE_HEAD 存在)。请在合并之前提交更改。
好的,很好:git commit -a -m "ugh mergeconflict"
; git pull origin master
。
现在一切都合并得很好,除了我缺少 dired-details+.el
,我有点困惑,我想知道以下问题的答案:
- How do I undo
git- rm
并从远程分支获取该文件?.. - 为什么首先会发生冲突?..
add/add
这里发生了什么?.. - 我应该有什么完成而不是
git-rm
'ing 我想要替换的文件?..
I was pulling in my .emacs directory, and hit the following conflict:
CONFLICT (add/add): Merge conflict in elisp/dired-details+.el
Git status showed the following:
Unmerged paths:
# (use "git add/rm <file>..." as appropriate to mark resolution)
# both added: elisp/dired-details+.el
Okay, so git suggested using git rm
. I want to completely replace the local file with the file I'm pulling in, so it seems kind of sort of sensible... So I do git rm elisp/dired-details+.el
and git merge
. I get:
git merge: fatal: You have not concluded your merge (MERGE_HEAD exists). Please, commit your changes before you can merge.
Okay, fine: git commit -a -m "ugh merge conflicts"
; git pull origin master
.
Now everything merges fine, except for I am missing dired-details+.el
, I am kind of confused, and I would like to know the answers to the following:
- How do I undo
git-rm
and get that file from the remote branch?.. - Why was there a conflict in the first place?.. What's going on here with
add/add
?.. - What should I have done instead of
git-rm
'ing the file I wanted to replace?..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,使用 git reset --hard HEAD^ 撤消合并提交。请注意,这将清除上次提交并将工作副本重置为上次提交的状态,因此在执行此操作之前请仔细了解这将做什么。 gitk 可以帮助可视化这一点。
其次, git rm 没有达到您的预期。当您遇到合并冲突时,您会查看处于半合并状态的文件,该文件包含冲突标记,供您解决冲突。此时,系统会要求您将工作副本修复为您希望最终合并提交的状态。通过删除该文件,您告诉 git 您不再希望该文件再次存在,这不是您想要的。
在此阶段您需要做的是将工作副本中的冲突文件更新为您想要保留的版本。通常这是通过检查冲突标记并相应地调整文件来完成的。在您的情况下,如果您确定需要来自要合并的分支的副本,您可以使用 git show :3:elisp/dired-details+.el > elisp/dired-details+.el 来执行此操作。然后 git add elisp/dired-details+.el 告诉 git 您已经解决了该文件中的冲突,然后 git commit 完成。
这里,
git show :3:...
正在从 MERGE_HEAD 请求文件的版本。如果您需要不同的版本,您还可以使用1
作为共同祖先,或使用2
作为合并的“您”一侧。First, undo your merge commit using
git reset --hard HEAD^
. Note that this will wipe out the last commit and reset your working copy to the state of the previous commit, so be careful to understand what this will do before doing it.gitk
can help with visualizing this.Second,
git rm
doesn't do what you expected here. When you have a merge conflict, you're looking at the file in a semi-merged state, with the file containing conflict markers for you to resolve the conflict. At this point, you are being asked to fix the working copy to the state that you want the final merge commit to look like. By removing the file, you told git that you don't want the file to be present at all any more, which is not what you wanted.What you need to do at this stage is update the conflicted file in your working copy to the version you want to keep. Usually this is done by examining the conflict markers and adjusting the file accordingly. In your case, if you know for certain that you want the copy from the branch you're merging from, you can use
git show :3:elisp/dired-details+.el > elisp/dired-details+.el
to do this. Thengit add elisp/dired-details+.el
to tell git that you've resolved the conflict in that file and thengit commit
to finish.Here,
git show :3:...
is requesting the version of the file from MERGE_HEAD. If you need a different version, you can also use1
for the common ancestor or2
for "your" side of the merge.