我如何解决 git 说“在合并之前提交您的更改或隐藏它们”?

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

How do I resolve git saying "Commit your changes or stash them before you can merge"?

git

提问by Jo Sprague

I made some updates on my local machine, pushed them to a remote repository, and now I'm trying to pull the changes to the server and I get the message;

我在本地机器上进行了一些更新,将它们推送到远程存储库,现在我正在尝试将更改拉到服务器并收到消息;

error: Your local changes to the following files would be overwritten by merge:

wp-content/w3tc-config/master.php

Please, commit your changes or stash them before you can merge.

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

wp-content/w3tc-config/master.php

请在合并之前提交您的更改或隐藏它们。

So I ran,

所以我跑了,

git checkout -- wp-content/w3tc-config/master.php

and tried again and I get the same message. I'm assuming that w3tcchanged something in the config file on the server. I don't care whether the local copy or remote copy goes on the server (I suppose the remote one is best), I just want to be able to merge the rest of my changes (plugin updates).

再试一次,我得到了同样的消息。我假设w3tc更改了服务器上配置文件中的某些内容。我不在乎服务器上是本地副本还是远程副本(我认为远程副本最好),我只想能够合并我的其余更改(插件更新)。

Any ideas?

有任何想法吗?

回答by stdcall

You can't merge with local modifications. Git protects you from losing potentially important changes.

您不能与本地修改合并。Git 保护您免于丢失潜在的重要更改。

You have three options:

您有三个选择:

  • Commit the change using

    git commit -m "My message"
    
  • Stash it.

    Stashing acts as a stack, where you can push changes, and you pop them in reverse order.

    To stash, type

    git stash
    

    Do the merge, and then pull the stash:

    git stash pop
    
  • Discard the local changes

    using git reset --hard
    or git checkout -t -f remote/branch

    Or: Discard local changes for a specific file

    using git checkout filename

  • 使用提交更改

    git commit -m "My message"
    
  • 藏起来。

    存储充当堆栈,您可以在其中推送更改,并以相反的顺序弹出它们。

    要存储,请键入

    git stash
    

    进行合并,然后拉出存储:

    git stash pop
    
  • 丢弃本地更改

    使用git reset --hard
    git checkout -t -f remote/branch

    或:放弃特定文件的本地更改

    使用 git checkout filename

回答by Loganathan

git stash
git pull <remote name> <remote branch name> (or) switch branch
git stash apply --index

The first command stores your changes temporarily in the stashand removes them from the working directory.

第一个命令将您的更改临时存储stash中,并将它们从工作目录中删除。

The second command switches branches.

第二个命令切换分支。

The third command restores the changes which you have stored in the stash (the --indexoption is useful to make sure that staged files are still staged).

第三个命令恢复您存储在存储中的更改(该--index选项对于确保暂存文件仍然暂存很有用)。

回答by kenorb

You can try one of the following methods:

您可以尝试以下方法之一:

rebase

变基

For simple changes try rebasing on top of it while pulling the changes, e.g.

对于简单的更改,尝试在拉动更改的同时在其之上重新定位,例如

git pull origin master -r

So it'll apply your current branch on top of the upstream branch after fetching.

因此,它会在获取后将您当前的分支应用到上游分支之上。

This is equivalent to: checkout master, fetchand rebase origin/mastergit commands.

这相当于:checkout master,fetchrebase origin/mastergit 命令。

This is a potentially dangerous mode of operation. It rewrites history, which does not bode well when you published that history already. Do not use this option unless you have read git-rebase(1)carefully.

这是一种潜在危险的操作模式。它改写了历史,这在您已经发布了该历史时并不是好兆头。除非您git-rebase(1)仔细阅读,否则请勿使用此选项。



checkout

查看

If you don't care about your local changes, you can switch to other branch temporary (with force), and switch it back, e.g.

如果你不关心你的本地更改,你可以临时切换到其他分支(强制),然后切换回来,例如

git checkout origin/master -f
git checkout master -f


reset

重启

If you don't care about your local changes, try to reset it to HEAD (original state), e.g.

如果您不关心本地更改,请尝试将其重置为 HEAD(原始状态),例如

git reset HEAD --hard

If above won't help, it may be rules in your git normalization file (.gitattributes) so it's better to commit what it says. Or your file system doesn't support permissions, so you've to disable filemodein your git config.

如果以上没有帮助,它可能是您的 git 规范化文件 ( .gitattributes) 中的规则,因此最好提交它所说的内容。或者你的文件系统不支持权限,所以你必须filemode在你的 git 配置中禁用。

Related: How do I force "git pull" to overwrite local files?

相关:如何强制“git pull”覆盖本地文件?

回答by Mr Nobody

Try this

尝试这个

git stash save ""

and try pull again

并再次尝试拉

回答by Mike

So the situation that I ran into was the following:

所以我遇到的情况如下:

error: Your local changes to the following files would be overwritten by merge: wp-content/w3tc-config/master.php Please, commit your changes or stash them before you can merge.

错误:您对以下文件的本地更改将被合并覆盖: wp-content/w3tc-config/master.php 请在合并之前提交您的更改或隐藏它们。

except, right before that, was remote: so actually this:

除了在此之前是远程的:所以实际上是这样的:

remote: error: Your local changes to the following files would be overwritten by merge: some/file.ext Please, commit your changes or stash them before you can merge.

