Copy a single commit to another branch using cherry-pick
1 min read

Copy a single commit to another branch using cherry-pick

Bring changes to you code from one branch to another.

Today I was working on a new feature. I created a new branch feature/add-gallery-button.

While a was working on it, I noticed some code that, if improved, would have made the development process much easier. So I improved it.

Once I was done, I committed the code and pushed to the remote. I started a merge request and assigned it to another developer.

Then, after a celebration coffee, I started working on yet another new feature, feature/update-log-component, and I wanted to use the code improvements from feature/add-gallery-button.

Instead of copying the code manually, thus creating two commits in the repository history with the same change, it would have been nice to bring the commit from feature/add-gallery-button to feature/update-log-component.

This is possible with git cherry-pick.

You only need the SHA of the commit you want to copy, let's say 4350a96303c474812523c1214f50ffda1f2e437a.

Be sure to checkout the branch you are working on now.

git checkout feature/update-log-component
git cherry-pick 4350a96303c474812523c1214f50ffda1f2e437a

en voilĂ , the improved code is also on my current branch.

Subscribe to a curated newsletter

Receive an email every week with curated content about Dart and Flutter.

See previous issues of the newsletter.