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

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

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

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

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


当前回答

这是我解决问题的策略。

问题陈述

我们需要对10多个文件进行更改。我们尝试了PULL(git PULL origin master),但git喊道:

错误:您对以下文件的本地更改将被覆盖通过合并:请先提交更改或将其隐藏合并

我们尝试执行commit,然后执行pull,但它们也不起作用。

解决方案

实际上,我们处于肮脏的阶段,因为这些文件位于“暂存区”,也就是“索引区”,有些文件位于“头部区”,即“本地Git目录”。我们想从服务器中提取更改。

查看此链接,以明确方式获取有关Git不同阶段的信息:Git阶段

我们遵循以下步骤

git stash(这使我们的工作目录变得干净。您的更改通过git存储在堆栈中)。git pull-origin master(从服务器中拉取更改)git stash apply(应用堆栈中的所有更改)gitcommit-m“message”(已提交更改)git推送原始主机(将更改推送到服务器)git stash drop(删除堆栈)

让我们了解您何时以及为什么需要隐藏

如果您处于脏状态,则意味着您正在对文件进行更改,然后由于任何原因,您被迫拉入或切换到另一个分支以进行一些非常紧急的工作,因此此时,在提交更改之前,您无法拉入或转换。隐藏命令在这里作为帮助。

摘自ProGIT,第2版:

通常,当你在项目的一部分工作时一个混乱的状态,你需要切换分支来处理其他东西。问题是,你不想完成了一半的工作,这样你就可以稍后回到这一点。这个这个问题的答案是gitstash命令。Stashing需要工作目录的脏状态–即修改后的跟踪文件和暂存更改–并将其保存在未完成的堆栈中您可以随时重新应用的更改。

其他回答

git pull --rebase --autostash

-r、 --rebase[=false|true|合并|保留|交互式]如果为true,则在提取后的上游分支。如果有与上游相对应的远程跟踪分支

--autostash,--无autostash在开始重新基础之前,如果需要,并在完成后应用隐藏条目


我不知道为什么这个问题还没有答案,但正如你们所看到的,解决方法很简单。这里的所有答案都是相同的:删除/保存本地更改并应用到上游,然后(如果保存)在顶部应用本地更改。

git pull--rebase--autostash一步一步地做什么:

1. your local changes saved by `--autostash`
2. your local commits saved by `--rebase`
3. commits from upstream applied to your branch
4. your local commits are restored on top of upstream
5. your local changes are restored to working directory

我的案子(可能也是你的):

我有本地更改(工作目录中的更改):

当我尝试进行远程更改时,我收到错误:

这些更改与本地更改不相交:

因此,当我拉动--rebase--autostash时,保存并应用的本地更改不会自动出现任何问题

现在我的本地变化有点低:

我是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

希望这有帮助。

如果使用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 checkout file_to_overwrite

试试这个

git fetch --all 

git reset --hard origin/master

git pull origin master

对我来说,用力拉