G
GuideDevOps
Lesson 10 of 18

Cherry-picking & Reverting

Part of the Git & Version Control tutorial series.

Git allows you to selectively apply commits from one branch to another using cherry-pick, and safely undo changes with revert. These tools are essential for hotfixes and fixing mistakes.

1. Git Cherry-pick

cherry-pick takes a commit from one branch and applies it to another. This is incredibly useful when you've fixed a bug in a development branch and want to apply it to production without merging the entire feature branch.

Basic Syntax

git checkout production
git cherry-pick <commit-hash>

Example with Result

Suppose you have a hotfix a1b2c3d on dev branch.

Command:

git checkout main
git cherry-pick a1b2c3d

Result:

[main f4e5d6c] Fix critical security vulnerability
 Date: Fri Apr 10 14:00:00 2026 +0000
 1 file changed, 1 insertion(+), 1 deletion(-)

Tip: Use -x flag to keep track of where the commit came from.

git cherry-pick -x a1b2c3d

2. Git Revert

revert creates a new commit that is the exact inverse of an existing commit. It's the safe way to undo changes in a shared repository because it doesn't rewrite history.

Basic Syntax

git revert <commit-hash>

Example with Result

You accidentally committed a broken feature z9y8x7w.

Command:

git revert z9y8x7w

Result:

[main b5c4d3e] Revert "Add broken feature"
 This reverts commit z9y8x7w123...
 2 files changed, 0 insertions(+), 12 deletions(-)

3. Revert vs. Reset (DevOps Best Practice)

ToolActionUse CaseShared History?
ResetDeletes commits from historyFixing local mistakesDangerous! (Never use on main)
RevertAdds a new "undo" commitUndoing shared codeSafe

4. Resolving Conflicts

During a cherry-pick or revert, conflicts can happen.

Action:

  1. Resolve the conflicts in the files.
  2. git add <files>
  3. git cherry-pick --continue (or git revert --continue)

Summary: The Hotfix Workflow

  1. Find the commit ID on your dev branch.
  2. Switch to the production/main branch.
  3. Use git cherry-pick <commit-hash>.
  4. Push to production.