Skip to content

Commit

Permalink
Pull request code review updates
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Fisher <[email protected]>
  • Loading branch information
rmarow and mfisher87 committed Sep 28, 2023
1 parent 9ee1d46 commit cd8b57c
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions posts/git-cherry-pick/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
title: "Cherry-Pick on GitHub"
author: "Robyn Marowitz"
date: "2023-09-26"
description: "Committing a mistake in GitHub can feel so scary, cherry picking is a tool to help you fix your history when something like that happens. This post lays out simple and clear instructions on the cherry picking method."
description: "Committing a mistake in GitHub can feel so scary. Cherry picking is a tool to help you fix your history when something like that happens. This post lays out instructions on the cherry picking method."
categories: [github, open-science]
image: "cherry_pick.jpeg"
---

## What is git cherry-pick?
## What is `git cherry-pick`?

It is the act of picking a commit from one branch and applying it to another branch.
Read more in the [official docs](https://git-scm.com/docs/git-cherry-pick)

## When should I use cherry-pick?
## When should I use `git cherry-pick`?

Cherry-Pick is best used when a change has been committed onto a different branch than desired. You can *pick* commits one by one and move them to the desired branch.
`cherry-pick` is best used when a change has been committed onto a different branch than desired. You can *pick* commits one by one or by choose a range and move them to the desired branch.

`git cherry-pick` usually brings a commit from somewhere else and applies it on top of your current branch, recording a new commit, while `git rebase` takes your current branch and rewrites a series of its own tip commits in one way or another.

## Git command/syntax:

When using cherry pick it is very important to look at your git log and understand what is gong on.
When using cherry pick it is very important to look at your git log and understand what is going on.

First it can be useful to look at the branches you have. I did this with the `git branch` command and as you can see the one with the asterik and in green is the branch I am currently on.

Expand All @@ -30,33 +31,31 @@ I use `git lola` which is an alias: `lola = log --graph --decorate --oneline --
![](lola-1.png)


Here you can see that I have my `HEAD` at `cherry-pick-post`. I have made changes to the about section which I actually wanted to do on the main branch. So I will use `git cherry-pick` to make that happen. First you want to be sure you are on the branch that you want to put the commit on.
Here you can see that I have the `cherry-pick-post` branch checked out, as indicated by the `HEAD` ref. I have made changes to the about section which I actually wanted to do on the main branch. So I will use `git cherry-pick` to make that happen. First you want to be sure you are on the branch that you want to put the commit on.

![](status-on-cpp-branch.png)

I can see I am on the `cherry-pick-post` branch so I will use `git switch` to go to the main branch.
I can see I am on the `cherry-pick-post` branch but I want the new commits to be created on `main`, so I will use `git switch` to go to the `main` branch.

![](switch-main.png)

Now I am on the desired branch so I can go ahead and `cherry-pick`

I want to use the syntax `cherry-pick <commit>`
Now I am on the desired branch so I can go ahead and cherry-pick using the syntax `git cherry-pick <commit>`.

![](pick-commit-1.png)

To see what thisdid we will once again do `git lola`
To see what this did we will once again do `git lola`:

![](lola-after-pick1.png)

Now the `Update about page` commit is on both the `cherry-pick-post` branch and the `main` branch. The commit ID is a new ID on the main branch.
Now the `Update about page` commit is on both the `cherry-pick-post` branch and the `main` branch. Note that the commit ID on the `main` branch is a new ID, but the original commit on `cherry-pick-post` branch has the same ID as before.

We will now push and look at the gt log again.
We will now push and look at the log again.

![](push-and-lola.png)

At this point `main` appears exactly as we want it to, but `chery-pick`-post` still has the unwanted commit.
At this point `main` appears exactly as we want it to, but `chery-pick-post` still has the unwanted commit.

So at this point we will once again use the cherry-pick command to mve forward. I will create a new branch called `cherry-pick-1` fro the main branchh.
So at this point we will once again use the cherry-pick command to move forward. I will create a new branch called `cherry-pick-1` from the main branch.

![](new-branch.png)

Expand All @@ -68,7 +67,7 @@ There are 3 commits that I want so I can actually pick them all a once.

![](pick-2.png)

GitHub gives a great summary of what is happening and shows the commits I am picking.
Git gives a great summary of what is happening and shows the commits I am picking.
To see where I am at I will do `git lola` once again.

![](lola-2.png)
Expand All @@ -77,10 +76,10 @@ At this point I have the 3 commits I want on my new branch so I can go ahead and

![](lola-3.png)

Now I no loger want the old branch so I will delete it using `git branch -D cherry-pick-post` this will only delete it locally, so you will need to do `git push origin :cherry-pick-post` to delete it from GitHub as well.
Now I no loger want the old branch so I will delete it using `git branch -D cherry-pick-post` this will only delete it locally, so I will also need to do `git push origin :cherry-pick-post` to delete the branch from GitHub.

Note: github will keep untracked history for 2 weeks so there is always time to alter if you decide you want to revive something.
Note: Git may keep untracked history for around 2 weeks so there is always time to alter if you decide you want to revive something.

## Summary

Cherry picking is a very powerful tool for when a user wants to copy a commit from one branch to another.
Cherry picking is a tool for copying commit(s) from one branch to another.

0 comments on commit cd8b57c

Please sign in to comment.