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

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

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

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

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


当前回答

好了,在其他两个答案的帮助下,我提出了一个直接的解决方案:

git checkout HEAD^ file/to/overwrite
git pull

其他回答

实例

[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

出现错误“您对以下文件的本地更改将被合并覆盖”,因为您在本地回购中有一些尚未提交的更改,因此在从远程回购中提取之前,只需提交本地回购中的更改。

假设您的远程回购有一些分支xyz并且您希望将远程repo xyz分支合并到(复制到)本地repo xyx分支,

{
git checkout xyz                  //check out to the respective branch in local repo
git commit -m "commiting message" //commit changes if any, in local repo branch xyz
git pull                          //it pulls remote xyz branch into local xyz branch
}

如果您想从工作副本中删除所有本地更改(包括未被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暂存。

只需提交本地更改并合并提交-a

我是git新手,不确定我的解决方案是否是个好主意。

我测试了所有答案,但没有一个对我有用!

但我找到了另一个解决方案:

1. Backup both of local and repository versions of the file.
2. Delete the file from repository.
3. git add .
4. git commit
5. git push

希望这有帮助。