是的,创建第三个分支并执行merge -s ours
是一种解决方案。
但你会发现所有的"让我们不要宣传任何“theirs”合并策略"这里。
在用另一个分支替换您的工作或者仅仅摆脱当前的工作并通过另一个工作完全替换它之间,Junio C. Hamano(主要Git Maintainer)更喜欢第二种方法:
I think "
-s theirs
" is even worse. It is how you would discard what you did (perhaps because the other side has much better solution than your hack), but that can be much more easily and cleanly done with:$ git reset --hard origin/master
Some people might say "But with '
merge -s theirs
', I can keep what I did, too". That reset is simply discarding what I did.That logic also is flawed. You can instead:
$ git branch i-was-stupid $ git reset --hard origin/master
if you really want to keep record of your failure.
One big problem "
-s theirs
" has, compared to the above "reset to origin, discarding or setting aside the failed history" is that your 'master' history that your further development is based on will keep your failed crap in it forever if you did "-s theirs".Hopefully you will become a better programmer over time, and you may eventually have something worth sharing with the world near the tip of your master branch. When that happens, however, you cannot offer your master branch to be pulled by the upstream, as the wider world will not be interested in your earlier mistakes at all.
git reset --hard origin/master
。 - Rian Sanderson前几天遇到了这个问题:
httpx://seanius.net/blog/2011/02/git-merge-s-theirs/
更新:旧链接已失效。以下是通过Archive.org的Wayback Machine获取的文章:
git merge -s ours ref-to-be-merged
git diff --binary ref-to-be-merged | git apply -R --index
git commit -F .git/COMMIT_EDITMSG --amend
reset --hard
和reset --soft
与引用进行大量操作,以便到达我想要的位置,这是粗暴的并且会弄乱提交历史(所以你最终会经常使用push --force
:)),但是你的解决方案看起来很可靠。我很快就会去查看它。 - Dan Rosenstarkgit reset --hard BRANCHNAME
选项,但我发现在Git(v.1.7.1至少)中有一个"theirs"选项。git checkout -b editBranch
-- 编辑您的文件 --
git add .
git commit -m "更新了文件"
git checkout master
git merge -Xtheirs editBranch
如果您在editBranch中删除了任何文件,则会出现合并冲突,可以使用git rm FILENAME
解决。-Xtheirs
和一个假想的 -s theirs
策略完全不同。-Xtheirs
将 theirs
选项传递给递归合并策略,通过选择“theirs”版本来解决冲突块。如果像 -s ours
那样实现 -s theirs
,则会选择整个“their”树而不是任何形式的智能合并。 - CB Bailey
git merge BRANCH --squash
做所有事情,有时候使用git reset --soft master
,然后像提交新的提交一样检查。很抱歉,但如果您在另一个问题中要求一个简单的例子,那就太好了。 - Dan Rosenstark