Skip to content

revert Git Command Guide

The git revert command creates new commits that undo the changes introduced by one or more existing commits. Unlike reset which rewinds history, revert preserves the complete project history by adding new commits that reverse the effects of problematic commits.

Terminal window
git revert [--[no-]edit] [-n] [-m <parent-number>] [-s] [-S[<keyid>]] <commit>...
git revert (--continue | --skip | --abort | --quit)
OptionDescription
--edit, -eEdit revert commit message (default)
--no-editUse auto-generated commit message
-n, --no-commitDon’t create commit, just apply changes
OptionDescription
-m <parent-number>Specify parent for merge commit revert
--mainline <parent-number>Alternative to -m for merge commits
OptionDescription
-s, --signoffAdd Signed-off-by trailer
-S[<keyid>], --gpg-sign[=<keyid>]GPG-sign the revert commit
OptionDescription
--continueContinue after resolving conflicts
--skipSkip current revert and continue
--abortCancel revert operation
--quitStop revert but keep current state
ParameterDescription
<commit>...Commits to revert (one or more)
Command Comparison:
├── git revert: Creates new commits that undo changes (safe for shared repos)
├── git reset: Rewinds branch pointer (changes history, avoid on shared branches)
├── git restore: Resets individual files (doesn't affect commits)
├── git rebase: Rewrites commit history (changes existing commits)
└── git checkout: Switches branches/commits (can create detached HEAD)
Revert Operation:
1. Identify target commit(s) to undo
2. Create reverse patch for each commit
3. Apply reverse changes to working tree
4. Create new commit with reverse changes
5. Preserve complete project history
Safety Features:
├── History remains intact
├── Changes are traceable
├── Safe for shared repositories
├── Can be reverted itself if needed
Single Commit Revert:
├── git revert <commit>
├── Creates one revert commit
├── Straightforward operation
Multiple Commit Revert:
├── git revert <commit1> <commit2> <commit3>
├── Creates multiple revert commits
├── Can cause conflicts between reverts
├── Consider reverting as one operation
Terminal window
# Revert last commit
git revert HEAD
# Revert specific commit
git revert abc123
# Revert commit with custom message
git revert -e abc123
# Revert without creating commit
git revert -n abc123
Terminal window
# Revert multiple commits
git revert HEAD~2 HEAD~1 HEAD
# Revert range of commits
git revert abc123..def456
# Revert with auto-generated messages
git revert --no-edit abc123 def456
Terminal window
# Revert merge commit (specify parent)
git revert -m 1 abc123
# Revert merge with mainline option
git revert --mainline 1 abc123
# Check merge commit parents
git show --pretty=raw abc123
Terminal window
# Handle revert conflicts
git revert problematic-commit
# If conflicts occur:
# 1. Edit conflicted files
# 2. Stage resolved files
git add resolved-file.txt
# 3. Continue revert
git revert --continue
# Or skip this revert
git revert --skip
# Or abort entire operation
git revert --abort
Terminal window
# Analyze commits before reverting
git log --oneline -10
# Check what changes will be reverted
git show <commit-to-revert>
# Preview revert impact
git revert --no-commit <commit>
git diff --staged # See what will be reverted
git revert --abort # Cancel preview
Terminal window
# Revert multiple commits in batch
commits_to_revert=("abc123" "def456" "ghi789")
for commit in "${commits_to_revert[@]}"; do
echo "Reverting $commit..."
if git revert --no-edit "$commit"; then
echo "✓ Successfully reverted $commit"
else
echo "✗ Failed to revert $commit - manual intervention needed"
exit 1
fi
done
Terminal window
# Revert only if tests pass
git revert --no-commit problematic-commit
if run-tests.sh; then
git commit -m "Revert problematic changes"
echo "Revert successful"
else
git revert --abort
echo "Revert cancelled - tests failed"
fi
Terminal window
# Configure revert behavior
git config revert.defaultHeadName "revert" # Default revert branch name
git config revert.reference true # Use revert- prefix for references
# Configure commit message editing
git config core.editor "code --wait" # Set editor for commit messages
git config commit.verbose true # Show diff in commit message editor
# Configure GPG signing
git config commit.gpgSign true # Sign all commits
git config user.signingKey "your-key-id" # Set signing key
Terminal window
# Always check what will be reverted
git show <commit-to-revert>
# Create backup before complex reverts
git branch backup-before-revert
# Test revert in isolation
git revert --no-commit <commit>
# Run tests, check functionality
git revert --abort # If issues found
# Use descriptive commit messages
git revert -e <commit> # Edit revert message
Terminal window
# Choose revert approach based on situation:
# - Single problematic commit: git revert <commit>
# - Multiple related commits: git revert <commit1> <commit2>
# - Merge commit: git revert -m 1 <merge-commit>
# - Batch reverts: Script with error handling
# - Emergency revert: git revert --no-commit + manual testing
# General rules:
# - Prefer revert over reset for shared branches
# - Test reverts before pushing
# - Use clear commit messages explaining why reverting
# - Consider impact on collaborators
#!/bin/bash
# Safe revert for release management
release_revert() {
local bad_commit="$1"
local release_branch="${2:-main}"
echo "Reverting $bad_commit from $release_branch"
# Switch to release branch
git checkout "$release_branch"
# Fetch latest changes
git pull origin "$release_branch"
# Create revert commit
if git revert --no-edit "$bad_commit"; then
echo "✓ Revert commit created"
# Run tests
if run-release-tests; then
# Push revert
git push origin "$release_branch"
echo "✓ Revert pushed to production"
else
echo "✗ Tests failed - revert not pushed"
git reset --hard HEAD~1
fi
else
echo "✗ Revert failed - manual intervention needed"
fi
}
release_revert "bad-commit-sha" "release/v1.2"
Terminal window
# Collaborative revert with team coordination
collaborative_revert() {
local commit_to_revert="$1"
local reason="$2"
echo "Collaborative revert of $commit_to_revert"
echo "Reason: $reason"
# Notify team
notify_team "Planning to revert $commit_to_revert: $reason"
# Create revert branch
git checkout -b "revert-$commit_to_revert"
# Perform revert
git revert --no-edit "$commit_to_revert"
# Add detailed commit message
git commit --amend -m "Revert \"$commit_to_revert\"
This reverts commit $commit_to_revert.
Reason for revert: $reason
Original commit details:
$(git show --no-patch --format="Author: %an <%ae>%nDate: %ad%n%n%s%n%n%b" "$commit_to_revert")"
# Create pull request
# Push branch and create PR for review
git push origin "revert-$commit_to_revert"
echo "Revert branch created and pushed"
echo "Create pull request for team review"
}
collaborative_revert "abc123" "Introduces breaking API changes"
Terminal window
# Automated revert for CI/CD failures
automated_revert() {
local failed_commit="$1"
local test_command="$2"
echo "Automated revert analysis for $failed_commit"
# Test current state
if $test_command; then
echo "Current state is healthy - no revert needed"
return 0
fi
# Test revert
echo "Testing revert of $failed_commit..."
if git revert --no-commit "$failed_commit" 2>/dev/null; then
if $test_command; then
echo "✓ Revert fixes the issue"
# Create revert commit
git commit -m "Automated revert of $failed_commit
This commit was automatically reverted because it caused test failures.
Original commit: $failed_commit"
echo "✓ Automated revert completed"
return 0
else
echo "✗ Revert doesn't fix the issue"
git revert --abort
fi
else
echo "✗ Cannot revert $failed_commit (likely merge commit or conflicts)"
fi
echo "Manual intervention required"
return 1
}
automated_revert "HEAD" "npm test"
Terminal window
# Resolve revert conflicts
git revert <problematic-commit>
# When conflicts occur:
# 1. Check conflicted files
git status
# 2. Edit conflicted files manually
# Remove conflict markers (<<<<<<< ======= >>>>>>>)
# 3. Stage resolved files
git add resolved-file.txt
# 4. Continue revert
git revert --continue
# Alternative: Abort and try different approach
git revert --abort
Terminal window
# Handle merge commit revert
git revert -m 1 <merge-commit>
# If wrong parent specified:
git revert --abort
git revert -m 2 <merge-commit> # Try other parent
# Check merge commit structure
git show --pretty=raw <merge-commit>
git log --graph --oneline -n 5
Terminal window
# Handle conflicts in multiple commit reverts
git revert commit1 commit2 commit3
# If conflicts in commit2:
# Option 1: Resolve and continue
git add resolved-files
git revert --continue
# Option 2: Skip problematic commit
git revert --skip # Skip commit2, continue with commit3
# Option 3: Abort all reverts
git revert --abort
Terminal window
# Undo a revert operation
git log --oneline -5 # Find revert commit
# Revert the revert commit
git revert <revert-commit-sha>
# This restores the original changes
Terminal window
# Handle large reverts efficiently
large_revert() {
local commits_file="$1"
echo "Processing large revert operation..."
# Process commits in batches
while read -r commit; do
echo "Reverting $commit..."
if git revert --no-edit "$commit" 2>/dev/null; then
echo "✓ Reverted $commit"
else
echo "✗ Failed to revert $commit - conflicts detected"
# Handle conflicts or skip
fi
done < "$commits_file"
}
large_revert "commits-to-revert.txt"
Terminal window
# Revert a bug-introducing commit
revert_bug_fix() {
local bug_commit="$1"
local bug_description="$2"
echo "Reverting bug-introducing commit: $bug_commit"
echo "Bug: $bug_description"
# Verify the commit exists and causes the bug
git show "$bug_commit"
# Create revert commit
git revert --edit "$bug_commit"
# In commit message editor:
# "Revert bug fix for [issue]
#
# This reverts commit $bug_commit.
#
# The original fix introduced: $bug_description
# Need to investigate root cause before reapplying."
echo "Revert commit created - investigate root cause before reapplying fix"
}
revert_bug_fix "abc123" "causes application crashes on startup"
Terminal window
# Rollback an entire feature
feature_rollback() {
local feature_commits="$1"
local feature_name="$2"
echo "Rolling back feature: $feature_name"
# Get all commits for the feature
commits=$(git log --oneline --grep="$feature_name" | cut -d' ' -f1)
if [ -z "$commits" ]; then
echo "No commits found for feature: $feature_name"
return 1
fi
echo "Found commits to revert:"
echo "$commits"
# Revert commits in reverse order
echo "$commits" | tac | while read -r commit; do
echo "Reverting $commit..."
git revert --no-edit "$commit"
done
echo "Feature rollback complete"
echo "Consider removing feature branch and related code"
}
feature_rollback "feature/user-auth" "User Authentication"
Terminal window
# Revert security patches that cause issues
security_revert() {
local security_commit="$1"
local vulnerability="$2"
echo "Security revert for: $vulnerability"
# Document the revert carefully
git revert --edit "$security_commit"
# Commit message should include:
# - Reference to original security issue
# - Why the patch is being reverted
# - Alternative mitigation strategy
# - Timeline for proper fix
echo "Security revert completed"
echo "IMPORTANT: Implement alternative security measures immediately"
echo "Schedule proper security fix deployment"
}
security_revert "sec-fix-123" "CVE-2023-XXXX: Buffer overflow vulnerability"
Terminal window
# Rollback deployment commits
deployment_rollback() {
local deployment_tag="$1"
echo "Rolling back deployment: $deployment_tag"
# Find commits since deployment
deployment_commit=$(git rev-parse "$deployment_tag")
current_commit=$(git rev-parse HEAD)
if [ "$deployment_commit" = "$current_commit" ]; then
echo "Already at deployment commit"
return 0
fi
# Get commits to revert
commits_to_revert=$(git log --oneline "$deployment_commit..HEAD" | cut -d' ' -f1)
echo "Commits to revert:"
echo "$commits_to_revert"
# Revert each commit
echo "$commits_to_revert" | tac | while read -r commit; do
if git revert --no-edit "$commit" 2>/dev/null; then
echo "✓ Reverted $commit"
else
echo "✗ Failed to revert $commit - manual intervention needed"
exit 1
fi
done
echo "Deployment rollback complete"
echo "Verify system stability before next deployment"
}
deployment_rollback "v1.2.0"
Terminal window
# Manage hotfix reverts
hotfix_revert_management() {
local hotfix_commit="$1"
local production_branch="${2:-main}"
echo "Hotfix revert management for $hotfix_commit"
# Check if hotfix is still needed
echo "Analyzing hotfix impact..."
# Run tests, check monitoring, etc.
# If hotfix causes more problems than it solves
if should_revert_hotfix; then
echo "Reverting hotfix..."
# Switch to production branch
git checkout "$production_branch"
git pull origin "$production_branch"
# Revert hotfix
git revert --no-edit "$hotfix_commit"
# Test revert
if run_production_tests; then
git push origin "$production_branch"
echo "✓ Hotfix reverted successfully"
else
echo "✗ Revert causes issues - manual intervention needed"
git revert --abort
fi
else
echo "Hotfix should remain in place"
fi
}
hotfix_revert_management "hotfix-456" "production"

What’s the difference between revert and reset?

Section titled “What’s the difference between revert and reset?”

revert creates new commits that undo changes (safe for shared repos); reset rewinds branch history (avoid on shared branches).

Use git revert -m 1 to specify which parent to revert to. Check git show to see parent structure.

Yes, git revert , but this creates multiple revert commits. Consider if you want to revert as one operation.

Resolve conflicts like any merge: edit files, remove conflict markers, stage changes, then git revert —continue.

Revert the revert commit: git revert . This restores the original changes.

Yes, git revert —no-commit applies the revert changes but doesn’t create the commit. Useful for combining with other changes.

Test the revert first with —no-commit, run your tests, then commit if successful. Always have a backup branch.

Use git revert .. or list individual commits. Consider if you want separate revert commits or one combined revert.

Yes, revert is designed for shared/public branches. It preserves history and is safe for collaboration.

What’s the difference between revert and cherry-pick?

Section titled “What’s the difference between revert and cherry-pick?”

revert applies reverse changes from a commit; cherry-pick applies the original changes from a commit to current branch.

Use git revert —no-commit then git diff —staged to see what changes will be reverted.

Yes, use git revert —edit to customize the commit message explaining why the revert is needed.

Revert the revert commit: git revert . This brings back the original changes.

How do I handle revert conflicts in automation?

Section titled “How do I handle revert conflicts in automation?”

Use git revert —abort on failure, or implement conflict resolution logic in your automation scripts.

Yes, but carefully. Use —no-commit for testing, then commit only if tests pass. Consider manual approval for production reverts.

  1. Safe Change Reversal: Undo problematic commits without rewriting shared history
  2. Bug Fix Rollback: Remove buggy changes while preserving complete project history
  3. Feature Rollback: Safely remove entire features or functionality
  4. Security Patch Management: Revert security fixes that cause unexpected issues
  5. Deployment Rollback: Undo deployment commits when issues are discovered
  6. Collaborative Development: Safely undo changes in shared repository environments