Keelmark Your record

Unit II · Saving and undoing

Branches and going back safely

Lesson 05 of 186 minSources checked Oct 17, 2026

Branches let you try something without touching the working version, and Git has more than one way to undo. The right one depends on whether others already have your commits.

Branches

A branch is a separate line of commits. Create one for an experiment (git switch -c try-new-checkout), let the AI change things there, and merge it back only when it works. If it never works, switch back to your main branch: the experiment never touched it.

Undo a commit others already have: revert

git revert <commit> makes a new commit that undoes the changes of an earlier one. The history stays intact, so it is safe on a branch that other people, or your deploy tool, have already pulled.

Rewind your own branch: reset, with care

git reset moves your branch back to an earlier commit. With --hard it also throws away uncommitted work in your files. Because it rewrites history, avoid it on commits you have already pushed and shared.

Undo one file

git restore <file> discards the edits to a file that you have not staged, putting it back to the version in the staging area: that is the last commit, unless you staged an edit with git add since. To put a file back to the last commit even after staging it, use git restore --staged --worktree <file>.

Words to know

Branch
an independent line of commits
Merge
bring one branch's commits into another
Revert
a new commit that undoes an earlier one
Reset
move a branch to another commit (it can discard work)

Sources

  1. Git documentation: git-restore
  2. Pro Git: Branches in a Nutshell
  3. Git documentation: git-revert
  4. Git documentation: git-reset
  5. Pro Git: Undoing Things

Checked October 17, 2026. Pages on other sites change; if a source no longer says what the lesson says, the source wins.

Lesson check

Two questions. Answer both correctly to pass this lesson. You can try as often as you like.

1A bad commit is already pushed and deployed. Which undo keeps the shared history intact?
2What can git reset --hard throw away?