Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy large amount of commits to new branch

Tags:

git

I have three branches:

  • releaseX
  • develop
  • topicA

releaseX was branched from develop maybe two weeks ago.

topicA was branched from develop a long time ago. Because we knew it was going to get merged into a release branch eventually, we kept merging develop into it. Unfortunately, we now have a good amount of commits in develop that we don't want in our releaseX branch.

Here's a diagram of my scenario:

      (releaseX)
     /
A---B---C---D---E---F---G---H (develop)
                     \
                      S---T---X---Y (topicA)

Above, I need to keep commits like S T X Y, but need to get rid of C and E. The number of commits in question is about 50 or more, but clearly I couldn't illustrate them here.

My goal is to create a branch based off of releaseX with 50+ relevant commits from topicA. I've tried creating a new branch off releaseX and then rebasing the new branch on topicA, but this didn't allow me to exclude the commits I didn't want in there.

What is the best way to "cherry pick" these 50+ commits into a new branch (topicA_clean) based off releaseX?

like image 854
Fillip Peyton Avatar asked Mar 07 '23 05:03

Fillip Peyton


1 Answers

Here a solution that uses git cherry-pick (works with git --version >= 1.7.2):

It is possible to cherry-pick a list of commits with ease:

$ git cherry-pick -x <commit-hash-1> <commit-hash-2> <...> <commit-hash-X>

I like to use the -x option to be able to find the original commits later on


If you happen to have a list of the commits you want to cherry-pick you can use a tiny bit of command line magic like so:

$ cat cherry-pick-hashes.md | xargs git cherry-pick -x
like image 172
AnimiVulpis Avatar answered Mar 14 '23 20:03

AnimiVulpis