Git Git Staging and Commits 1 β Questions and Answers
Question 1: What is the staging area (index) in Git?
- The remote repository
- An intermediate area where changes are prepared before being committed (Correct answer)
- The working directory
- A temporary branch used during merges
Correct answer: An intermediate area where changes are prepared before being committed
The staging area (also called the index) holds a snapshot of changes you have marked to be included in the next commit.
Question 2: Which command stages only specific hunks of a modified file interactively?
- git add -i <file>
- git add -p <file> (Correct answer)
- git stage --hunks <file>
- git add --selective <file>
Correct answer: git add -p <file>
The `git add -p` (patch) command interactively presents each change hunk and lets you choose whether to stage it.
Question 3: What does `git commit --amend` do?
- Creates a new branch from the last commit
- Modifies the most recent commit by adding staged changes or editing its message (Correct answer)
- Reverts the last commit
- Squashes the last two commits
Correct answer: Modifies the most recent commit by adding staged changes or editing its message
The `--amend` flag replaces the most recent commit with a new one that includes any newly staged changes and an optionally updated message.
Question 4: What is the effect of `git commit -a`?
- Commits all files in the repository including untracked ones
- Automatically stages all tracked modified files and commits them (Correct answer)
- Amends the previous commit
- Commits with an empty message
Correct answer: Automatically stages all tracked modified files and commits them
The `-a` flag automatically stages all modifications and deletions to tracked files before committing, skipping the need for explicit `git add`.
Question 5: How do you unstage a file that was added with `git add`?
- git remove <file>
- git restore --staged <file> (Correct answer)
- git unstage <file>
- git reset HEAD <file>
Correct answer: git restore --staged <file>
The `git restore --staged <file>` command removes the file from the staging area without discarding the working directory changes.
Question 6: Which command shows the difference between the working directory and the staging area?
- git diff --staged
- git diff (Correct answer)
- git diff HEAD
- git status -v
Correct answer: git diff
A plain `git diff` shows the difference between the working directory and what is currently staged (the index).
What is the staging area (index) in Git?