从 Git 远程拉取时使用远程更改解决冲突
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4785107/
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
Resolve conflicts using remote changes when pulling from Git remote
提问by David Tuite
I'm trying to pull code from my GitHub repo onto my server, but the pull keeps failing because of merge conflicts. I don't want to keep any of the changes that may have occurred on my local server since the last pull.
我正在尝试将代码从我的 GitHub 存储库拉到我的服务器上,但由于合并冲突,拉取一直失败。我不想保留自上次拉取以来我的本地服务器上可能发生的任何更改。
So is there a way I can force Git to overwrite with whatever version is in GitHub, rather than bother me about conflicts?
那么有没有一种方法可以强制 Git 用 GitHub 中的任何版本覆盖,而不是打扰我冲突?
回答by Cascabel
If you truly want to discard the commitsyou've made locally, i.e. never have them in the history again, you're not asking how to pull - pull means merge, and you don't need to merge. All you need do is this:
如果你真的想放弃你在本地所做的提交,即不再在历史中包含它们,你不是在问如何拉 - 拉意味着合并,你不需要合并。您需要做的就是:
# fetch from the default remote, origin
git fetch
# reset your current branch (master) to origin's master
git reset --hard origin/master
I'd personally recommend creating a backup branch at your current HEAD first, so that if you realize this was a bad idea, you haven't lost track of it.
我个人建议首先在您当前的 HEAD 上创建一个备份分支,这样如果您意识到这是一个坏主意,您就不会忘记它。
If on the other hand, you want to keep those commits and make it look as though you merged with origin, and cause the merge to keep the versions from origin only, you can use the ours
merge strategy:
另一方面,如果您想保留这些提交并使其看起来好像与源合并,并导致合并仅保留来自源的版本,则可以使用ours
合并策略:
# fetch from the default remote, origin
git fetch
# create a branch at your current master
git branch old-master
# reset to origin's master
git reset --hard origin/master
# merge your old master, keeping "our" (origin/master's) content
git merge -s ours old-master
回答by Antoine Pelisse
You can either use the answer from the duplicate link pointed by nvm.
您可以使用 nvm 指向的重复链接中的答案。
Or you can resolve conflicts by using their changes (but some of your changes might be kept if they don't conflict with remote version):
或者您可以通过使用他们的更改来解决冲突(但如果您的某些更改与远程版本不冲突,则可能会保留):
git pull -s recursive -X theirs