Git
Version Control
Commit
Merge
Programming

How to merge a specific commit in Git

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

When you need one change from another branch without merging everything, the right Git tool is usually cherry-pick. This pattern is common for release backports, hotfixes, and selective patch transfers. A safe workflow includes commit inspection, conflict handling, and traceability.

Confirm the Exact Commit First

Before applying anything, identify and review the commit you want.

bash
git checkout feature-branch
git log --oneline --decorate

Inspect patch content:

bash
git show <commit_sha>

Do not skip this review. Many commit messages are broader than the actual code change and may include side effects.

Apply One Commit with Cherry Pick

Switch to target branch and apply selected commit.

bash
git checkout release-1.4
git pull --ff-only origin release-1.4
git cherry-pick <commit_sha>

For better audit history, include original reference automatically:

bash
git cherry-pick -x <commit_sha>

-x appends source hash info to commit message, which helps future debugging.

Resolve Conflicts Correctly

If branches diverged, conflicts may occur. Handle them in a controlled sequence.

bash
git status

Resolve files, stage, then continue:

bash
git add <resolved_file>
git cherry-pick --continue

If you need to cancel:

bash
git cherry-pick --abort

After conflict resolution, run tests in target branch context before pushing.

Pick Multiple Commits Safely

When change spans several commits, you can pick a range or specific list.

Range example:

bash
git cherry-pick <start_sha>^..<end_sha>

List example:

bash
git cherry-pick <sha1> <sha2> <sha3>

Prefer explicit lists when commits are not contiguous or contain unrelated edits.

Use No Commit Mode for Fine Control

Sometimes you want to inspect or edit patch before committing.

bash
1git cherry-pick --no-commit <commit_sha>
2git diff
3git add -p
4git commit -m "Backport selected parts of <commit_sha>"

This is useful when target branch needs a subset of the original change.

Validate History and Push

After picking, confirm local history and verify build integrity.

bash
git log --oneline -n 5
git diff HEAD~1..HEAD

Then push branch:

bash
git push origin release-1.4

If using pull requests, mention original commit hash and rationale so reviewers understand scope.

When Merge Is Better Than Cherry Pick

Cherry pick is ideal for isolated fixes. Use branch merge when changes are tightly coupled and you need full context.

Choose merge if:

  • many dependent commits are required.
  • branch is already review approved for full integration.
  • preserving branch history is important.

Choose cherry pick if:

  • release branch needs one targeted fix.
  • full branch includes unfinished work.
  • backport scope must stay minimal.

Backport Checklist for Teams

Before applying patches to release branches, use a checklist:

  1. Review patch content and dependencies.
  2. Confirm target branch compatibility.
  3. Run tests after cherry pick.
  4. Record original commit reference.
  5. Ensure rollback strategy exists.

Checklists reduce emergency mistakes during production incidents.

Special Case: Cherry Picking Merge Commits

If the source commit is a merge commit, Git needs a mainline parent reference.

bash
git cherry-pick -m 1 <merge_commit_sha>

Use this only when you understand parent context. In many cases it is safer to cherry pick the underlying non merge commits instead of replaying an entire merge diff.

Common Pitfalls

  • Cherry picking without inspecting actual patch content.
  • Picking commits with hidden dependencies on earlier changes.
  • Forgetting traceability flags or notes.
  • Resolving conflicts quickly without rerunning tests.
  • Reapplying already picked commits and creating duplicate logic.

Summary

  • Use git cherry-pick for selective commit transfer.
  • Inspect source commit before applying to target branch.
  • Resolve conflicts carefully and validate behavior with tests.
  • Use -x for clear provenance.
  • Prefer full branch merge when change dependencies are broad.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.