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 a1b2c3dResult:
[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 a1b2c3d2. 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 z9y8x7wResult:
[main b5c4d3e] Revert "Add broken feature"
This reverts commit z9y8x7w123...
2 files changed, 0 insertions(+), 12 deletions(-)3. Revert vs. Reset (DevOps Best Practice)
| Tool | Action | Use Case | Shared History? |
|---|---|---|---|
| Reset | Deletes commits from history | Fixing local mistakes | Dangerous! (Never use on main) |
| Revert | Adds a new "undo" commit | Undoing shared code | Safe |
4. Resolving Conflicts
During a cherry-pick or revert, conflicts can happen.
Action:
- Resolve the conflicts in the files.
git add <files>git cherry-pick --continue(orgit revert --continue)
Summary: The Hotfix Workflow
- Find the commit ID on your dev branch.
- Switch to the production/main branch.
- Use
git cherry-pick <commit-hash>. - Push to production.