Skip to main content

Sign in

Save your progress and access it from any device.

Or with email

Don't have an account?

Privacy policy

Git revert: undo a commit cleanly

git revert creates a new commit that undoes the changes from a previous commit. History stays intact, perfect for teamwork.

Safe

What is git revert?

git revert creates a new commit that does the exact opposite of an existing commit. Additions become deletions and vice versa.

It's like pressing Ctrl+Z but keeping a record of the undo. Everyone can see what was done and undone.

Unlike git reset, revert never rewrites history. It's the recommended method for undoing changes on a shared branch.

Git revert syntax

git revert HEAD

Undo the last commit

git revert <hash>

Undo a specific commit

git revert HEAD~3..HEAD --no-commit

Undo multiple commits without committing

Git revert in practice

Undo a single commit or a series of commits.

Revert a commit

Revert multiple commits

git revert vs git reset

git revert

Creates a new undo commit. History stays intact. Safe for shared branches.

Use when: the branch is shared with the team

git reset

Removes commits from history. Rewrites history. Can break others' work.

Use when: you're working alone on your local branch

Common mistakes with git revert

Reverting the wrong commit

Check the hash with git log --oneline before reverting. If you make a mistake, you can revert the revert.

Not handling conflicts

A revert can generate conflicts if the code has evolved since the original commit. Resolve them just like a regular merge conflict.

Using reset instead of revert

On a shared branch, reset requires a push --force which desynchronizes the entire team. Always use revert in this case.

Part of the Undo Changes in Git guide

A

GitQuest is created by Anaïs (nouvelle fenêtre), web developer and head of education, specializing in tech training and digital accessibility.

Questions about git revert

Learn to undo without stress

GitQuest investigations teach you to correct your mistakes with confidence.

Start the investigations