Using git to cherry-pick a set of changes from a branch to a new branch on b4b-dev for only some files #3665
ekluzek
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Introduction
Making small commits can make it easy to move just a few changes from one branch to another without bringing the entire branch of changes in. When it's just a few commits this is easy to do. You want to go through them in reverse order, so oldest first.
However, you might have a case where a longer term branch has a lot of changes and a lot of commits -- and you only want to bring changes for a few select files -- cherry-picking the right commits is a bigger deal.
Example, cherry-picking commits for some files on mpi_scan branch to another b4b-dev branch
Here I give the process I used for cherry-picking the commits for a few specific files from my mpi_scan ($BRANCH) 00branch to a new b4b-dev ($BASE) branch I'll call "decomp_mpi_scan_move_to_b4b" ($MERGE_BRANCH). Here I want a new branch to $BASE that brings in some of the changes on the $BRANCH branch for just some files. There's 169 commits on the $BRANCH branch, but I only want some of those to a few of the files. As you see below the list of commits I want to bring over is 42, which is still a bunch. And the reason to have some automated process to do it rather than identify all of them by hand.
Here's the basic sequence:
Environment variables to use in later steps
Merge branches to the same point
I want the new branch to come to b4b-dev, so I'm going to update the base branch to it, and then use the latest b4b-dev as the comparison.
Figure out list of commits
Do a git log for the files on the branch and on the base branch and compare
The branchlog.diff file then has the list of commits that are different in the branch.
Verify the list is right, and remove merges
The branchlog.diff file should just have a list of "<" differences. So you can go through the file and make sure that's the case. The other thing that might be in there are merges to the $BASE branch and/or other branches that you probably don't want in your cherry-pick operation. To remove them you can search for 'Merge: " and remove the commits that have to do with a merge. There might be merges you want to keep, but you'll need to include an "-m" option in the final cherry-pick operation below. The kind of merges you might keep are for branches that weren't merged to $BASE and are only on the $BRANCH.
At this step I also removed commits that I didn't want to come into $BASE. For example, I removed several commits that had to do with memory handling that I didn't want to bring into $BASE.
One way to check the list of commits is to use "git show" to show what files are changed, and/or show the files modified to make sure you are getting the right changes.
[! NOTE]
While doing the above I noticed a commit that also editted clm_initialize so I used the show on it to make sure I wanted those changes to clm_initializeMod.F90.
There were also two commits that did something to the TestDecompInit.F90 self tester, so I made sure both of those were fine.
With these I decided that I'll leave these commits in. But, may cancel the changes to this file in case they cause problems. The other changes are important so I wanted to make sure they were in place.
Do cherry-pick on list in reverse order
My final list of commits was 34, which is a fairly reasonable list to deal with. To do the cherry-pick in reverse order I use "tac" (the reverse order of UNIX cat) as follows.
git cherry-pick `tac commitlist.txt`Do continue and/or skip to move along the list of commits
After doing the initial cherry-pick above I see the following output:
This means I need to edit the file "src/cpl/share_esmf/lnd_set_decomp_and_domain.F90" for conflicts. I then continue along with...
$EDITOR src/cpl/share_esmf/lnd_set_decomp_and_domain.F90 git add src/cpl/share_esmf/lnd_set_decomp_and_domain.F90 git cherry-pick --continue!NOTE: git status also shows you what you need to do:
As you go along you may run into commits that end up being empty, in these cases you'll use the "--skip" option to remove those commits.
For this case, probably especially because there were changes I didn't want to bring in there were over 20 continues, and 3 skips, and took about 3 hours.
Verify the changes are correct and just to the files you wanted
Steps for this are:
First we want to compare the resulting cherry-pick to the $BRANCH and make sure the changes are correct for the files you intended to change and that any differences with $BRANCH are expected.
The differences I saw there had to do with comments, memory checking and for code timers. I brought in some code comments by hand that I saw missing, and I made sure the code timers were correct (they have a start and stop for a code section). So I committed those changes.
Next compare the other file that was changed in the cherry-pick. To first get the list and then compare do a
git difffrom $BASE to $CHERRYPICK_BRANCH to show that clm_initializeMod.F90 also had a few changes. And then do a diff on itgit diff $BRANCH src/main/clm_initializeMod.F90The differences were mostly what I wanted, there was just one change I moved by hand to the new branch.
The third step is to compare to the $BASE branch and make sure it looks like the correct changes are there and nothing that shouldn't be.
git diff $BASEHere I noticed that I didn't want all the changes to the timers and a few other changes that I didn't want to pull in right now.
Beta Was this translation helpful? Give feedback.
All reactions