Cherry picking in git is a relatively advance way to bring in the code from a single commit into your current working branch.
Why might you want to do this? Well, a good example in our class is that I forgot to add some code to the .eleventy.js file in the eleventy-basic repository template I made on GitHub. Many of you created your projects from this. If I update that now, which I did, you won’t get the changes unless you do something like use cherry pick.
Example: Update eleventy-basic with git cherry-pick
Steps to update your file
- Open Visual Studio Code to your project that is based on eleventy-basic.
- Open the terminal
- Add my repository as a remote by putting the following in the Terminal:
git remote add stein-repo https://github.com/profstein/eleventy-basic.git
- Run git fetch to bring down the info on my repo. type in terminal:
git fetch stein-repo
- Make sure you’re on the right branch. Create a new one if you need to. This way if you mess it up it won’t effect anything else. You can copy below into the terminal to make a new branch.
git checkout -b pick-stein
- Run the cherry-pick. Again paste in template
You should see a new commit in your history with my comment: “set nunjucks as template lang, install ja-yaml eleventyNavigation”
It changes .eleventy.js, package.json and package-lock.json.
If everythign worked out then merge this back into main
git checkout main
git merge pick-stein
You will also need to run npm install as I added some modules you might need as part of this commit.
Here is the process again with just the git commands
- git remote add stein-repo https://github.com/profstein/eleventy-basic.git
- git fetch stein-repo
- git checkout -b pick-stein
- git cherry-pick df9e35e
- git checkout main
- git merge pick-stein
- npm install
Here is a video tutorial I made that talks through the process more generally.
Here is the Miro file I use in the tutorial: https://miro.com/app/board/uXjVObNvJA4=/?invite_link_id=473752031096