Unit II · Saving and undoing
Branches and going back safely
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
- Git documentation: git-restore
- Pro Git: Branches in a Nutshell
- Git documentation: git-revert
- Git documentation: git-reset
- 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.