远程:错误:您对以下文件的本地更改将被合并覆盖:some/file.ext 请在合并之前提交您的更改或隐藏它们。

What was happening was (I think, not 100% positive) the git post receive hook was starting to run and screwing up due to movement changes in the remote server repository, which in theory, shouldn't have been touched.

发生的事情是(我认为,不是 100% 肯定)由于远程服务器存储库中的移动变化,git post 接收钩子开始运行并搞砸了,理论上不应该被触及。

So what I ended up doing by tracing through the post-receive hook and finding this, was having to go to the remote repository on the server, and there was the change (which wasn't on my local repository, which, in fact, said that it matched, no changes, nothing to commit, up to date, etc.) So while on the local, there were no changes, on the server, I then did a git checkout -- some/file.extand then the local and remote repositories actually matched and I could continue to work, and deploy. Not entirely sure how this situation occurred, though a couple dozen developers plus IT changes may had something to do with it.

所以我最终通过跟踪 post-receive 钩子并找到了这个,不得不转到服务器上的远程存储库,并且发生了更改(这不在我的本地存储库中,实际上,说它匹配,没有更改,没有提交,最新等。)所以在本地,没有更改,在服务器上,然后我做了一个git checkout -- some/file.ext,然后本地和远程存储库实际上匹配,我可以继续工作和部署。不完全确定这种情况是如何发生的,尽管几十个开发人员加上 IT 变化可能与它有关。

回答by Dark Matter

WARNING: This will delete untracked files, so it's not a great answer to this question.

警告:这将删除未跟踪的文件,因此这不是这个问题的好答案。

In my case, I didn't want to keep the files, so this worked for me:

就我而言,我不想保留文件,所以这对我有用:

Git 2.11 and newer:

Git 2.11 及更新版本:

git clean  -d  -fx .

Older Git:

旧版 Git:

git clean  -d  -fx ""


Reference:http://www.kernel.org/pub/software/scm/git/docs/git-clean.html

参考:http : //www.kernel.org/pub/software/scm/git/docs/git-clean.html

  • -x means ignored files are also removed as well as files unknown to git.

  • -d means remove untracked directories in addition to untracked files.

  • -f is required to force it to run.

  • -x 表示忽略的文件以及 git 未知的文件也将被删除。

  • -d 意味着除了未跟踪的文件之外还删除未跟踪的目录。

  • -f 需要强制它运行。

回答by Manpreet

To keep record of your newly created files while resolving this issue:

要在解决此问题时记录新创建的文件,请执行以下操作:

If you have newly created files, you can create a patch of local changes, pull in remote merges and apply your local patch after the remote merge is complete as defined step by step below:

如果您有新创建的文件,您可以创建一个本地更改补丁,拉入远程合并并在远程合并完成后应用您的本地补丁,如下分步定义:

  1. Stage your local changes. (do not commit). Staging is required to create patch of new created files (as they are still untracked)
  1. 暂存您的本地更改。(不要提交)。需要暂存来创建新创建的文件的补丁(因为它们仍未被跟踪)

git add .

git add .

  1. Create a patch to keep record
  1. 创建一个补丁来保存记录

git diff --cached > mypatch.patch

git diff --cached > mypatch.patch

  1. Discard local changes and delete new local files
  1. 放弃本地更改并删除新的本地文件

git reset --hard

git reset --hard

  1. Pull changes
  1. 拉动更改

git pull

git pull

  1. Apply your patch
  1. 应用您的补丁

git apply mypatch.patch

git apply mypatch.patch

Git will merge changes and create .rej files for changes which are not merged.

Git 将合并更改并为未合并的更改创建 .rej 文件。

As suggested by Anu, if you have issues applying patch, try:

按照 Anu 的建议,如果您在应用补丁时遇到问题,请尝试:

git apply --reject --whitespace=fix mypatch.patchThis answer git: patch does not applytalks in detail about this issue

git apply --reject --whitespace=fix mypatch.patch这个答案git: patch does not apply有关此问题的详细讨论

Enjoy your continued work on your feature, and commit your local changes when done.

享受您对功能的持续工作,并在完成后提交本地更改。

回答by Leo

For me, only git reset --hardworked.

对我来说,只有git reset --hard工作。

Commiting was not an option, as there was nothing to commit.

提交不是一种选择,因为没有什么可提交的。

Stashing wasn't an option because there was nothing to stash.

藏匿不是一种选择,因为没有什么可以藏匿的。

Looks like it could have been from excluded files in .git/info/excludeand having git update-index --assume-unchanged <file>'ed some files.

看起来它可能来自排除的文件,.git/info/exclude并且已经git update-index --assume-unchanged <file>编辑了一些文件。

回答by Rahul Mankar

Asking for commit before pull

在拉取之前要求提交

  • git stash
  • git pull origin << branchname >>
  • 混帐
  • git pull origin << 分支名称 >>

If needed :

如果需要的话 :

  • git stash apply
  • git stash 应用

回答by CodyBugstein

In my case, I backed up and then deleted the file that Git was complaining about, committed, then I was able to finally check out another branch.

就我而言,我备份然后删除了 Git 抱怨、提交的文件,然后我终于能够查看另一个分支。

I then replaced the file, copied back in the contents and continued as though nothing happened.

然后我替换了文件,将内容复制回并继续,好像什么也没发生。