2

I have the following commits in a branch now:

enter image description here

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.

Zulhilmi Zainudin
  • 9,017
  • 12
  • 62
  • 98
  • use a cherry picking function. so you can pick your commits and merge them into correct branch. but you should also remove commits in a wrong branch – hotfix Aug 17 '18 at 08:43

2 Answers2

2

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.

L McClean
  • 340
  • 1
  • 8
0

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.

bk2204
  • 64,793
  • 6
  • 84
  • 100