Git 2 reference · Undoing Changes

git revert Command (Git)

Create a new commit that undoes an earlier commit's changes; the safe way to undo commits that were already pushed.

Git 2

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

git revert [--no-edit] COMMIT

Parameters

ParameterDescription
--no-edit Use the default message.
-m 1 Revert a merge commit, keeping parent 1.

Examples

Undo a pushed commit
git init -q -b main app && cd app
echo '# Shop' > README.md
git add README.md && git commit -q -m 'Initial commit'
echo 'discount=50' > promo.txt; git add .; git commit -q -m 'Add 50% promo'
git revert -q --no-edit HEAD
git log --oneline; ls

Output

302f114 Add 50% promo
7f7f133 Initial commit
README.md
promo.txt

Run this on your computer — see “Get Started” for setup.

Undo an older commit
git init -q -b main app && cd app
echo '# Shop' > README.md
git add README.md && git commit -q -m 'Initial commit'
echo a > a.txt; git add .; git commit -q -m 'Add a'
echo b > b.txt; git add .; git commit -q -m 'Add b'
git revert -q --no-edit HEAD~1
ls

Output

README.md
a.txt
b.txt

Run this on your computer — see “Get Started” for setup.

Was this page helpful?