0

I am new to git. I have mistakenly done a commit in a wrong branch B. It should be in branch A.

Is there any way I can move the commit from B to A.

Please suggest. Any help is highly appreciated.

Prithviraj Mitra
  • 11,002
  • 13
  • 58
  • 99
  • Possible duplicate of [Move the most recent commit(s) to a new branch with Git](https://stackoverflow.com/questions/1628563/move-the-most-recent-commits-to-a-new-branch-with-git) – phd Jun 21 '18 at 15:40

2 Answers2

2
  • 1.- The first thing will be list the commits with :

    git log --oneline
    
  • 2.- You must find the commit (from branch B) and select the ID of the commit to change

  • 3.- Change the branch

    git checkout A
    
  • 4.- And from there you must execute the following commit

    git cherry-pick c8dc73f
    

    Where c8dc73f is the number or ID of the commit that I selected in the second step.

Nicolás Alarcón Rapela
  • 2,714
  • 1
  • 18
  • 29
0

You can use cherry-pick:

Let say you have two branch A and B and mistakenly done a commit in a wrong branch B

Take commit it from the branch B using

 git log 

Got to branch A using

git checkout <branch-name> 
git checkout A

Now do cherry-pick using:

git cherry-pick <commit-id>

If you get conflicts resolve it and do git add and then commit.If you want to abort cherry-pick use

git cherry-pick --abort
Mohammad Raheem
  • 1,131
  • 1
  • 13
  • 23