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-2-1-git-branch

 

git log

git-2-2-introduction-part-1git-2-3-introduction-part-2

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

 

Objectives

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

git-2-4-create-branch-and-cherry-pick

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

git-2-5-git-add-readme-and-cherry-pick-continue

git-2-6-git-commit-readme

 

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

git add README.md
git commit

git-2-7-git-add-readme-and-commit

 

git log after cherry-pick

The result is a brand new local feature/README branch

git-2-8-git-log-after-cherry-pick-part-1git-2-9-git-log-after-cherry-pick-part-2

 

Push to remote

I can now push it to my remote.

git push origin feature/README

git-2-10-git-push-origin-feature_readme

git-2-11-git-log-after-push-origine-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