recovery
git revert
Creates a new commit that inverts the changes of a previous commit, preserving history. The safe way to undo pushed commits on shared branches.
git revert [<options>] <commit>... Common flags
| Flag | Purpose |
|---|---|
| -n / --no-commit | Stage the revert without creating a commit |
| --continue | Resume after resolving conflicts |
| --abort | Cancel an in-progress revert |
| -m <parent> | Required when reverting a merge commit; picks the mainline |
| -e / --edit | Edit the revert commit message |
Examples
git revert HEAD Undo the latest commit with a new inverse commit
git revert abc123..def456 Revert a range of commits, newest first
git revert -m 1 <merge-sha> Revert a merge commit relative to its first parent
Gotcha
Reverting a merge with -m 1 makes it hard to re-merge the same branch later; you may need to revert the revert before merging again.
Related commands
git reset
Moves the current branch tip to a specified commit, optionally updating the index and working tree. Used to unstage files or rewind local history.
git cherry-pick
Applies the changes from one or more specific commits onto the current branch as new commits. Useful for porting bug fixes across branches.
git log
Displays commit history in reverse chronological order with filters for author, date, path, or content. The primary tool for exploring project history.
git restore
Modern command (Git 2.23+) for restoring working tree files from the index or a commit, or unstaging files. Replaces the confusing file-mode of 'git checkout'.
git stash
Shelves uncommitted changes onto a stack so you can switch context with a clean working tree. Later, reapply them with pop or apply.