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.
git diff
git diff --staged
git diff HEAD
git diff HEAD~1 HEADgit diff shows unstaged changes. git diff --staged shows staged changes. git diff HEAD compares the entire working state against HEAD.- Review
git diffbefore staging. - Review
git diff --stagedbefore committing. git diff HEADcombines the comparison against the current commit.
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.
git restore file.js can permanently discard uncommitted modifications to that file. Inspect the diff first if the changes matter.
git restore app.js
git restore --staged app.js
git restore --source=HEAD~1 app.jsgit restore --stagedunstages without discarding the working copy.git restorecan discard working-tree changes.- Use explicit source commits when restoring historical versions.
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.
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.- Amend is ideal for fixing the latest local commit.
- Amending changes the commit hash.
- Be careful when rewriting history that others have already pulled.
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.
git reset --hard can remove uncommitted tracked changes. Make sure important work is committed, stashed, or otherwise backed up before using it.
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.- Soft reset preserves changes in the index.
- Mixed reset is the default and unstages changes.
- Hard reset can discard local tracked changes.
Which reset mode keeps changes staged after moving HEAD?