Git Cherry-pick update for Downstream Repos¶
git cherry-pick is a handy tool to directly apply specific commits from one branch or repo to another even when they don't share a git history—especially when you do not want to merge large change sets. This can be helpful when forking another repo or building off an evolving template, such as the Collaborative Distributed Science Guide. Below, we provide a step-by-step guide to updating repositories based on updating this repo from the template guide:
Be Prepared!
Before you start, you should know which commits are going to be pulled from the template repo. Collect their hashes in a separate text file; be sure to list them in chronological order, so they can be applied correctly.
- Ensure the target repo is up-to-date.
-
Create a new branch onto which to pull the changes:
-
Check the remotes available for your repo:
Note
If you haven't added the template repo as a remote yet, you will only see the current repo options (
origin):origin git@github.com:Imageomics/Imageomics-guide.git (fetch) origin git@github.com:Imageomics/Imageomics-guide.git (push)In which case, run the following to add the template guide as an available remote under the title
upstream:After running
git remote -v, you should then see -
Run
git fetch upstreamto get the commits from the template repo (now recognized asupstream). -
Run
git cherry-pick --edit <first-commit-hash>, this way, the URL pointing to the Collaborative Distributed Science Guide can be modified to function properly from the downstream repo. Ex:The next screen should provide the commit message for editing:
Update GitHub Repo Archiving Guidance (#29) * Add section on automatically maintaining metadata on Zenodo ...press I, then, using arrow keys to navigate the console edit the message to the following:
Update GitHub Repo Archiving Guidance Pull from Collab Guide [PR 29](https://github.com/Imageomics/Collaborative-distributed-science-guide/pull/29) * Add section on automatically maintaining metadata on Zenodo ...The URL will render as "Imageomics/Collaborative-distributed-science-guide#29", with the functional hyperlink. Finally, select Esc and type
:wqto complete the commit message edit.Conflicts Happen
If you have a merge conflict, open the file, resolve the conflict, and then
git addthe file. From that point you should be able to rungit cherry-pick --continueand it will provide the commit message from the upstream. Checkinggit statusandgit logat various points in this process will allow you to check on how these are progressing and see the addition of the upstream commits to your current repo's branch. -
Once you've collected all the upstream commits, run
git push --set-upstream origin devto add them to the current repo. -
Open a pull request from the
devbranch to add these upstream commits tomain. Be sure to include a description of the commits, where they came from, and include links to PRs from the upstream repo. Auto-links generated by GitHub (based on#<issue/PR-number>) will link to that number issue or PR in the current repo, not the upstream one. -
Rebase commit the PR. This allows for the changes pulled from upstream to be seamlessly integrated into the downstream repo. The commit hashes are not preserved across repositories, so there is no information to lose.
See also git cherry-pick for more info on available options.