Skip to content
Justice Tech School

Undoing Changes

git restore file discards unstaged edits and git restore --staged file unstages. git commit --amend rewrites the last commit message or contents. git revert makes a new commit that undoes an old one and is safe for shared history.

Example
echo v1 > a.txt; git add .; git commit -q -m "v1"
echo v2 > a.txt
git restore a.txt
cat a.txt
echo v2 > a.txt; git commit -q -am "v2"
git revert --no-edit HEAD >/dev/null
cat a.txt
git log --format=%s
git commit -q --amend -m "Revert v2 (edited)"
git log -n 1 --format=%s

Result

v1
v1
Revert "v2"
v2
v1
Revert v2 (edited)

Exercise

Discard edits in a file.

git  a.txt

Quiz Test

Which undo is safe on shared history?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.