Git module
Lesson 12
Undo safely
restore and revert for everyday mistakes — without rewriting shared history.
beginner30 minRun on your machine
What you will be able to do
- Discard unstaged edits with git restore
- Unstage with git restore --staged
- Revert a commit with a new undo commit
Why this matters for data work
Mistakes happen. Safe undo means you do not rewrite commits already on origin/main. Prefer restore and revert over hard reset on shared branches.
Concepts
git restore file— drop unstaged editsgit restore --staged file— unstagegit revert HEAD— new commit that undoes last commitgit reset --hard— destructive; lab-only when you are sure
Practice on your machine
cd ~/dcubes/git-lab/init-demo
echo "oops" >> README.md
git restore README.md
git status
echo "temp" > temp.txt
git add temp.txt
git restore --staged temp.txt
rm temp.txt
echo "bad" > bad.txt
git add bad.txt
git commit -m "Add bad.txt accidentally"
git revert --no-edit HEAD
git log --oneline | head
What you should see
- README restored before commit.
- Revert commit on top of history;
bad.txtgone from latest tree.
Common mistakes
reset --hardon main after push — hurts collaborators.- Reverting merge commits without reading docs — advanced.