Commit Rollback Methods
Understanding Commit Rollback
Git provides multiple strategies to revert or undo changes at different stages of development.
Rollback Techniques
1. Soft Reset
Moves HEAD pointer without modifying working directory
## Undo last commit, keeping changes staged
git reset --soft HEAD~1
2. Mixed Reset
Default reset mode, unstages changes
## Undo last commit, unstaging changes
git reset HEAD~1
3. Hard Reset
Completely removes commits and changes
## Permanently discard last commit and changes
git reset --hard HEAD~1
Rollback Strategies Comparison
Method |
Working Directory |
Staging Area |
Commit History |
Soft Reset |
Unchanged |
Staged |
Commits Removed |
Mixed Reset |
Unchanged |
Unstaged |
Commits Removed |
Hard Reset |
Discarded |
Cleared |
Commits Removed |
Reverting Specific Commits
Using git revert
## Create a new commit that undoes previous commit
git revert <commit-hash>
gitGraph
commit
commit
commit
revert
Advanced Rollback Scenarios
Recovering Deleted Commits
## Find lost commits
git reflog
## Restore specific commit
git checkout <lost-commit-hash>
LabEx Tip
In LabEx's Git learning environments, you can safely practice these rollback techniques without risking production code.
Best Practices
- Use soft reset for local, unpublished changes
- Use revert for published commits
- Avoid hard reset on shared branches
- Always backup important work
Key Takeaways
- Multiple rollback methods exist
- Choose method based on specific scenario
- Understand implications of each technique
- Prioritize data preservation