Undoing Changes in Git: A Professional Guide to Reset, Revert, Stash & Cherry-Pick
📅 Published: Feb 2026
⏱️ Estimated Reading Time: 15 minutes
🏷️ Tags: Git Reset, Git Revert, Git Stash, Git Cherry-pick, DevOps
Overview
Mistakes are a normal part of software development.
Git provides powerful tools to undo changes safely, whether you want to discard local edits, undo commits, temporarily save work, or selectively apply changes.
This guide explains Git reset, revert, stash, and cherry-pick in a professional and beginner-friendly way.
Understanding Undo Operations in Git
Undoing changes in Git depends on what you want to undo and where the change exists.
Common Scenarios
Undo local file changes
Undo committed changes
Save unfinished work temporarily
Apply specific commits to another branch
Git offers different commands for each scenario.
git reset Explained
git reset moves the HEAD pointer and optionally updates the staging area and working directory.
Types of git reset
| Mode | Affects Commit History | Staging Area | Working Directory |
|---|---|---|---|
--soft | Yes | No | No |
--mixed (default) | Yes | Yes | No |
--hard | Yes | Yes | Yes |
git reset --soft
Moves HEAD but keeps changes staged.
Use case:
Modify the last commit message or regroup commits.
git reset --mixed
Unstages changes but keeps files intact.
Use case:
Re-stage changes differently.
git reset --hard
Completely removes commits and changes.
⚠️ Warning: This permanently deletes changes.
git revert Explained
git revert creates a new commit that reverses an earlier commit.
When to Use git revert
Undo changes in shared branches
Maintain safe commit history
Production environments
👉 git revert is preferred in teams.
git reset vs git revert
| Feature | Reset | Revert |
|---|---|---|
| History Rewrite | Yes | No |
| Safe for Teams | No | Yes |
| Creates New Commit | No | Yes |
| Best Use Case | Local cleanup | Shared branches |
git stash Explained
git stash temporarily saves uncommitted changes and cleans the working directory.
Common Commands
Use Case:
Switch branches without committing unfinished work.
git cherry-pick Explained
git cherry-pick applies a specific commit from one branch to another.
Use Case:
Apply bug fix to multiple branches
Selective commit transfer
Choosing the Right Command
| Scenario | Recommended Command |
|---|---|
| Undo local commit | git reset |
| Undo shared commit | git revert |
| Save work temporarily | git stash |
| Copy specific commit | git cherry-pick |
How These Commands Help DevOps Teams
These commands enable:
Safe rollback in CI/CD pipelines
Controlled production fixes
Efficient release management
Reduced deployment risks
Hands-on Git recovery workflows are practiced in SKY Tech DevOps programs:
👉 https://devops.trainwithsky.com
Best Practices for Undoing Changes
| Best Practice | Reason |
|---|---|
Avoid --hard on shared branches | Prevent data loss |
| Use revert for production | Safe history |
Check git status before undo | Awareness |
| Backup before risky operations | Safety |
Frequently Asked Questions (FAQs)
Is git reset --hard dangerous?
Yes. It permanently deletes changes.
Should I use reset or revert?
Use reset locally, revert in teams.
Does stash save untracked files?
Only with:
Can cherry-pick cause conflicts?
Yes, if changes overlap.
Where can I practice Git recovery scenarios?
Practice safely with real workflows at SKY Tech:
👉 https://devops.trainwithsky.com
Conclusion
Undoing changes in Git is a critical skill for developers and DevOps engineers.
Understanding when and how to use reset, revert, stash, and cherry-pick ensures safer workflows and confident development.
Recommended Next Reads
Advanced Git Commands
Git Workflow Strategies
GitHub Pull Requests & Reviews
No comments:
Post a Comment