Sample questions
Question 1Easy
You ran git add notes.txt by mistake. Which command unstages it but keeps your edits?
- git rm notes.txt
- git restore --staged notes.txt
- git restore notes.txt
- git clean notes.txt
Answer: B. git restore --staged notes.txt
Explanation
git restore --staged resets the index entry to HEAD, leaving the working file as it is. git reset notes.txt does the same.
Step-by-step solution
- File is staged.
git restore --staged copies the HEAD entry into the index.- The working file keeps your edits.
Why the other options are wrong
- A.
git rm would delete the file. - C. Without
--staged, it would discard your edits. - D.
git clean only deletes untracked files.
Question 2Easy
What does git clean -n do?
- Deletes every untracked file immediately without asking for confirmation
- Removes ignored files only
- Resets tracked files to HEAD
- Shows which untracked files would be deleted, without deleting
Answer: D. Shows which untracked files would be deleted, without deleting
Explanation
-n is a dry run. git clean -f actually deletes untracked files.
Step-by-step solution
git clean targets untracked files.-n only previews.- Use
-f to delete for real.
Why the other options are wrong
- A. Deleting needs
-f. - B. Ignored files need
-x. - C. Clean never touches tracked files.
Tip: Always dry-run first: it is irreversible.
Question 3Easy
Which command sets the name Git writes in new commits for all your repositories?
- git init --name "Your Name"
- git config --global user.name "Your Name"
- git commit --author-global
- git remote add name
Answer: B. git config --global user.name "Your Name"
Explanation
--global writes to ~/.gitconfig; without it, the setting applies to the current repository only.
Step-by-step solution
- Run
git config --global user.name .... - It is stored in
~/.gitconfig. - New commits use that name.
Why the other options are wrong
- A.
git init has no such option. - C. No such commit option exists.
- D.
remote add configures remotes.
Question 4Easy
What does git diff --staged (or --cached) compare?
- The working tree against the staging area (what plain git diff shows)
- The staging area against the last commit (HEAD)
- The local branch against the remote branch
- Two branches by name
Answer: B. The staging area against the last commit (HEAD)
Explanation
--staged shows what would go into the next commit: index versus HEAD.
Step-by-step solution
- Plain
git diff is working tree vs index. --staged shifts to index vs HEAD.- Use it to review before committing.
Why the other options are wrong
- A. That is plain
git diff without options. - C. Comparing with a remote needs
git diff origin/main. - D. Branch comparison is
git diff A B.
Tip: Review with git diff --staged before every commit.
Question 5Medium
In a repository, what does HEAD~2 refer to?
- The commit two steps ahead of HEAD on the branch
- The second branch that was created in the repository
- The second parent of the current merge commit
- The grandparent of HEAD, following first parents
Answer: D. The grandparent of HEAD, following first parents
Explanation
~n walks n generations back along first parents (HEAD~2 is the grandparent). ^2 selects the second parent of a merge commit.
Step-by-step solution
- ~ walks back along first parents.
- HEAD~1 is the parent, HEAD~2 the parent's parent.
- So HEAD~2 is the grandparent.
Why the other options are wrong
- A. ~ goes backwards in history, not ahead.
- B. ~ counts commits, not branches.
- C. The second parent of a merge is HEAD^2.
Tip: ~ = generations back; ^ = which parent.
Question 6Medium
What does git rm --cached secret.txt do?
- Deletes the file from the disk and also removes it from Git tracking entirely
- Deletes it from history of all commits
- Restores it from the last commit
- Removes it from the index so it is untracked, but keeps the file on disk
Answer: D. Removes it from the index so it is untracked, but keeps the file on disk
Explanation
--cached limits the removal to the staging area. The next commit stops tracking the file while your local copy stays.
Step-by-step solution
- The index entry is removed.
- The working file stays on disk.
- After commit the file is untracked (add it to .gitignore).
Why the other options are wrong
- A. Without
--cached, it would also delete the file. - B. Old commits still contain it; history is untouched.
- C. Restoring is
git restore.
Tip: Add the filename to .gitignore after this.
Question 7Medium
You commit and notice you forgot a file. The commit is not pushed. After git add forgot.txt, which command folds it into the last commit keeping the same message?
- git commit --amend --no-edit
- git commit --fixup
- git reset --hard HEAD
- git revert HEAD
Answer: A. git commit --amend --no-edit
Explanation
--amend replaces the last commit with a new one that includes the staged changes. --no-edit keeps the message.
Step-by-step solution
- Stage the forgotten file.
git commit --amend --no-edit rewrites HEAD.- The commit gets a new hash.
Why the other options are wrong
- B.
--fixup makes a separate commit meant for autosquash. - C.
reset --hard discards the staged file. - D.
revert adds an opposing commit.
Tip: Amend only unpushed commits.
Question 8Medium
You want to see what changed in the working tree compared with the last commit, staged and unstaged together. Which command?
- git diff --staged
- git diff
- git diff HEAD
- git log -p
Answer: C. git diff HEAD
Explanation
git diff HEAD compares the working tree to HEAD, merging staged and unstaged edits.
Step-by-step solution
- Plain
git diff is working tree vs index. --staged is index vs HEAD.git diff HEAD covers both.
Why the other options are wrong
- A. That shows only staged changes.
- B. That shows only unstaged changes.
- D.
log -p shows past commits.
Question 9Hard
You run git commit --allow-empty -m "trigger CI". What happens?
- The command fails because nothing is staged
- A commit with no file changes is created
- The last commit is amended
- The remote is updated automatically
Answer: B. A commit with no file changes is created
Explanation
By default Git refuses commits with no changes; --allow-empty creates one anyway, often used to retrigger CI.
Step-by-step solution
- No changes are staged.
--allow-empty overrides the check.- A new commit with the same tree as the parent appears.
Why the other options are wrong
- A. That is the default behaviour without the flag.
- C. Amending needs
--amend. - D. Pushing is separate.
Question 10Hard
After git reset --soft HEAD~1, what happens to the changes from the undone commit?
- They are lost
- They are moved to the stash
- They stay in the working tree and remain staged
- They stay in the working tree but are unstaged
Answer: C. They stay in the working tree and remain staged
Explanation
--soft moves only the branch pointer. The index and working tree are untouched, so the changes are still staged. --mixed (the default) would unstage them, and --hard would discard them.
Step-by-step solution
- --soft moves only the branch pointer back one commit.
- The staging area and working tree are untouched.
- So the changes stay in the files and remain staged.
Why the other options are wrong
- A. Only --hard discards the changes.
- B. reset never moves changes to the stash.
- D. That is --mixed, the default.
Tip: soft keeps staged, mixed unstages, hard deletes.
Practise all 29 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →