# Git Cherry-Pick Saved My Messed-Up Branch — Here’s How You Can Too!

## Introduction

A practical, real-world guide to understanding and applying Git **cherry-pick** with actual developer mistakes and fixes.

![](https://media3.giphy.com/media/v1.Y2lkPTc5MGI3NjExcDZvaDQ0bWxtc3g0ZjBnaXJwdTNjdnc1MXE1MGFkdGJub2tzanhkMSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/VrTtTArIazPiB6kbFm/giphy.gif align="center")

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

`git cherry-pick` lets you **selectively** apply commits from **one branch to another** — like copying specific code changes **without merging entire branches**.

## **Now let’s understand a quick comparison** `cherry-pick` **vs** `merge` vs `rebase`

| Command | What it Does | When to Use |
| --- | --- | --- |
| `git cherry-pick` | Apply specific commits to current branch | You want *only* certain commits, not the full branch |
| `git merge` | Combine entire branch history into current branch | You want to bring all commits and preserve history |
| `git rebase` | Move/Replay commits onto another base branch | You want a clean, linear history, avoid merge commits |

**TL;DR:**

* Use `cherry-pick` for precision, specific commits only
    
* Use `merge` to combine full branch history
    
* Use `rebase` to clean history and avoid unnecessary merges
    

## **Why You Might Need** `cherry-pick`

1. You committed on the wrong branch (happens more than we admit)
    
2. You need a bug fix from one branch to another
    
3. You want to copy only certain commits, not merge everything
    
4. You made changes, reverted them locally, but commits got mixed up
    

## **My Real-Time Scenario: How I Discovered** `cherry-pick`

Let me share how I personally ran into this:

> I made some changes in a branch (let's call it `branch A`) but accidentally reverted the commits locally, leaving them in the unstaged area. Later, I re-committed those changes — but this time, I was working on `branch B`, where I only wanted my latest changes, not the leftover ones from `branch A.`

I pushed the branch to GitHub, went to open the PR, and BOOM — I saw two commits:

* One commit with my intended change for `branch B`
    
* But another unintended commit sneakily came from **branch A**
    

Total mess, right? But no worries — I fixed this cleanly using `git cherry-pick`.

I removed the wrong commit from `branch B`, stayed on `branch A`, cherry-picked only the commit I actually needed onto `branch B`, and pushed it again. Clean history, problem solved.

## **Step-by-Step Practical Guide to** `git cherry-pick`

### **1\. View Commit History with** `git log`

```bash
git log --oneline --graph --all
```

✅ This shows a simple, visual commit history across all branches.

**Tip:** If you're working on a shared repository with your team and didn't create your own branch yet, you can check history of a specific branch like:

```bash
git log <branch-name> --oneline --graph --all
```

**Example:**

```bash
git log main --oneline --graph --all
```

✅ Use this to find the commit hash (`abc123`) you want to cherry-pick.

✅ Copy that hash — you'll need it in the next step.

### **2\. Switch to Your Target Branch**

Before you apply the commit, make sure you're on the correct branch:

```bash
git status
```

This shows your current branch.

If unsure, list all branches:

```bash
git branch
```

Switch to your target branch:

```bash
git checkout <your-branch-name>
```

**Example:**

```bash
git checkout feature/login
```

### **3\. Cherry-Pick the Commit**

```bash
git cherry-pick <commit-hash>
```

✅ This applies only the specific commit to your current branch, without merging the entire source branch or bringing unrelated changes.

**Example:**

```bash
git cherry-pick abcd1234
```

If the commit applies cleanly, you're done. If there's a conflict, Git will pause and you resolve it manually.

## **Cherry-Pick Multiple Commits**

One by one:

```bash
git cherry-pick <commit1> <commit2>
```

Range of commits:

```bash
git cherry-pick commitA^..commitB
```

## **Handling Conflicts During Cherry-Pick**

Conflict? No stress. Git pauses:

```bash
CONFLICT (content): Merge conflict in file.js
```

Resolve it:

```bash
git add .
git cherry-pick --continue
```

Or abort:

```bash
git cherry-pick --abort
```

## You can also follow this short youtube video if you want:

%[https://www.youtube.com/watch?v=i657Bg_HAWI] 

## **Final Thoughts**

Mistakes happen — commits go to the wrong place, branches get mixed up.

`git cherry-pick` is the clean, targeted way to:

✅ Move specific commits

✅ Fix accidental commits

✅ Avoid messy merges

Once you try it practically, you’ll use it confidently — like I did when I accidentally messed up branches.

### Thanks for reading guys…
