DCubes
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 edits
  • git restore --staged file — unstage
  • git revert HEAD — new commit that undoes last commit
  • git 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.txt gone from latest tree.

Common mistakes

  • reset --hard on main after push — hurts collaborators.
  • Reverting merge commits without reading docs — advanced.

Next

Repo hygiene for pipelines