Undoing a commit with git revert
Not every commit turns out to be a good idea. git revert is the safe way to
undo one: instead of deleting the commit or rewriting history (which gets
dangerous the moment anyone else has that history too), it makes a new
commit that's the exact opposite of the one you're undoing. Whatever that
commit added, the revert removes. History stays intact - anyone reading the
log later can see both "this happened" and "then it got undone," instead of
the mistake quietly vanishing.
main has one commit already (app.txt). You're about to add a file that
turns out to be a mistake - and undo it the right way.
Your task: stage and commit feature.txt with the exact message
Add feature.txt, then run git revert HEAD to undo that commit. When
you're done, main should be back to just app.txt - with two commits in
the log on top of the starting one: the commit that added feature.txt, and
the revert that cancels it out.
You'll practice:
git revert HEAD - undoing the most recent commit by adding a new one
- Why "undo by adding a commit" is safer than rewriting history
Related reading: Undoing What You've Already Pushed →