覆盖和推送 Git 分支

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

Overwriting and Pushing a Git Branch

git

提问by tmountain

In merging my changes against an upstream master, I frequently find myself doing the following:

在将我的更改与上游 master 合并时,我经常发现自己执行以下操作:

git checkout somefeature
git checkout -b integration
git rebase master # resolving conflicts along the way
git checkout somefeature
git merge integration # or rebase, doesn't matter in this example

I'll often find that merging the integration branch back into my feature branch fails do to some conflicts. The first question I have is, "why is this happening if my integration branch is a descendent of somefeature and I've already resolved conflicts against the upstream master?"

我经常会发现将集成分支合并回我的功能分支无法解决一些冲突。我的第一个问题是,“如果我的集成分支是某个功能的后代并且我已经解决了与上游主节点的冲突,为什么会发生这种情况?”

If you're wondering why I'm using an integration branch to begin with, it's to prevent polluting my current branch with a half-failed merge.

如果您想知道为什么我开始使用集成分支,这是为了防止半失败合并污染我当前的分支。

My current workaround is to do this:

我目前的解决方法是这样做:

git checkout integration
git branch -f somefeature # overwrite the branch

The problem now is that I can't push my changes back to a remote branch:

现在的问题是我无法将更改推送回远程分支:

git push origin somefeature
! [rejected]        somefeature -> somefeature (non-fast forward)

So now I have to remove the remote branch and re-push my changes. This can't be the optimal way to do this, so I'm wondering, "what's the best way to overwrite a branch and push the changes to a remote branch?"

所以现在我必须删除远程分支并重新推送我的更改。这不是执行此操作的最佳方法,所以我想知道,“覆盖分支并将更改推送到远程分支的最佳方法是什么?”

回答by Nemo157

The problem is caused because git rebasegenerates a new series of commits that aren't descended from the somefeaturebranch, then when you try and merge them back into somefeaturethe conflict resolution done during the rebase doesn't apply. If you were to just merge instead of rebase then this would work as the merge commit woulddescend from the somefeaturebranch.

问题是因为git rebase生成了一系列不是来自somefeature分支的新提交,然后当您尝试将它们合并回somefeature在 rebase 期间完成的冲突解决方案不适用时。如果您只是合并而不是变基,那么这将起作用,因为合并提交将从somefeature分支下降。

In terms of pushing to the remote branch you can just use --forceto make the push succeed, that will cause problems for anyone else that has a copy of it though.

在推送到远程分支方面,您可以使用它--force来使推送成功,但这会给拥有它的副本的任何其他人带来问题。

回答by Walter Mundt

You could use git merge -s recursive -Xtheirs, which will auto-resolve conflicts in favor of the integration branch. However, that still does a merge and may not play well with files moving around and such.

您可以使用git merge -s recursive -Xtheirs,它将自动解决有利于集成分支的冲突。但是,这仍然会进行合并,并且可能无法很好地处理文件移动等。

However, since your branch is pushed, you have a reason to want to keep its history around. To get the ideal behavior you want, you could use the 'ours' strategy in the integration branch.

但是,由于您的分支被推送,您有理由想要保留其历史记录。要获得您想要的理想行为,您可以在集成分支中使用“我们的”策略。

git checkout somefeature
git checkout -b integration
git rebase master # resolving conflicts along the way
git merge -s ours somefeature # mark integration as superseding the somefeature branch
git checkout somefeature
git merge integration # this is now a fast-forward, no conflicts

回答by VonC

You could make sure your final merge overwrite the destination branch with a merge driver:
See "Can I tell git pull to overwrite instead of merge?" for example.

您可以确保您的最终合并使用合并驱动程序覆盖目标分支:例如,
请参阅“我可以告诉 git pull 覆盖而不是合并吗?”。

The idea is to have a custom merge driver with a "keepTheir" script.

这个想法是有一个带有“ keepTheir”脚本自定义合并驱动程序

回答by Adam Dymitruk

Your first bit of operations is very strange. You make a copy of somefeature. You rebase that against master. Then you go back to that feature and merge it with the rebased equivelant of itself. This is not the way to do things.

你的第一个操作很奇怪。您制作了某个功能的副本。您将其重新设置为针对 master。然后你回到那个特性并将它与它自身的rebase equivelant 合并。这不是做事的方式。

Don't over complicate things. If you want the feature branch to start somewhere else instead (like the latest master), just rebase it.

不要把事情复杂化。如果您希望功能分支从其他地方开始(例如最新的主分支),只需重新设置它即可。

Then push it with --force (or -f). Tell anyone else that was using that feature branch that they have to fetch the changes and adjust anything that they had locally that they didn't push yet.

然后用--force(或-f)推动它。告诉正在使用该功能分支的任何其他人,他们必须获取更改并调整他们在本地拥有但尚未推送的任何内容。

Merge is better in the case when others have been working on it.

在其他人一直在处理的情况下,合并会更好。

Hope this helps.

希望这可以帮助。