git 解决合并冲突:强制覆盖所有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14250257/
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 merge conflicts: Force overwrite all files
提问by Qix - MONICA WAS MISTREATED
I am working on a git repository by myself(so yes, I know the implications and the warnings of doing this) and somehow one of the trees got a commit after being pushed when it shouldn't have.
我工作的一个Git仓库由我(所以是的,我知道的含义,这样的警告),不知怎么的一棵树上有推时,它不应该有后提交。
Now I'm trying to pull back and it's complaining about hundreds of merge conflicts.
现在我试图撤回,它抱怨数百个合并冲突。
Is there a way to tell git to forcefullyoverwrite any and all files locally that are coming from the remote server? Is there a faster way than doing git reset --hard HEAD~1
and then doing the pull?
有没有办法告诉 git强制覆盖来自远程服务器的本地所有文件?有没有比做git reset --hard HEAD~1
然后做拉更快的方法?
On that same note, is there a way to do the same with with a simple merge? Everything I've seen suggests to check out each and every file during the merge conflict resolution stage, but with hundreds of files it's just not possible to do so manually.
同样,有没有办法通过简单的合并来做同样的事情?我所看到的一切都建议在合并冲突解决阶段检查每个文件,但是对于数百个文件,手动这样做是不可能的。
回答by William Seiti Mizuta
There are three simple solutions to copy the last version that is in you remote repository, discarding all changes that you have made locally:
有三种简单的解决方案可以复制远程存储库中的最后一个版本,丢弃您在本地所做的所有更改:
Discard your repository and clone again. This is the most simple solution, but if your repository is big, it can take a long time, and may require extra effort like re
configure
ing, etc.Discard the local changes with
git reset --hard <hash>
and then do agit pull
. The problem is you need to first find a commit that precedes whatever change history you are trying to avoid. After resetting to that commit hash, do agit pull
.Do a
git fetch
to bring the updates to your local reference of the remote branch (usually origin/master) and then do agit reset --hard
passing this reference, ie,git reset --hard origin/master
.
丢弃您的存储库并再次克隆。这是最简单的解决方案,但如果您的存储库很大,则可能需要很长时间,并且可能需要额外的努力,例如重新
configure
编译等。丢弃本地更改,
git reset --hard <hash>
然后执行git pull
. 问题是您需要首先找到一个提交,该提交位于您试图避免的任何更改历史记录之前。重置为该提交哈希后,执行git pull
.执行 a
git fetch
将更新带到远程分支的本地引用(通常是 origin/master),然后git reset --hard
传递此引用,即git reset --hard origin/master
.
回答by Sithara
git reset --hard {remote_repository_name}/{branch_name}
Example:
例子:
git reset --hard upstream/branch1
If you are working with a branch you can use the above code.But before that, you have to set (upstream or origin) to your local repository,
如果你正在使用一个分支,你可以使用上面的代码。但在此之前,你必须设置(上游或起源)到你的本地存储库,
git remote add upstream https://github.com/lakini/example.git
here,https://github.com/lakini/example.gitis the remote upstream repository.
在这里,https://github.com/lakini/example.git是远程上游存储库。
Same as like this we can work in the remote repository(origin) as well.
同样,我们也可以在远程存储库(源)中工作。
git reset --hard origin/branch1