Practical git cherry pick example
Tags: git
Published: 2023-08-19 09:10:38

I do most of my learning when trying to solve an actual problem that I'm working on. But I thought it might be fun to randomly learn new things by reading the documentation and then trying it out. I plan to do this regularly now and write about my findings. 

I had heard about `git cherry-pick` before, but never tried it out. After learning about it, I found I could have actually used it just the other day when doing a hotfix that needed to be in both the integration branch (for testing) and the main branch (on production). 

I found the documentation a bit hard to follow in regards to `git cherry-pick`, so I asked ChatGPT to generate a practical example:

***"Generate a practical example that I can try out on using git cherry pick"***


> Below is the answer from ChatGPT that I edited a bit, in order to make things a bit clearer (e.g. in regards to creating branches and using VS Code for merging).

---

Let's consider a scenario where you have a Git repository with two branches: `feature` and `main`. You've been working on the `feature` branch, and you've made some commits that you want to selectively apply to the `main` branch using `git cherry-pick`.

Here's a step-by-step example:

**1) Create the Repository:**
Create a new directory and initialize a Git repository:

```bash
mkdir git-cherry-pick-example
cd git-cherry-pick-example
git init
```

Let's create a file on the main branch:

```bash
touch test.txt
git add test.txt
echo "First line of code" >> test.txt
git commit -m "first commit"
```

**2) Create Commits on the Feature Branch:**

Create a feature branch and make a few commits on the `feature` branch:

```bash
git checkout -b feature
   
echo "Feature update 1" >> test.txt
git commit -am "Add feature update 1"
  
echo "Feature update 2" >> test.txt
git commit -am "Add feature update 2"
```

**3) Return to the main branch**

```bash
git checkout main
```

**4) Cherry-Pick Commits:**

Now let's say you want to apply only the first commit from the `feature` branch onto the `main` branch:

Note down the commit hash of "Add feature update 1"

```bash
git log feature --oneline 
```

This will return something like this:

```bash
b245f30 (feature) Add feature update 2
93b448c Add feature update 1
d3bdfaf (HEAD -> main) first commit
```

```bash
git cherry-pick <commit-hash> #(in my case I replace `<commit-hash>` with `93b448c`
```

It now added the line "Add feature update 1" below the line "First line of code", so my file test.txt now looks like this:

```bash
First line of code
Feature update 1
```

5) **Review and Resolve Conflicts:**

If I had added something to line 2 in test.txt before doing the cherry-pick, this would have resulted in a merge conflict. Let's simulate this by first reverting our cherry pick and then adding some random text to line 2 before doing the cherry pick:

```bash
git log --oneline #copy the commit hash of `(HEAD -> main) Add feature update 1`
git revert -m 1 <cherry-picked-commit-hash> #this will revert it so that we now just have "first line of code" again inside the file
echo "Second line of code" >> test.txt
git commit -am "Added second line of code"
git cherry-pick <commit-hash> #same commit hash we used further above for getting "Add feature update 1" from the feature branch
```

This will now return the following message:
```
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
```

   If there are conflicts between the changes in the commit you're cherry-picking and the current state of the `main` branch, Git will pause the cherry-pick process and ask you to resolve the conflicts manually. 
I find it easiest to resolve conflicts in VS Code, so I open the editor with `code .`, go to source control and click on "Accept both changes".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8uybqgbrylujvmqdwt1p.png)

I save the file and then I either click on the "plus" symbol next to the file to stage it or in the terminal I can do `git add test.txt`

After resolving the conflicts, continue the cherry-pick process by running:

```bash
git cherry-pick --continue
```