Git Git Staging and Commits 2 — Questions and Answers
Question 1: What does `git diff --staged` compare?
- Working directory vs last commit
- Staging area vs last commit (Correct answer)
- Two commits by hash
- Remote vs local
Correct answer: Staging area vs last commit
The `--staged` (or `--cached`) flag makes `git diff` compare the staging area against the last commit, showing what will be in the next commit.
Question 2: How do you create a commit with a message provided inline?
- git commit --note 'message'
- git commit -m 'message' (Correct answer)
- git commit --msg 'message'
- git commit -t 'message'
Correct answer: git commit -m 'message'
The `-m` flag allows you to pass the commit message directly on the command line without opening an editor.
Question 3: What does the command `git add .` stage?
- Only modified tracked files
- All changes in the current directory and below, including new untracked files (Correct answer)
- All files in the repository root only
- Deleted files only
Correct answer: All changes in the current directory and below, including new untracked files
The `git add .` command stages all new, modified, and deleted files within the current directory and all subdirectories.
Question 4: Which command discards unstaged changes to a tracked file?
- git reset <file>
- git restore <file>
- git checkout -- <file>
- Both B and C are correct (Correct answer)
Correct answer: Both B and C are correct
Both `git restore <file>` (modern syntax) and `git checkout -- <file>` (older syntax) discard working directory changes to a tracked file.
Question 5: What is a signed commit in Git?
- A commit with a very long message
- A commit cryptographically signed with a GPG key to verify authorship (Correct answer)
- A commit that requires two approvals
- A commit tagged with a version number
Correct answer: A commit cryptographically signed with a GPG key to verify authorship
A signed commit includes a GPG signature attached by the author, allowing others to verify the commit's authenticity and integrity.
Question 6: How do you view what is currently in the staging area?
- git staged
- git status
- git diff --cached
- Both B and C show staged changes (Correct answer)
Correct answer: Both B and C show staged changes
Both `git status` (which lists staged files by name) and `git diff --cached` (which shows the actual diff) provide information about staged changes.
What does `git diff --staged` compare?