如何将主分支的更改分离到另一个分支而不覆盖历史记录?

发布于 2024-10-19 07:37:00 字数 210 浏览 3 评论 0原文

最近我开始为我的项目实现新的实验功能。不幸的是,我在开始之前忘记了分支,并将共享存储库服务器上的多个提交推送到主分支中。由于其他一些人可能已经检查了我的提交,我想避免覆盖服务器上的历史记录。

由于我的更改,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原来分手还会想你 2024-10-26 07:37:00

该答案使用命令行工具并采用不同的方法来管理分支。它可能比使用各种风格的多次重置更容易混淆。

Git 1.7.2 及更高版本中的 revert 命令可以在一个命令中还原多个提交:

git revert last-stable..

这将为 last-stable 之后的每个提交创建还原提交,包括:当前 HEAD 提交(按相反顺序)。如果您正在处理许多不需要的提交,那么您可能希望在一次提交中将它们全部还原:

git revert -n last-stable..
git commit # edit the message to explain that you are reverting multiple commits

以下是如何将其与其他一些命令结合起来,以在还原提交之上重新建立实验分支:
(假设共享分支名为master

# Make sure we have the latest shared master
git checkout master
git pull

# Mark the last experimental commit
git branch experimental

# Revert the experimental commits
git revert last-stable.. # optionally use -n and manually commit batched reverts

# Replay the experimental commits on top of the reverted commits
git checkout -B experimental master
git cherry-pick last-stable..experimental@{1}

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:

git revert last-stable..

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:

git revert -n last-stable..
git commit # edit the message to explain that you are reverting multiple commits

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)

# Make sure we have the latest shared master
git checkout master
git pull

# Mark the last experimental commit
git branch experimental

# Revert the experimental commits
git revert last-stable.. # optionally use -n and manually commit batched reverts

# Replay the experimental commits on top of the reverted commits
git checkout -B experimental master
git cherry-pick last-stable..experimental@{1}
殤城〤 2024-10-26 07:37:00

我在写问题的时候已经想出了解决问题的办法。我描述该解决方案,希望它对将来的人有用。对于所有更改,我使用 gitk 和 git-gui。我建议您这样做,因为它比使用命令行容易得多,并且您将对正在发生的事情有一个直观的了解。以下是我已采取的步骤:

  1. 执行硬重置到最后的稳定提交。这将从索引和工作目录中删除所有不稳定的更改。
  2. 对共享服务器上的最后一次提交执行混合重置。这会将索引重置为共享服务器上的当前提交,但会使工作目录保持稳定状态。
  3. 提交工作目录中的所有更改。然后,您将进行提交,以取消主服务器上所有不稳定的更改。
  4. 在此提交中创建实验分支并检查它。此时实验分支将没有实验代码,所以让我们修复这个问题。
  5. 樱桃选择所有应该进入实验分支的提交。这样您将在实验分支上重新引入这些更改。确保不要挑选我们刚刚做出的最后一次提交。如果使用 gitk,它将显示一条警告,表明您正在尝试挑选已经在分支中的提交 - 忽略这些警告并继续。
  6. 中提琴。你完成了。

为了安全起见,我建议您在本地进行所有更改,并仅在完成并确保一切正确后才推送它们。然后,如果出现错误,您可以随时删除存储库并从共享服务器再次克隆它。

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:

  1. Perform hard reset to the last stable commit. This will remove all unstable changes from the index and working directory.
  2. Perform mixed reset onto last commit on the shared server. This will reset the index to the current commit on the shared server, but will keep the working directory in stable state.
  3. Commit all the changes in the working directory. You will then have commit that will cancel all the unstable changes on the master.
  4. Create experimental branch at this commit and check it out. At this point experimental branch will have no experimental code, so let's fix this.
  5. Cherry pick all commits that should go into experimental branch. This way you will reintroduce back these changes on the experimental branch. Make sure not to cherry-pick last commit that we have just made. If using gitk, it will show a warning that you are trying to cherry-pick the commit that was in the branch already - ignore these warning and proceed.
  6. Viola. You are done.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文