繁体   English   中英

如何将一个分支从分支的头提交到新分支中,而合并后又不丢弃它们呢?

[英]How does one Move Commits from the Head of a Branch into a New Branch and not have them be Discarded when Merged back in?

我在一个不该做的分支上做了一些提交!

冲刺1分行

  • 提交#4
  • 提交#3
  • 提交#2
  • 提交#1

我想将提交#2-#4移到Hot Fix分支中,以便以后可以将它们合并回Sprint 1分支中。 重要的是,我在Sprint 1分支上所做的任何暂时摆脱这些提交的操作都不会优先于在我将它们合并回这些提交时(例如,在为它们创建修补程序分支后将其还原)。 我怎样才能做到这一点?

与往常一样,在git中,有很多实现目标的可能性。 因此,假设您在分支sprint上有4次提交,而您想最后一次提交移至分支修订。 为了模拟您的问题,我创建了sprint分支,如下所示:

git checkout -b sprint
touch c1 c2 c3 c4
git add c1
git commit -m "commit 1"
git add c2
git commit -m "commit 2"
git add c3
git commit -m "commit 3"
git add c4
git commit -m "commit 4"
git push origin sprint

现在,我们无需移动提交,而只是从sprint分支的head创建了fix分支:

git checkout -b fix
git push origin fix

现在,我们将sprint分支重置为第一次提交。

git checkout sprint
git log --oneline # copy the hash of the first commit, say f58f8d8
git reset --hard f58f8d8
git push -f origin sprint # -f (force option) is needed because of the hard reset

因此, fix分支现在具有所有提交,而sprint分支仅具有第一个提交,据我所知,这就是您所需要的。

编辑 :您需要RW+权限才能使用-f--force选项(请参阅下面的注释)。

在这种情况下,请使用git rebase 它会将提交重新部署到另一个分支。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM