Script Valley
Git and GitHub Complete Course: From Beginner to Advanced
Intermediate Git: Stashing, Tagging, Reverting, and ResettingLesson 4.3

Undoing Changes: git revert

git revert, safe undo, revert commit, revert merge commit, revert vs reset

Undoing Changes: git revert

Git revert creates a new commit that undoes the changes introduced by a specified commit. Unlike reset, revert is safe for shared repositories because it adds to history rather than rewriting it.

Diagramgit revert vs Dangerous Undo

IMAGE PROMPT (replace this block with your generated image):

Flat safe-vs-unsafe comparison diagram on white background. Title: git revert โ€” Safe Undo That Preserves History. Two horizontal commit timeline tracks. Track 1 (top): git revert (Safe). Shows: C1 โ†’ C2 โ†’ C3 โ†’ Revert-C2. C2 has a red error badge (the bad commit). Revert-C2 has a green undo badge. Arrow annotation: New commit undoes C2's changes. History intact. Safe for shared repos. Label above: green shield icon โ€” Safe for pushed commits. Track 2 (bottom): git reset --hard (Dangerous). Shows: C1 โ†’ C2 โ†’ C3 โ†’ [C3 crossed out, gone]. Branch pointer moves back to C2. C3 shown as a ghost/faded box. Arrow annotation: Rewrites history. Dangerous on shared branches. Label above: red warning icon โ€” Only for local unpushed commits. Command box showing merge commit revert syntax: git revert -m 1 merge-hash with explanation of -m 1 flag. White background, green for revert track, brand color #3A5EFF for headers.

Reverting a Commit

git revert abc1234

Replace abc1234 with the commit hash you want to undo. Git will create a new commit with the opposite changes, effectively cancelling out the original commit's effect. Your history shows both the original commit and the revert commit.

Reverting Without Opening an Editor

git revert abc1234 --no-edit

This automatically uses the default revert message without opening your text editor.

Reverting a Range of Commits

git revert HEAD~3..HEAD

This reverts the last three commits. Each gets its own revert commit unless you use --no-commit to stage them all and commit once.

Reverting a Merge Commit

Merge commits have two parents, so you must specify which parent represents the mainline:

git revert -m 1 merge-commit-hash

The -m 1 flag tells Git that parent 1 (the branch you merged into) is the mainline.

When to Use Revert

Use revert on any commit that has been pushed to a shared repository. It is the safest way to undo mistakes in public history because it never overwrites existing commits.

Up next

Resetting History: git reset

Sign in to track progress

Undoing Changes: git revert โ€” Intermediate Git: Stashing, Tagging, Reverting, and Resetting โ€” Git and GitHub Complete Course: From Beginner to Advanced โ€” Script Valley โ€” Script Valley