如何忽略Git pull上的以下错误消息?

您对以下文件的本地更改将被合并覆盖

如果我想覆盖它们怎么办?

我试过像git pull-f这样的方法,但没有任何效果。

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


当前回答

我忽略了我的repo中的一个文件,当我执行git pull upstream master时,我得到了以下错误:

错误:您对以下文件的本地更改将被合并覆盖:myfile.js请在合并之前提交更改或将其隐藏。正在中止

为了解决这个问题,我采取了以下措施

git update-index --no-assume-unchanged myfile.js

然后我做了git状态并得到了这条消息

在分支主机上您的分支落后于“origin/master”4次提交,并且可以被快速转发。(使用“git pull”更新本地分支)未提交的更改:(使用“git add…”更新将提交什么)(使用“gitcheckout--…”放弃工作目录中的更改)修改:myfile.js未向提交添加任何更改(使用“gitadd”和/或“gitcommit-a”)

然后我执行git checkout myfile.js,然后执行git pull upstream master。这次git拉动操作成功。

其他回答

如果使用git lfs并且文件指针被实际文件覆盖,也会出现此消息。

然后使用:

git stash
git lfs migrate import
git pull

我案子的全部结果

λ git stash
Saved working directory and index state WIP on master: 5d4ad47 Merge branch 'feature/...' into 'master'
Encountered 1 file(s) that should have been pointers, but weren't:
        public/apple-touch-icon.png

λ git pull
Updating 5a4ad44..b25f79d
error: Your local changes to the following files would be overwritten by merge:
        public/apple-touch-icon.png
Please commit your changes or stash them before you merge.
Aborting

λ git lfs migrate import
migrate: Fetching remote refs: ..., done
migrate: Sorting commits: ..., done
migrate: Rewriting commits: 100% (0/0), done
migrate: Updating refs: ..., done
migrate: checkout: ..., done


λ git pull
Updating 5d4ad47..a25c79a
Fast-forward
 public/apple-touch-icon.png | Bin 2092 -> 130 bytes
 public/favicon.ico          | Bin 6518 -> 1150 bytes
 2 files changed, 0 insertions(+), 0 deletions(-)

看见https://github.com/git-lfs/git-lfs/issues/2839

如果您想从工作副本中删除所有本地更改(包括未被git跟踪的文件),只需将其隐藏:

git stash push --include-untracked

如果你不再需要它们,你现在可以放下这些东西:

git stash drop

如果您不想隐藏已经暂存的更改(例如使用gitadd),那么添加选项--keep-index。然而,请注意,如果这些阶段性的更改与上游的更改发生冲突,这仍然会阻止合并。


如果您只想覆盖本地更改的特定部分,有两种可能:

提交您不想覆盖的所有内容,其余部分使用上面的方法。对于要覆盖的更改,请使用git checkout path/to/file/to/revert。确保文件未通过git reset HEAD path/to/file/to/revert暂存。

您可以将其用于覆盖文件

git checkout file_to_overwrite

如果你想覆盖特定的更改,你需要一些方法告诉它你想忘记哪些更改。

您可以尝试使用gitstash-patch选择性地隐藏您想要放弃的更改,然后使用gitstash drop删除该更改。然后,您可以按正常方式拉入远程更改并合并它们。

实例

[root@localhost www]# git pull
Username for 'http://159.65.151.11': amol
Password for 'http://amol@159.65.151.11':
remote: Counting objects: 34, done.
remote: Compressing objects: 100% (34/34), done.
remote: Total 34 (delta 13), reused 0 (delta 0)
Unpacking objects: 100% (34/34), done.
From http://159.65.151.11/OpenCC
   f793c8e..b8fe60c  v1.18.0_24Feb2022 -> origin/_v1.18.0_24Feb2022
error: Your local changes to the following files would be overwritten by merge:
        cc/routes/web.php
Please, commit your changes or stash them before you can merge.
Aborting

解决方案(忽略本地文件中已提交的更改)

git checkout HEAD^ cc/routes/web.php
git pull