如何解决git所说的“提交更改或在合并之前将其隐藏”?

本文翻译自:How do I resolve git saying “Commit your changes or stash them before you can merge”?

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 wp-content / w3tc-config / master.php

Please, commit your changes or stash them before you can merge. 请先提交您的更改或将其存储起来,然后再进行合并。

So I ran, 所以我跑了

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

and tried again and I get the same message. 并再次尝试,我得到相同的消息。 I'm assuming that w3tc changed something in the config file on the server. 我假设w3tc更改了服务器上的配置文件中的某些内容。 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). 我不在乎本地副本还是远程副本在服务器上(我认为远程副本最好),我只想能够合并其余的更改(插件更新)。

Any ideas? 有任何想法吗?


#1楼

参考:https://stackoom.com/question/1440L/如何解决git所说的-提交更改或在合并之前将其隐藏


#2楼

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 使用git reset --hard
    or git checkout -t -f remote/branch git checkout -t -f remote/branch

    Or: Discard local changes for a specific file 或:放弃特定文件的本地更改

    using git checkout filename 使用git checkout filename


#3楼

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. remote:错误:您对以下文件的本地更改将被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.ext and then the local and remote repositories actually matched and I could continue to work, and deploy. 因此,我最终通过跟踪接收后钩子并找到了它,最终要做的是转到服务器上的远程存储库,然后进行了更改(实际上,这不在我的本地存储库中,表示它匹配,没有更改,没有提交,没有更新,等等。)因此,在本地时,没有任何更改,在服务器上,我然后进行了git checkout -- some/file.ext ,然后本地和远程存储库实际上匹配,我可以继续工作并部署。 Not entirely sure how this situation occurred, though a couple dozen developers plus IT changes may had something to do with it. 尽管有数十名开发人员加上IT变更可能与这种情况有关,但还不能完全确定这种情况是如何发生的。


#4楼

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 stash and removes them from the working directory. 第一条命令将您的更改临时存储存储中,并将其从工作目录中删除。

The second command switches branches. 第二条命令切换分支。

The third command restores the changes which you have stored in the stash (the --index option is useful to make sure that staged files are still staged). 第三个命令将还原您存储在存储中的更改( --index选项可用于确保已暂存的文件仍处于暂存状态)。


#5楼

You can try one of the following methods: 您可以尝试以下方法之一:

rebase 重新设定

For simple changes try rebasing on top of it while pulling the changes, eg 对于简单的更改,请尝试在更改的基础上重新进行更改,例如

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 , fetch and rebase origin/master git commands. 这等效于: checkout masterfetchrebase origin/master git命令。

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, eg 如果您不关心本地更改,则可以(强制)切换到其他临时分支,然后将其切换回原来的状态,例如

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), eg 如果您不关心本地更改,请尝试将其重置为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. 如果上述方法无济于事,则可能是您的git规范化文件( .gitattributes )中的规则,因此最好提交其内容。 Or your file system doesn't support permissions, so you've to disable filemode in your git config. 或者您的文件系统不支持权限,因此您必须在git config中禁用filemode

Related: How do I force "git pull" to overwrite local files? 相关: 如何强制“ git pull”覆盖本地文件?


#6楼

Try this 尝试这个

git stash save ""

and try pull again 然后尝试再次拉

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值