简体   繁体   中英

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.

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.

My question

How do I move those 3 commits to Branch B and delete them in Branch A? For the Branch B, I want it to branch off from commit 97b in the Branch A.

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.

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.

If branch B doesn't already exist and you don't have other work on branch A, you can write

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. Note that if you push A, you'll need to force push by doing git push origin +a .

If you have other work on branch B, you can do

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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