Git Cherry Pick

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

  1. Open Visual Studio Code to your project that is based on eleventy-basic.
  2. Open the terminal
  3. Add my repository as a remote by putting the following in the Terminal:
    1. git remote add stein-repo https://github.com/profstein/eleventy-basic.git
  4. Run git fetch to bring down the info on my repo. type in terminal:
    1. git fetch stein-repo
  5. 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.
    1. git checkout -b pick-stein
  6. Run the cherry-pick. Again paste in template
    1. git cherry-pick df9e35e

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

  1. git checkout main
  2. 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.

npm install

Here is the process again with just the git commands

  1. git remote add stein-repo https://github.com/profstein/eleventy-basic.git
  2. git fetch stein-repo
  3. git checkout -b pick-stein
  4. git cherry-pick df9e35e
  5. git checkout main
  6. git merge pick-stein
  7. npm install

Video Tutorial

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

Leave a comment