HomeBytesheetsRoadmapsUI TemplatesUI ToolsStack Discussion
4 Topics
Chapter•40 Minutes

Staging, Commits, and Undoing Changes

Master staging techniques, diff inspection, commit creation, restoring files, and safely undoing changes.

Understanding git diff

git diff compares your working tree with the staging area. git diff --staged compares the staged snapshot with the current commit. These commands are essential before committing because they let you verify exactly what will be recorded.

Inspect Changes
git diff
git diff --staged
git diff HEAD
git diff HEAD~1 HEAD
💡 git diff shows unstaged changes. git diff --staged shows staged changes. git diff HEAD compares the entire working state against HEAD.
Key Takeaways
  • Review git diff before staging.
  • Review git diff --staged before committing.
  • git diff HEAD combines the comparison against the current commit.
Knowledge Check

Which command shows changes currently staged for commit?

Restoring and Unstaging Files

Modern Git provides git restore for restoring working-tree files and removing changes from the staging area. git restore file.js discards unstaged modifications to that file, while git restore --staged file.js removes it from the staging area without discarding its working-tree changes.

Discarded Changes Can Be Lost

git restore file.js can permanently discard uncommitted modifications to that file. Inspect the diff first if the changes matter.

Restore and Unstage
git restore app.js
git restore --staged app.js
git restore --source=HEAD~1 app.js
💡 The first command restores the working file from the index. The second removes it from staging. The third restores the file from another commit.
Key Takeaways
  • git restore --staged unstages without discarding the working copy.
  • git restore can discard working-tree changes.
  • Use explicit source commits when restoring historical versions.
Knowledge Check

What does `git restore --staged file.js` do?

Amending Commits

When you have just created a commit and discover a small mistake, git commit --amend can replace the current commit with a corrected version. It is useful for fixing the latest commit message or adding forgotten changes.

Amending changes the commit identity. Avoid rewriting a commit that has already been shared when other developers may depend on its original history.

Amend the Latest Commit
git add forgotten-file.js
git commit --amend --no-edit

git commit --amend -m "Correct commit message"
💡 --no-edit keeps the existing message. Supplying -m replaces it. Both operations create a replacement commit for the previous HEAD.
Key Takeaways
  • Amend is ideal for fixing the latest local commit.
  • Amending changes the commit hash.
  • Be careful when rewriting history that others have already pulled.
Knowledge Check

What is a common use for `git commit --amend`?

Understanding git reset

git reset moves the current branch reference and can also modify the staging area and working tree depending on its mode. --soft moves HEAD while keeping changes staged. --mixed, the default, moves HEAD and resets the index while keeping working-tree changes. --hard resets HEAD, the index, and working tree.

Reset is powerful and should be used carefully, particularly on shared branches.

Use --hard Carefully

git reset --hard can remove uncommitted tracked changes. Make sure important work is committed, stashed, or otherwise backed up before using it.

Reset Modes
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
💡 --soft preserves staged changes, --mixed preserves them only in the working tree, and --hard discards tracked working-tree and staging changes that differ from the target.
Key Takeaways
  • Soft reset preserves changes in the index.
  • Mixed reset is the default and unstages changes.
  • Hard reset can discard local tracked changes.
Knowledge Check

Which reset mode keeps changes staged after moving HEAD?