A commit lost in the wrong branch (git cherry-pick case) [Git-2]

I work on an Android project and I made a mistake. I have committed all my work on the wrong branch ! Now I want to repair it and I will use some git commands to achieve that goal. These commands are cherry-pick and rebase.

This article is the first of three where I explain how to correct this. The others are available here:


The current situation

I have 5 branches but the 3 most important ones are:

  • develop
  • feature/diagrams
  • feature/DesignBeautifulReceptionScreen



git log


As you may imagine, I didn’t want to commit every single commit on the feature/diagrams branch.



Instead of that, I want to create a new branch feature/README where I will put the commit 363edc4 (marked in red in the previous screenshot), and merge some eventual divergences.

In phase 2, I also want to split the bd0e153 (“Update the TO READ section.”). Indeed the first part of the modifications needs to be put on feature/README, and the others one is an Android Java code which is intented to the branch feature/DesignBeautifulReceptionScreen. This second objective will be done here.

Plus I want to move all the changes which were made after this into the correct branch (aka feature/DesignBeautifulReceptionScreen). This third objective will be done here.

  1. Create feature/README,
  2. cherry-pick commit 363edc4 from feature/diagrams to feature/README,
  3. resolve potential conflicts,
  4. Split commit bd0e153 and cherry-pick,
  5. move other commits to feature/DesignBeautifulReceptionScreen,
  6. clean feature/diagrams.


Steps 1&2 – Create branch feature/README and cherry pick

> git checkout develop
> git checkout -b feature/README
> git cherry-pick 363edc4


The cherry-pick applies an existing commit or range of commits to another branch. Here I’am “picking” a commit localized on the feature/diagrams branch to create a brand new commit on the feature/README branch.

But as I have merge conflits, I have to resolve it !


Step 3 – Resolve conflicts

I changed the README.md file in this commit and in a following one. As some modifications were made at the same places, Git is not able to merge automatically, resulting in a conflict which need a human intervention.

To resolve it, I edit the file in order to make my corrections. Once it is done, I add the README.md to the staged area with the “git add” command. And I finish by a “git cherry-pick –continue” command.

git add README.md
git cherry-pick --continue




It was also possible to finish by a “git commit” command. The both being equivalent.

git add README.md
git commit



git log after cherry-pick

The result is a brand new local feature/README branch



Push to remote

I can now push it to my remote.

git push origin feature/README




Next steps

In the second article of this serie, I will dig out another commit which need to be split in two. Then cherry-pick one of his half.

In the third, I will move all the other commits to the already existing branch feature/DesignBeautifulReceptionScreen. Plus I will clean the feature/diagrams branch.


Leave a Reply