Git Reset, Revert, and Restore Cheat Sheet

Understand the differences between reset, revert, and restore and use the right recovery command safely.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Reset Basics
Undo last commit but keep everything staged
git reset --soft HEAD~1

# Move HEAD back one commit while preserving index and working tree.

Undo last commit and unstage changes
git reset --mixed HEAD~1

# Move HEAD back one commit and leave changes in the working tree.

Discard last commit and local changes
git reset --hard HEAD~1

# Move HEAD back one commit and discard index and working tree changes.

Unstage a single file
git reset HEAD path/to/file

# Remove a path from the index without changing its contents in the working tree.

Reset out of a conflicted merge state
git reset --merge ORIG_HEAD

# Try to leave local changes intact while backing out of a merge-like operation.

## Revert Basics
Revert a single commit
git revert <commit_sha>

# Create a new commit that reverses an earlier commit.

Revert several commits without auto-committing
git revert --no-commit <start_commit>^..<end_commit>

# Apply inverse changes into the index and working tree first.

Revert a merge commit
git revert -m 1 <merge_commit_sha>

# Reverse a merge relative to a chosen parent.

Continue a conflicted revert
git revert --continue

# Resume revert after resolving conflicts.

## Restore Basics
Discard unstaged changes in a file
git restore path/to/file

# Restore a file in the working tree from the index.

Unstage a file with restore
git restore --staged path/to/file

# Restore the index entry for a path from HEAD.

Restore a file from another commit
git restore --source=<commit_sha> path/to/file

# Copy file contents from a specific commit into the working tree.

Interactively restore hunks
git restore --patch path/to/file

# Select portions of a file to restore interactively.

Restore both index and working tree from another commit
git restore --source=<commit_sha> --staged --worktree path/to/file

# Reset a file to another revision in both staging area and working tree.

## Decision Guide
Mental model for reset vs restore vs revert
reset = move branch/history
restore = restore file content/index state
revert = add new commit that undoes old commit

# Quick decision guide for the three similarly named commands.

Create a safety branch before destructive changes
git branch backup-before-reset

# Save a recovery pointer before hard reset or risky history edits.

Use reflog after a mistaken reset
git reflog

# Find the previous branch tip after a bad reset.

Recommended next

No recommendations yet.