Restoring changes
๐ The everyday picture
An "undo" button that works across days, for the whole project, and never forgets.
๐What it really is
-
Throw away unsaved changes to a file (go back to the last commit):
git restore index.html. Careful: those uncommitted changes are gone. -
Unstage a file (take it out of the box, keep the changes):
git restore --staged index.html. -
Undo a commit safely by adding a new commit that reverses it:
git revert <commit-id>. History keeps both, so nothing is lost and it's safe even after sharing. -
Look at an old version without changing anything:
git show <commit-id>. -
Avoid commands that rewrite history (like
git reset --hardor force-pushing) until you truly understand them. They can delete work for good, and on shared projects they break other people's copies. -
The AI safety habit: commit before asking AI for a change. If the result is bad, look first:
git statuslists what changed andgit diffshows the lines. Then: - edits to files Git already tracks, not staged:
git restore .(run in the project's top folder) puts them back as they were at your last commit; - anything you staged:
git restore --staged --worktree .resets both the box and the files to the last commit; - brand-new files the AI created:
git restoreleaves them alone; delete them yourself (git clean -nlists them first,git clean -fremoves them for good); - not sure you want to lose it all?
git stash -usets every change aside, new files included, andgit stash popbrings it back.
๐ฑ๏ธTry it
- Make sure
git statusis clean. - Delete all the text in
index.htmland save. (Pretend an AI did this.) - Run
git restore index.html. Your text is back.
โ Quick check
An AI edited ten files and broke your site. You committed just before asking it, and git status shows only those ten files as changed, nothing staged. What's the quickest way back?
Show the answer
From the project's top folder, git restore . puts those files back as they were at your last commit. It doesn't
touch staged changes or brand-new files, so check git status afterwards.
Why is git revert safer than rewriting history?
Show the answer
It undoes a commit by adding a new one, so nothing is deleted and other copies of the project stay in step.
