简体   繁体   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?

I've made some commits on a branch I was not supposed to! 我在一个不该做的分支上做了一些提交!

Sprint 1 Branch 冲刺1分行

  • Commit #4 提交#4
  • Commit #3 提交#3
  • Commit #2 提交#2
  • Commit #1 提交#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.

相关问题 将提交从一个分支移动到另一个分支,然后将它们合并回来 - Move commits from one branch to another and then merge them back in 如何从合并分支移动提交? - How to move commits from merged branch? 从源分支移动提交,但在目标分支将合并回原位时保留提交? - Moving commits from source branch, but keeping them when target branch will be merged back in? 如何将分支头从一个分支移动到另一个 - How to move branch HEAD from one branch to anonther 如何从一个git分支中删除已合并到另一个分支的提交 - How to remove commits from one git branch that have been merged into another 在git中,如何在分支中回滚特定提交,但即使在合并之后也不会将它们回滚到父分支中? - In git, how does one roll back specific commits in a branch but not roll them back in the parent branch even after merging? 当我压缩来自父分支的提交并将其合并到主分支时,子分支会发生什么? - What happens to the child branch when I have squashed the commits from parent branch and merged it into master? Git-合并分支中的新提交-如何合并这些新提交? - Git - new commits in merged branch — how to merge these new commits? 如何取消母版上的提交并将其移到分支 - How to cancel commits on master and move them to branch 如何将 Git 提交移动到另一个分支并在原始分支中删除它们? - How to move Git commits to another branch and delete them in the original branch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM