Skip to content

Instantly share code, notes, and snippets.

@imrankabir02
Last active April 22, 2025 10:51
Show Gist options
  • Select an option

  • Save imrankabir02/65c9a260ace93dddeb9cbd43d2345784 to your computer and use it in GitHub Desktop.

Select an option

Save imrankabir02/65c9a260ace93dddeb9cbd43d2345784 to your computer and use it in GitHub Desktop.
GitHub Workflow: Cherry-Pick & Merge to a Specific Branch

πŸ“ GitHub Workflow: Cherry-Pick & Merge to a Specific Branch

πŸ“Œ Purpose

This guide provides a detailed workflow for cherry-picking individual commits and merging entire branches into a specific target branch using Git and GitHub. Useful when:

  • You need to bring selected commits from one branch into another.
  • You want to fully merge the contents of a feature branch into a base branch.
  • You're collaborating via GitHub and want to manage changes efficiently.

🧰 Prerequisites

  • Git installed on your machine
  • GitHub repository access
  • Branches already created
  • Familiarity with terminal or Git client

πŸ”€ What is Cherry-picking?

Cherry-picking means taking one or more individual commits from one branch and applying them onto another branch.


πŸ”„ What is Merging?

Merging combines the entire history of one branch into another.


βœ… Step-by-Step: Cherry-pick a Commit into Another Branch

1. Clone or fetch the latest repository

git clone https://github.com/your-username/your-repo.git
cd your-repo
git fetch origin

2. Identify the commit you want to cherry-pick

You can get the commit hash using:

git log origin/feature-branch

Or use GitHub:

  • Go to the feature-branch on GitHub
  • Navigate to Commits
  • Copy the hash (first 7 characters is usually enough)

3. Checkout the target branch

git checkout main
git pull origin main

4. Cherry-pick the commit

git cherry-pick <commit-hash>

βœ… If successful:

git push origin main

⚠️ If there are conflicts:

  • Git will stop and highlight the conflict files.
  • Manually edit the conflicting files to resolve issues.
  • Then run:
    git add .
    git cherry-pick --continue
    git push origin main

πŸ›‘ To abort:

git cherry-pick --abort

βœ… Step-by-Step: Cherry-pick a Range of Commits

git checkout main
git cherry-pick <commitA>^..<commitC>
git push origin main
  • This will pick commits between A (excluded) and C (included).

βœ… Step-by-Step: Merge a Branch into Another

1. Checkout the target branch

git checkout main
git pull origin main

2. Merge the feature branch

git merge feature-branch

3. Push changes

git push origin main

⚠️ Conflict Resolution

If there are conflicts:

# After fixing conflicts manually:
git add .
git merge --continue
git push origin main

πŸ›‘ To cancel:

git merge --abort

βœ… Merge via GitHub Pull Request (PR)

  1. Push your local feature branch to GitHub:
git push origin feature-branch
  1. Go to the repository on GitHub

  2. Click Compare & pull request

  3. Make sure:

    • Base branch is the target (e.g., main)
    • Compare branch is your feature branch
  4. Add title, description, and click Create pull request

  5. After review, click Merge pull request


πŸ’‘ Best Practices

Practice Description
Use descriptive commit messages Helps in identifying cherry-picked commits
Keep branches up to date Always pull before cherry-picking or merging
Test after merge/cherry-pick Ensure no broken functionality
Avoid cherry-picking merge commits Can cause complex conflicts

πŸ“˜ Useful Commands Summary

Command Purpose
git cherry-pick <hash> Pick one commit
git cherry-pick A^..C Pick range of commits
git cherry-pick --abort Abort cherry-pick
git merge <branch> Merge full branch
git merge --abort Abort merge
git cherry-pick --continue After conflict resolution
git merge --continue Continue merge after resolving conflicts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment