我如何忽略关于我的本地更改的“git pull”错误将被合并覆盖?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14318234/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:15:46  来源:igfitidea点击:

How do I ignore an error on 'git pull' about my local changes would be overwritten by merge?

gitgit-pullgit-stash

提问by mae

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 wantto 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.

需要明确的是,我只想覆盖特定的更改,而不是所有更改。

采纳答案by mae

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

回答by Daniel Hilgarth

If you want remove all local changes - including files that are untracked by git - from your working copy, simply stash them:

如果要从工作副本中删除所有本地更改(包括 git 未跟踪的文件),只需将它们隐藏起来:

git stash push --include-untracked

If you don't need them anymore, you now can drop that stash:

如果你不再需要它们,你现在可以放下那个藏匿处:

git stash drop

If you don't want to stash changes that you already staged - e.g. with git add- then add the option --keep-index. Note however, that this will still prevent merging if those staged changes collide with the ones from upstream.

如果您不想隐藏已经上演的更改(例如 with)git add,请添加选项--keep-index。但是请注意,如果这些阶段性更改与来自上游的更改发生冲突,这仍然会阻止合并。



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/revertfor the changes you wish to overwrite. Make sure that file is not staged via git reset HEAD path/to/file/to/revert.

  1. 提交您不想覆盖的所有内容,其余部分使用上述方法。

  2. 使用git checkout path/to/file/to/revert您要覆盖更改。确保该文件不是通过git reset HEAD path/to/file/to/revert.

回答by kravits88

This works for me to override all local changes and does not require an identity:

这对我来说可以覆盖所有本地更改并且不需要身份:

git reset --hard
git pull

回答by Aftershock

Here is a solution that throws away staged changes:

这是一个丢弃分阶段更改的解决方案:

git reset file/to/overwrite
git checkout file/to/overwrite

回答by Suneel Kumar

You can either commit your changes before you do the merge, or you stash them:

您可以在合并之前提交更改,也可以将它们隐藏起来:

  1. git stash save
  2. git merge origin/master
  3. git stash pop
  1. git stash save
  2. git merge origin/master
  3. git stash pop

回答by pabloasc

If you want to discard your local changes on one file you can do the following:

如果您想放弃对一个文件的本地更改,您可以执行以下操作:

git checkout -- <file>

Then you could overwrite the file[s] with the latest version just doing:

然后,您可以使用最新版本覆盖文件 [s],只需执行以下操作:

git pull

回答by Nikhil K R

If your repository contains a few files which are removed from master:

如果您的存储库包含一些从 中删除的文件master

  1. git checkout master
  2. git fetch origin
  3. git reset --hard origin/master
  4. git checkout -b newbranch
  1. git checkout master
  2. git fetch origin
  3. git reset --hard origin/master
  4. git checkout -b newbranch

回答by Jeremy Holovacs

Sometimes, none of these work. Annoyingly, due to the LF thing I think, what will work is deletingthe files, thenpulling. Not that I recommend this solution, but if the file doesn't exist, git won't uselessly inform you that your changes (which may not even be changes) will get overridden, and will let you continue.

有时,这些都不起作用。恼人的是,由于我认为 LF 的事情,有效的是删除文件,然后拉。并不是我推荐这个解决方案,但是如果文件不存在,git 不会无用地通知您您的更改(甚至可能不是更改)将被覆盖,并会让您继续。

Use at your own risk.

使用风险自负。

回答by Khemraj

git stash save --keep-indexdid not worked for me.

git stash save --keep-index没有对我来说有效。

below command worked as expected.

下面的命令按预期工作。

git reset --hard
git pull

It override all local changes if you don't need them.

如果您不需要它们,它会覆盖所有本地更改。

回答by BSB

This problem is because you have made changes locally to file/s and the same file/s exists with changes in the Git repository, so before pull/push you will need stash local changes:

这个问题是因为你在本地对 file/s 进行了更改,并且相同的 file/s 存在与 Git 存储库中的更改,所以在 pull/push 之前你需要存储本地更改:

To overwrite local changes of a single file:

要覆盖单个文件的本地更改:

git reset file/to/overwrite
git checkout file/to/overwrite

To overwrite all the local changes (changes in all files):

覆盖所有本地更改(所有文件中的更改):

git stash
git pull
git stash pop

Also this problem may be because of you are on a branch which is not merged with the master branch.

此问题也可能是因为您所在的分支未与主分支合并。