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:
- 2 – [will be released soon] Split an old commit in N commits
- 3 – [will be released soon] All my commits are on the wrong branch ! (git rebase –onto case).
The current situation
I have 5 branches but the 3 most important ones are:
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.
- Create feature/README,
- cherry-pick commit 363edc4 from feature/diagrams to feature/README,
- resolve potential conflicts,
- Split commit bd0e153 and cherry-pick,
- move other commits to feature/DesignBeautifulReceptionScreen,
- 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
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.