Staging and Committing
5 min read Quiz at the end
Stage changes with git add, commit with git commit, and unstage or discard with git restore.
Staging and Committing
# Working directory -> Staging area -> Repository
git status
git add file.txt # stage one file
git add . # stage everything
git add -p # interactively stage hunks
git commit -m "feat: add user login"
git commit -am "fix: correct typo" # stage+commit tracked files
git commit --amend # edit last commit (before push)
# Unstage
git restore --staged file.txt
git reset HEAD file.txt # older syntax
# Discard working directory changes
git restore file.txt
git checkout -- file.txt # older syntax
Topic Quiz · 2 questions
Test your understanding before moving on
1. What does git add -p do?
💡 git add -p (patch mode) lets you review and stage individual hunks within files.
2. How do you amend the last commit message before pushing?
💡 git commit --amend opens the editor to change the last commit message or add staged files.