本文翻译自:How do I ignore an error on 'git pull' about my local changes would be overwritten by merge?
How do I ignore the following error message on Git pull? 如何忽略有关Git pull的以下错误消息?
Your local changes to the following files would be overwritten by merge 您对以下文件的本地更改将被合并覆盖
What if I want to overwrite them? 如果我想覆盖它们怎么办?
I've tried things like git pull -f
, but nothing works. 我已经尝试过git pull -f
类的东西,但是没有任何效果。
To be clear, I only want to overwrite specific changes, not everything. 需要明确的是,我只想覆盖特定的更改,而不是所有内容。
#1楼
参考:https://stackoom.com/question/y4pG/如何忽略有关本地更改的-git-pull-错误-因为合并会覆盖我的本地更改
#2楼
If you want remove all local changes from your working copy, simply stash them: 如果要从工作副本中删除所有本地更改,只需将它们存储起来:
git stash save --keep-index
If you don't need them anymore, you now can drop that stash: 如果您不再需要它们,现在可以删除该存储:
git stash drop
If you want to overwrite only specific parts of your local changes, there are two possibilities: 如果只想覆盖本地更改的特定部分,则有两种可能性:
Commit everything you don't want to overwrite and use the method above for the rest. 提交所有您不想覆盖的内容,其余使用上面的方法。
Use
git checkout path/to/file/to/revert
for the changes you wish to overwrite. 使用git checkout path/to/file/to/revert
来覆盖您想要覆盖的更改。 Make sure that file is not staged viagit reset HEAD path/to/file/to/revert
. 确保未通过git reset HEAD path/to/file/to/revert
。
#3楼
If you want to overwrite specific changes, you need some way of telling it which ones you want to forget. 如果要覆盖特定的更改,则需要某种方式告诉它要忘记的更改。
You could try selectively stashing the changes you want to abandon using git stash --patch
and then dropping that stash with git stash drop
. 你可以尝试选择性地积攒你想用放弃修改git stash --patch
然后滴加与藏匿git stash drop
。 You can then pull in the remote changes and merge them as normal. 然后,您可以拉入远程更改并按常规合并它们。
#4楼
Alright with the help of the other two answers I've come up with a direct solution: 在其他两个答案的帮助下,我提出了一个直接的解决方案:
git checkout HEAD^ file/to/overwrite
git pull
#5楼
Here is a solution that throws away staged changes: 这是一个解决方案,可消除分阶段进行的更改:
git reset file/to/overwrite
git checkout file/to/overwrite
#6楼
如果您的存储库包含一些已从master
删除的文件:
-
git checkout master
-
git fetch origin
-
git reset --hard origin/master
-
git checkout -b newbranch