[英]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?
I've made some commits on a branch I was not supposed to! 我在一个不该做的分支上做了一些提交!
Sprint 1 Branch 冲刺1分行
I want to move Commits #2 - #4 into a Hot Fix Branch so that they can be merged back into the Sprint 1 Branch at a later date. 我想将提交#2-#4移到Hot Fix分支中,以便以后可以将它们合并回Sprint 1分支中。 It is important that whatever I do on the Sprint 1 Branch to get rid of these commits temporarily won't take precedence over these commits when I merge them back in (eg reverting them after creating a hotfix branch for them).
重要的是,我在Sprint 1分支上所做的任何暂时摆脱这些提交的操作都不会优先于在我将它们合并回这些提交时(例如,在为它们创建修补程序分支后将其还原)。 How can I do this?
我怎样才能做到这一点?
As always, in git, there are many possibilities to achieve something. 与往常一样,在git中,有很多实现目标的可能性。 So, let's say you have 4 commits on branch
sprint
and you want to move the last commits to a branch fix. 因此,假设您在分支
sprint
上有4次提交,而您想将最后一次提交移至分支修订。 To simulate your problem, I created the sprint
branch as follows: 为了模拟您的问题,我创建了
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
Now, instead of moving commits, we just create the fix
branch from the head
of the sprint
branch: 现在,我们无需移动提交,而只是从
sprint
分支的head
创建了fix
分支:
git checkout -b fix
git push origin fix
Now, we reset the sprint
branch to the first commit. 现在,我们将
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
So, the fix
branch has now all the commits, and the sprint
branch only has the first commit, which, I understand, is what you needed. 因此,
fix
分支现在具有所有提交,而sprint
分支仅具有第一个提交,据我所知,这就是您所需要的。
EDIT : you need RW+
permissions to use the -f
or --force
option (see comments below). 编辑 :您需要
RW+
权限才能使用-f
或--force
选项(请参阅下面的注释)。
Use git rebase for that case. 在这种情况下,请使用git rebase 。 It rebases commits onto another branch.
它会将提交重新部署到另一个分支。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.