将 master 分支恢复到之前的状态
我有两个分支:master 和 bonbon
目前它们处于同一点。 bonbon 是一个本来要发布的副项目,但现在已被搁置。我想将主分支恢复到创建 bonbon 分支之前的某个点。不过,我还想保留在 bonbon 分支中所做的一些更改,将来可能需要合并回 bonbon 分支,所以我不想丢失这些更改。恢复主分支并选择所需更改的最佳方法是什么?
我尝试重置分支,但是当我提交并推送更改时它失败了,我最终再次将两个分支合并在一起!我应该用力推动(编辑:我的意思是用力推动)吗?
I have two branches: master and bonbon
Currently they are at the same point. The bonbon was a side project that was going to be released but has now been put on hold. I would like to revert the master branch to a point before the bonbon branch was created. However I would also like to keep some of the changes I made in the bonbon branch and in the future it might be required to merge back in the bonbon branch so I don't want to lose those changes. What is the best way to revert the master branch and pick out the required changes.
I tried to reset the branch but when I commited and push the changes it failed and I've ended up merging the two branches together again! Should I have done a hard push (edit: i meant force push)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是,通过重置分支,您的意思是这样的,而在
master
分支上:在这种情况下,推送将会失败,因为默认情况下,对特定分支的推送需要合并(远程分支与本地分支)是快进,也就是说,远程的引用是本地引用的直接父级。
因此,你别无选择,只能使用 git push --force 或 git push +localref:remoteref 来强制推送(本地引用规范可以如果远程引用具有相同的名称,则被省略)。
例如:
My guess is that by resetting the branch you meant something like this, while on the
master
branch:In this case, a push will fail, because a push on a particular branch, by default, requires that the merge (of the remote branch with your local branch) is a fast forward, that is, that the remote's ref is an immediate parent to your local ref.
So, you have no other choice than to force the push -- either with
git push --force
, or withgit push +localref:remoteref
(the local ref spec can be omitted if the remote ref has the same name).For instance:
至于问题的第一部分。如果与bonbon分支的合并只是顶部提交,那么只需要将master分支重置为合并前的状态即可。
如果您有多个合并点,则必须
立即执行交互式变基以将存储库与远程同步。由于您刚刚覆盖了历史记录,因此 git 不会只允许您提交此状态。你必须用力推动。
请注意,这将重写远程存储库的历史记录,并且使用此存储库的任何人都必须强制拉取更改。
As for the first part of the question. If the merge with the bonbon branch is just the top commit, then you only need to reset the master branch to the state before the merge.
If you have multiple merge points, you will have to do an interactive rebase
Now as for syncing the repo with the remote. Since you have just overwritten history, git won't just allow you to commit this state. You will have to do a forced push.
Note though that this will rewrite the history of the remote repo and anyone using this repo will have to force pull the changes.