DEV Community

Mahmud Hasan Rafid
Mahmud Hasan Rafid

Posted on

How Git Cherry-Pick Saved My Day: A Tale of Branch Chaos 🍒

Ever had one of those moments where a single command turns disaster into triumph? That was me today, through a maze of Git branches.

The scene:

  • Three branches: main (production), test (staging), and feature
  • Main branch lagging behind the test
  • Feature branch spawned from an outdated main
  • PRs flowing from feature to test
  • Conflict resolution on GitHub’s web interface (pro tip: avoid this if possible!)
  • Post-conflict feature branch: a mix of my work and others, looking like the test branch

The challenge: My feature branch now had commits from the test branch, making it impossible to merge directly into the production (main) branch.

Enter git cherry-pick, the unsung hero of version control. Here’s what I did:

  1. Identified my commits in the messy feature branch
  2. Created a new branch from the updated main
  3. Cherry-picked my commits from the old feature branch into the new one

Result? I got a new updated feature branch branching from the production branch with only my changes, merge crisis averted, and valuable lessons learned:

  1. Keep branches updated regularly
  2. Resolve conflicts locally when possible
  3. Make small, focused commits
  4. Master cherry-picking for precise code management

Remember folks: In the orchard of code, sometimes you need to carefully pick your cherries instead of shaking the whole tree. 🌳🍒

Top comments (0)