简体   繁体   English

如何将 Git 提交移动到另一个分支并在原始分支中删除它们?

[英]How to move Git commits to another branch and delete them in the original branch?

I have the following commits in a branch now:我现在在一个分支中有以下提交:

在此处输入图片说明

I mistakenly started Branch B work and have committed b58 , 151 and 5ef in Branch A.我错误地开始了分支 B 的工作,并在分支 A 中提交了b581515ef

Those 3 commits supposed to be in the Branch B and not in the Branch A. All 3 commits have been pushed to my remote Git server.这 3 次提交应该在分支 B 中而不是在分支 A 中。所有 3 次提交都已推送到我的远程 Git 服务器。

My question我的问题

How do I move those 3 commits to Branch B and delete them in Branch A?如何将这 3 个提交移动到分支 B 并在分支 A 中删除它们? For the Branch B, I want it to branch off from commit 97b in the Branch A.对于分支 B,我希望它从分支 A 中的提交97b分支出来。

I would create the branch B now and then reset branch A to the state you want it to be, and then you will need to force push it to the remote server.我现在会创建分支 B,然后将分支 A 重置为您想要的状态,然后您需要将其强制推送到远程服务器。

git checkout -b branchB
git checkout branchA
git reset --hard HEAD~3
git push -f

Check the status with git log and git status all the way along.一直使用 git log 和 git status 检查状态。

If branch B doesn't already exist and you don't have other work on branch A, you can write如果分支 B 不存在并且您在分支 A 上没有其他工作,则可以编写

git checkout a
git branch b
git reset --hard 97b

That will create B to point to the current A and then set A to 97b.这将创建 B 以指向当前的 A,然后将 A 设置为 97b。 Note that if you push A, you'll need to force push by doing git push origin +a .请注意,如果您推送 A,则需要通过执行git push origin +a来强制推送。

If you have other work on branch B, you can do如果你在分支 B 上有其他工作,你可以做

git checkout b
git cherry-pick 5ef 161 b58b
git checkout a
git reset --hard 97b

You'll need to do a force push for a here as well.您还需要对 a 进行强制推动。

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

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