Undo without losing work · Lesson 11 of 15
Revert or reset?
Revert adds a safe correction; reset rewrites the branch line.
git revert X does not erase X; it creates a new commit with the inverse change. That is safe after pushing because everyone can pull the new history. reset moves a branch name backward, potentially hiding published commits from its visible line.
A—B—C (bug)
revert adds D inverse to C
history preserved
git init -q -b main echo "safe" > app.txt git add app.txt git commit -q -m "Safe" echo "bug" > app.txt git commit -q -am "Bad change" git revert HEAD cat app.txt
A simple choice: published or shared commit? Revert it. Private local commit that nobody relies on and history needs reshaping? Reset may fit. If a revert conflicts, resolve it like any other conflict.
Quick question
A bug reached main and others pulled it. Best choice?
Exercise
After a safe commit and a bug commit, reverse the latest commit and print the file.
Expected output: safe
git init -q -b main echo "safe" > app.txt git add app.txt git commit -q -m "Safe" echo "bug" > app.txt git commit -q -am "Bug" cat app.txt
Learn more from
Played 0 of 2 games
Sign in to save your progress and earn XP.