TekOnline

Git Revert Cheat Sheet

Basic Revert Operations

Single Commit Revert

# Revert and commit immediately
git revert <commit-hash>

# Revert but stage changes only (no automatic commit)
git revert --no-commit <commit-hash>   # or -n

Multiple Commit Reverts

# Revert multiple specific commits
git revert <commit1-hash>
git revert <commit2-hash>

# Revert a range of commits
git revert --no-commit <oldest-commit>..<newest-commit>
git commit -m "Reverted multiple commits"

Working with Staged Reverts

Review and Modify

# See what's staged
git status
git diff --staged

# Unstage specific files
git restore --staged <file>

# Stage modified files
git add <file>

Managing Reverts

# Abort a revert operation
git restore --staged .  # unstage everything
git revert --abort     # abort current revert

# Complete the revert
git commit -m "Reverted changes from commit xyz"

Backup Strategies

Create Safety Branch

# Create backup before major reverts
git branch backup-branch

# Return to backup if needed
git checkout backup-branch

Cherry-pick Changes Back

# Cherry-pick specific commits from backup
git cherry-pick <commit-hash>

# Or merge all backup changes
git merge backup-branch

Tips

  • Always create a backup branch before major reverts
  • Use --no-commit flag to review changes before committing
  • Commit messages should clearly indicate what was reverted
  • Use git log --oneline to find commit hashes easily

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *