Inspect history and undo carefully
Use diffs and history to understand changes before choosing an undo command.
- Choose a safe undo command based on whether a change is committed or shared.
Inspection comes before undoing. git log shows commits, git show explains one commit, and git diff compares versions. For an edit you have not committed, git restore can discard it. For a commit already shared with others, git revert adds a new commit that undoes it.
1git log --oneline --decorate -4
2git show a1b2c3d
3git diff HEAD~1..HEAD
4
5# Discard an unstaged edit to one file
6git restore README.md
7
8# Undo a published commit without rewriting history
9git revert a1b2c3da1b2c3d (HEAD -> main) Clarify setup instructions 74cd912 Add install section # git show and git diff display the selected changes # git restore changes the working tree # git revert creates a new commit
git restore can discard work, so inspect it first. git reset moves the current branch and has several modes; it can make commits unreachable. Avoid rewriting commits other people may already have based work on.
Key takeaways
Use
git log,git showandgit diffto inspect before acting.git restoreaffects working-tree or staged files; check which form you need.Prefer
git revertfor shared history because it does not remove existing commits.
Lesson quiz
5 questions · pass with 4 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…