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.

1

2 Answers 2

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.

4
  • Your answer could be improved with some explanation of what this does and how it solves the OPs problem.
    – user47589
    Commented Jun 21, 2018 at 13:52
  • @Amy I'm finish Commented Jun 21, 2018 at 13:57
  • Nice explanation. All good now. The commit has been copied from B to A. Now my next task is to delete/remove that commit from branch B. Commented Jun 21, 2018 at 14:15
  • @PrithvirajMitra This commit is the ultime ? Commented Jun 21, 2018 at 14:16
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

Not the answer you're looking for? Browse other questions tagged or ask your own question.