Picking cherries

If you''re working on a feature branch, or a temporary branch for pair or mob programming, what do you do if you accidentally commit a change to the wrong branch?

Do you reset your changes, switch to the correct branch and re-create the same changes manually?

You don''t need to.

Git has a solution for this.

Create the commit as you would on the correct branch and copy the commit SHA.

Use git checkout or git switch to move to the correct branch and use git cherry-pick with the commit SHA.

It will pluck the commit from the branch and re-apply the changes with the same commit message.

Then, if you merge or rebase your temporary branch, Git will know the change has already been applied and skip that commit.

No need to re-do the same changes again manually.