如何忽略有关本地更改的“ git pull”错误,因为合并会覆盖我的本地更改?

当Git pull提示本地更改将被合并覆盖时,可以通过存储备份、选择性覆盖或取消暂存来处理。若想覆盖特定更改,可使用git stash保存不需要的更改,然后拉取更新,再删除stash。或者,提交不想覆盖的更改,仅用git checkout重置特定文件。
摘要由CSDN通过智能技术生成

本文翻译自: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: 如果只想覆盖本地更改的特定部分,则有两种可能性:

  1. Commit everything you don't want to overwrite and use the method above for the rest. 提交所有您不想覆盖的内容,其余使用上面的方法。

  2. 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 via git 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删除的文件:

  1. git checkout master
  2. git fetch origin
  3. git reset --hard origin/master
  4. git checkout -b newbranch
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值