Git reset + cherry-pick

Just a git technique that is sometimes useful. Mostly when the starting branch changes a lot, due to for example a rebase, but can be other reasons too.

You should start with a git fetch , just to have up to date origin/*.

After this, or before, you need to collect the list of commit hashes that contain the changes you made in your branch. Here’s an example.

In this case, branch RNL-4106 added 2 commits on top of RNL-4048. We ignore the merge commit – hopefully it did not have any actual changes and was a straight merge.

Then with those commit hashes obtained, we can reset the branch to the current state of the source branch with git reset --hard origin/RNL-4048 , then add back the 2 commits with git cherry-pick 36b98cae2cb970c19713cc9900f03114fa4d25a4 5687fb0366dfef9676622e99da266f77818745a1 ( note that it is in the chronological order in which the commits themselves were made )

There may be conflicts, and you’d have to resolve them for each cherry-picked commit at a time. Once you have resolved one commit’s conflicts, you run git add . && git cherry-pick --continue.

After all the commits have been cherry-picked, you run git push -f because you’ve “re-written git history”, and that requires a force push.

Hope That Helps!

Published by

Dan Bernardic

A Winnipeg Web programmer. Proud member of the Skip the Dishes team. Experienced with Web technologies, e.g. HTML5, CSS, ( Server-side ) JS, PHP, WordPress, MVC, etc.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s