
Most developers use Git every day but only a handful of its commands. This fifth set covers the commands that make you faster at reading history and moving changes around: the different kinds of diff, two-dot and three-dot ranges, patches with format-patch and am, the newer restore and switch commands, and clone options that keep CI pipelines and huge repositories fast.
How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 15 questions.
Topics covered in this set
- git diff, diff --staged and diff --stat
- Two-dot vs three-dot ranges, HEAD~ vs HEAD^
- format-patch, am and apply
- restore, switch and clean
- Shallow and partial clones
- describe, shortlog and git grep
Git MCQs with answers and explanations
Q1. You edited two files but have not run git add. What does plain git diff (with no arguments) show?
a) Changes between your last commit and the remote
b) Changes in the working tree that are not yet staged, compared with the index
c) Only staged changes
d) Nothing, because diff needs a commit hash
Q2. Which command shows exactly what will go into your next commit?
a) git diff --staged (or --cached)
b) git diff HEAD~1
c) git log -p
d) git status -s
Q3. What does git log main..feature list?
a) Commits on main that are not on feature
b) All commits on both branches
c) Commits that are reachable from feature but not from main
d) Only merge commits
Q4. What does git diff main...feature (three dots) compare?
a) The tip of main with the tip of feature
b) Every file in both branches
c) Only the files that conflict
d) The tip of feature with the merge base of main and feature, so you see only the changes made on feature
Q5. HEAD is a merge commit. What is the difference between HEAD~2 and HEAD^2?
a) HEAD~2 is the grandparent following first parents; HEAD^2 is the second parent of the merge, which is the tip of the branch that was merged in
b) They always point to the same commit
c) HEAD~2 is the second parent; HEAD^2 is the grandparent
d) HEAD^2 is invalid syntax
Q6. What does git format-patch -3 do?
a) Deletes the last 3 commits
b) Creates one .patch file for each of the last 3 commits, including the author, date and commit message
c) Squashes the last 3 commits
d) Sends 3 emails automatically
Q7. You received patch files made by git format-patch. What is the difference between applying them with git am and with git apply?
a) There is no difference
b) git apply creates commits; git am only changes files
c) git am creates commits that keep the original author and message, while git apply only changes the files in your working tree
d) git am works only on Windows
Q8. You accidentally ran git add on config.local. How do you unstage it but keep your edits in the file?
a) git restore --staged config.local
b) git restore config.local
c) git clean -f config.local
d) git rm config.local
Q9. What does git switch -c feature/login do?
a) Deletes the branch feature/login
b) Renames the current branch
c) Switches to a remote branch without creating anything
d) Creates a new branch called feature/login and switches to it
Q10. Before deleting untracked build files, you want to see what git clean would remove. Which command shows this without deleting anything?
a) git clean -fd
b) git clean -n
c) git reset --hard
d) git stash -u
Q11. A CI job runs git clone --depth 1. What is the effect?
a) Only the first file in the repository is downloaded
b) The clone is encrypted
c) Only the latest commit is downloaded, without older history, so the clone is much faster, but commands that need history such as describe or blame will not work fully
d) The repository is cloned with one branch per folder
Q12. What does git clone --filter=blob:none create?
a) A partial clone that has all commits and trees but downloads file contents (blobs) only when they are needed, for example at checkout
b) A clone with no files at all and no history
c) A clone that ignores binary files forever
d) A bare repository
Q13. git describe --tags prints v2.3.0-5-g1a2b3c4. What does this mean?
a) Version 2.3.0 has 5 bugs
b) There are 5 tags after v2.3.0
c) The commit 1a2b3c4 is tagged v2.3.0-5
d) The current commit is 5 commits after the tag v2.3.0, and its abbreviated hash is 1a2b3c4 (the g stands for git)
Q14. Which command gives the number of commits per author, sorted from most to fewest?
a) git blame -c
b) git shortlog -sn
c) git log --authors
d) git count-objects
Q15. Why might you use git grep instead of a normal grep in a repository?
a) It also searches deleted files on disk
b) It edits files as it searches
c) It searches only tracked files, so build output and node_modules are skipped, it is fast, and it can search any commit or branch, for example git grep TODO v1.0
d) It only works on binary files
Quick revision notes
- git diff = working tree vs index; git diff --staged = index vs HEAD; git diff HEAD = both.
- A..B = commits in B not in A; diff A...B = changes on B since the merge base. HEAD~n follows first parents, HEAD^2 is a merge's second parent.
- format-patch makes patch files; git am turns them into commits; git apply only changes files.
- restore --staged unstages; switch -c creates a branch; git clean -n previews and -fd deletes untracked files.
- --depth 1 = shallow clone for CI; --filter=blob:none = partial clone with history; describe gives tag-based version strings.
Frequently asked questions
Should I use git switch and git restore instead of git checkout?
For new habits, yes. They do the same jobs as checkout but each has one clear purpose, so mistakes such as overwriting files when you meant to change branch are less likely. checkout still works and is still common in scripts and tutorials.
When should I use a shallow clone?
Use it in CI builds and deployment jobs that only need the latest code. Avoid it when the job needs history, for example to calculate versions with git describe, run blame or compare against older commits.
Are patches still used now that we have pull requests?
Yes. Projects such as the Linux kernel and Git itself accept patches by email, and patches are useful to move changes between repositories that are not connected or to share a fix without pushing a branch.
Comments
Post a Comment