Using Git Cherry Pick
Problem: You want to test a feature someone has developed, but it only exists in a remote branch which is woefully out of date.
Example: Bug 3475, an enhancement from PTFS's "Harley" branch which adds the ability to copy an existing patron record when creating a new one. Checking out the branch and rebasing against origin will fail because the branch is so far out of date.
Solution: Use git cherry-pick
Check out the branch you want to cherry-pick from
Harley bugfix branches have been added to the main Koha git repo, so you don't have to add a new remote repo. There we can find a branch for Bug 3475.
git checkout ptfs/Bug3475
git will warn you "You are in 'detached HEAD' state," but we're just checking out the branch to check the log of commits.
Find the commits to cherry-pick
git log --pretty=oneline
Outputs:
3e86ed85fd595c99bd73292553ef1d09d95e53ae Added Copy Patron button to circ-toolbar ab97540637691bd73581fed3828c164191c9e0bb Cleared the extended patron attributes from being copied when adding 04566389ae36651daf3dfa117a1088d594632370 Added a copy patron link from moremembers page 595e6a190b9e90cf1371ac0b853999286948c852 Merge branch 'Bug3789' into ptfs-master fa59e2f7480dc63d7824c5587c82c3f6df3b96ae Bug 3789 Set off shelving location in staff and OPAC title display 38ddef75baeb3f3bd4487cfc25340be9dc2baa4c Merge branch 'Bug3514' into ptfs-master 5d3b6945764cd2a292f822963feca9cf12776605 Extends Bug 3514 syspref to control whether the Return column is acti 279dab8f631ba7aa44b8a35da7f5d43b24d69924 Bug 3514 syspref to control whether the Return column is active in Pa 1f1540f0218d5ad8348700f74c60ad7ca95625b3 Bug 4215 OPAC "Search for title in" can fail with trailing slashes in 6e0cc4061e82c2f861aef1a0db584502c9a3268e Merge branch 'Bug4243' into ptfs-master
Looking at the history in the branch we have to find the earliest change relevant to the feature we want to cherry-pick. If we're lucky we'll be looking at a clean branch that diverges from origin/main for that particular feature/bugfix. In this example we have to make a guess at where the Bug 3475 fixes begin. Probably with 'Added a copy patron link from moremembers page'
Create a clean new branch to work in
Open another terminal window, leaving this one open to the git log output for reference, and create a new branch based on main to cherry-pick into:
git checkout -b bug-3475 origin
Start cherry-picking
Then we'll start the cherry-pick process with the earliest commit relevant to Bug 3475:
git cherry-pick 04566389ae36651daf3dfa117a1088d594632370
In this case the commit applies cleanly and we can proceed to the next ones:
git cherry-pick ab97540637691bd73581fed3828c164191c9e0bb git cherry-pick 3e86ed85fd595c99bd73292553ef1d09d95e53ae
Resolve conflicts
On the last one we hit a snag:
error: could not apply 3e86ed8... Added Copy Patron button to circ-toolbar hint: after resolving the conflicts, mark the corrected paths hint: with 'git add <paths>' or 'git rm <paths>' hint: and commit the result with 'git commit -c 3e86ed8'
Unfortunately it doesn't tell us what the problems are, so we type:
git status
On branch bug-3475 Your branch is ahead of 'origin/main' by 2 commits. Unmerged paths: (use "git reset HEAD <file>..." to unstage) (use "git add/rm <file>..." as appropriate to mark resolution) both modified: koha-tmpl/intranet-tmpl/prog/en/includes/circ-toolbar.inc
Now we can see that circ-toolbar.inc is the file with the conflict. Open the file, look for the standard "<<<<<<< HEAD" marker and edit as necessary. When finished:
git add koha-tmpl/intranet-tmpl/prog/en/includes/circ-toolbar.inc
And then:
git commit -c 3e86ed8
Notice we're following the instructions for git commit as output in the conflict message. This will launch your commit message editor, but you can save without making changes. If this is the last commit to cherry-pick, you're done! It's time to test and make any corrections which are necessary.