Skip to content

Undoing Changes — Module Recap

Module 04 is complete. You now have the full toolkit for undoing changes at every stage: working tree, staging area, the most recent commit, any previous commit, and ranges of commits.

Discarding working-tree changesgit restore <file> overwrites the file with the staged or last-committed version. Destructive — changes not recoverable from Git. Review git diff before discarding.

Unstaging filesgit restore --staged <file> removes a file from the staging area without touching the working tree. Not destructive. Use it to split staged changes into separate commits.

Amending the last commitgit commit --amend replaces the last commit with a new one — with a fixed message, additional staged content, or both. Creates a new hash. Only safe for unpushed commits.

git revert — Creates a new commit that is the inverse of a target commit. Preserves full history. Safe for pushed commits. The right choice when you need to undo something on a shared branch.

git reset — Moves the branch pointer backward, removing commits from the tip of the branch. Three modes: --soft (keep staged), --mixed (keep unstaged), --hard (discard). Only safe for unpushed commits. git reflog is the recovery tool if you reset too far.

SituationRight tool
Discard unsaved working-tree changesgit restore <file>
Remove a file from staginggit restore --staged <file>
Fix the last commit’s message (not pushed)git commit --amend -m "..."
Add a forgotten file to the last commit (not pushed)git add <file> then git commit --amend --no-edit
Undo a commit on a pushed/shared branchgit revert <hash>
Undo a local-only commit, keep changes stagedgit reset --soft HEAD~1
Undo a local-only commit, keep changes unstagedgit reset HEAD~1
Undo a local-only commit and discard all changesgit reset --hard HEAD~1
Recover from an accidental hard resetgit reflog then git reset --hard <recovered-hash>
CommandWhat it does
git restore <file>Discard working-tree changes (destructive)
git restore --staged <file>Remove from staging area (keep working-tree changes)
git restore --staged .Unstage everything
git commit --amendReplace last commit (opens editor)
git commit --amend -m "msg"Replace last commit with new message
git commit --amend --no-editReplace last commit, keep message
git revert <hash>Create a new commit inverting the target commit
git revert HEADRevert the most recent commit
git revert --no-editSkip editor for revert commit message
git reset --soft HEAD~nMove branch back n commits, keep changes staged
git reset HEAD~nMove branch back n commits, keep changes unstaged
git reset --hard HEAD~nMove branch back n commits, discard all changes
git reflogLog of all HEAD movements — recovery tool

Module 05 takes Git from your local machine to the internet. You will connect a local repository to GitHub, push and pull changes, and understand the relationship between local branches and their remote counterparts.

Everything you have learned about commits, branches, and history is the same on GitHub — you are just sharing it with the world.