如何将主分支的更改分离到另一个分支而不覆盖历史记录?
最近我开始为我的项目实现新的实验功能。不幸的是,我在开始之前忘记了分支,并将共享存储库服务器上的多个提交推送到主分支中。由于其他一些人可能已经检查了我的提交,我想避免覆盖服务器上的历史记录。
由于我的更改,master 目前不稳定,这也不好。因此,我想恢复对 master 所做的更改,创建一个单独的分支来包含这些更改,并且一旦它们足够稳定,仍然能够将它们重新引入(合并)回 master。
Recently I have started to implement new experimental feature for my project. Unfortunately I forgot to branch before starting and pushed several commits onto shared repository server into the master branch. Since some other people may have already checked my commits out I would like to avoid overwriting the history on the server.
Due to my changes the master is currently unstable, which is also not good. Therefore I would like to revert changes made to master, create a separate branch that will contain these changes and still be able to reintroduce (merge) them back to master once they are stable enough.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该答案使用命令行工具并采用不同的方法来管理分支。它可能比使用各种风格的多次重置更容易混淆。
Git 1.7.2 及更高版本中的
revert
命令可以在一个命令中还原多个提交:这将为
last-stable
之后的每个提交创建还原提交,包括:当前 HEAD 提交(按相反顺序)。如果您正在处理许多不需要的提交,那么您可能希望在一次提交中将它们全部还原:以下是如何将其与其他一些命令结合起来,以在还原提交之上重新建立实验分支:
(假设共享分支名为
master
)This answer uses the command line tools and takes a different approach to managing the branches. It may be less confusing than using multiple resets of various flavors.
The
revert
command in Git 1.7.2 and later can revert multiple commits in one command:This will create revert commits for each commit after
last-stable
up to, and including, the current HEAD commit (in reverse order). If you are dealing with many unwanted commits, then you may want to revert them all in a single commit:Here is how to put that together with some other commands to re-establish your experimental branch on top of the revert commit(s):
(assumes that the shared branch is named
master
)我在写问题的时候已经想出了解决问题的办法。我描述该解决方案,希望它对将来的人有用。对于所有更改,我使用 gitk 和 git-gui。我建议您这样做,因为它比使用命令行容易得多,并且您将对正在发生的事情有一个直观的了解。以下是我已采取的步骤:
为了安全起见,我建议您在本地进行所有更改,并仅在完成并确保一切正确后才推送它们。然后,如果出现错误,您可以随时删除存储库并从共享服务器再次克隆它。
I have figured out the solution to the problem while I was writing the question. I describe the solution in hope that it may be useful to someone in future. For all the changes I was using gitk and git-gui. I recommend you to do the same as it's much easier than using command line and you will have a visual understanding of what is going on. Here are the steps that I have taken:
To be safe, I recommend you to make all changes locally and push them only when you are done and sure that everything is correct. Then in case of an error you can always delete the repository and clone it again from the shared server.