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.
- Git installed on your machine
- GitHub repository access
- Branches already created
- Familiarity with terminal or Git client
Cherry-picking means taking one or more individual commits from one branch and applying them onto another branch.
Merging combines the entire history of one branch into another.
git clone https://github.com/your-username/your-repo.git
cd your-repo
git fetch originYou can get the commit hash using:
git log origin/feature-branchOr use GitHub:
- Go to the
feature-branchon GitHub - Navigate to Commits
- Copy the hash (first 7 characters is usually enough)
git checkout main
git pull origin maingit cherry-pick <commit-hash>git push origin main- 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
git cherry-pick --abortgit checkout main
git cherry-pick <commitA>^..<commitC>
git push origin main- This will pick commits between A (excluded) and C (included).
git checkout main
git pull origin maingit merge feature-branchgit push origin mainIf there are conflicts:
# After fixing conflicts manually:
git add .
git merge --continue
git push origin maingit merge --abort- Push your local feature branch to GitHub:
git push origin feature-branch-
Go to the repository on GitHub
-
Click Compare & pull request
-
Make sure:
- Base branch is the target (e.g.,
main) - Compare branch is your feature branch
- Base branch is the target (e.g.,
-
Add title, description, and click Create pull request
-
After review, click Merge pull request
| 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 |
| 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 |