
This first Git set covers the commands you use every single day: staging and committing, reading history, and creating and merging branches. Each explanation shows what Git does to three places: your working files, the staging area and the commit history.
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
- Working tree, staging area and commits
- git log and HEAD
- Creating and switching branches
- Fast-forward vs three-way merges
- Resolving merge conflicts and .gitignore
- git restore and git reset basics
Git MCQs with answers and explanations
Q1. What does git add app.js do?
a) Copies the current version of app.js into the staging area (index), ready for the next commit
b) Creates a commit containing app.js
c) Starts tracking app.js but ignores its current changes
d) Uploads app.js to the remote repository
Q2. You edit a.js and b.js, run git add a.js and then git commit -m "Fix". What is in the commit?
a) Nothing, because b.js was not staged
b) Both files' changes
c) Only the changes to a.js; the b.js changes stay as unstaged changes in the working tree
d) Only b.js
Q3. What does git commit -a -m "msg" include?
a) Only files already in the staging area
b) All branches
c) Modified and deleted files that Git already tracks, staged automatically; new untracked files are left out
d) Every file in the folder, including new untracked files
Q4. Which command shows the commit history as one short line per commit?
a) git diff --stat
b) git log --oneline
c) git status --short
d) git show --all
Q5. In Git, what does HEAD normally point to?
a) The branch you currently have checked out, and through it, that branch's latest commit
b) The staging area
c) The latest commit on the remote
d) The first commit in the repository
Q6. Which command creates a new branch named feature/login and switches to it in one step?
a) git switch -c feature/login
b) git commit -b feature/login
c) git merge feature/login
d) git branch feature/login
Q7. You run git merge feature on main, and main has no new commits since feature branched off. What does Git do by default?
a) A fast-forward: it moves the main pointer forward to feature's latest commit without creating a merge commit
b) Refuses to merge
c) Rebases feature onto main
d) Creates a merge commit with two parents
Q8. Both main and feature have new commits since they diverged. What does git merge feature on main create?
a) A new branch
b) A copy of each feature commit with new hashes
c) A fast-forward
d) A merge commit with two parents, built by a three-way merge that compares both branch tips with their common ancestor (merge base)
Q9. Why would a team run git merge --no-ff feature?
a) To force a merge commit even when a fast-forward is possible, so the history shows where the feature branch was merged
b) To skip conflict checks
c) To delete the feature branch
d) To merge without committing
Q10. During a merge, a file contains the lines <<<<<<< HEAD, ======= and >>>>>>> feature. What should you do?
a) Edit the file to the correct final content, remove the markers, then git add it and finish with git commit (or git merge --continue)
b) Run git log
c) Run git push to let the server decide
d) Delete the file
Q11. You add .env to .gitignore, but git status still shows changes to .env. Why?
a) .gitignore must be in the .git folder
b) Dotfiles cannot be ignored
c) .env was already tracked; .gitignore only affects untracked files, so you must run git rm --cached .env and commit
d) .gitignore only works after a push
Q12. You staged config.js by mistake. Which command unstages it but keeps your edits in the working tree?
a) git restore config.js
b) git restore --staged config.js
c) git rm config.js
d) git reset --hard
Q13. What does git restore styles.css (without --staged) do?
a) Restores a deleted branch
b) Unstages styles.css
c) Throws away the unstaged changes in styles.css, replacing it with the staged version (or the HEAD version if nothing is staged)
d) Shows the file's history
Q14. You want to undo your last commit but keep its changes staged so you can recommit them. Which command fits?
a) git restore HEAD~1
b) git reset --mixed HEAD~1
c) git reset --soft HEAD~1
d) git reset --hard HEAD~1
Q15. After git checkout 3f2a9c1 (a commit hash), Git says you are in "detached HEAD" state. What does this mean?
a) The repository is corrupted
b) You cannot view files
c) The remote has been removed
d) HEAD points directly at a commit instead of a branch, so new commits will not belong to any branch unless you create one
Quick revision notes
- Working tree → git add → staging area → git commit → history. commit -a stages tracked files only.
- HEAD points to the current branch; a commit hash checkout gives detached HEAD.
- Fast-forward moves the pointer; diverged branches get a three-way merge commit; --no-ff forces a merge commit.
- Fix conflicts by editing, removing markers, git add, then commit; git merge --abort cancels.
- restore --staged unstages. restore throws away local edits. reset --soft, --mixed and --hard keep less and less of your work.
Frequently asked questions
What is the difference between git switch and git checkout?
git switch only changes branches, and git restore only restores files. git checkout does both jobs, which was confusing, so Git 2.23 added these two clearer commands.
Is it safe to use git reset on commits I have already pushed?
Not really. It rewrites your branch, so anyone who already pulled those commits will have a different history from yours. For pushed commits, git revert is the safer way to undo things.
How often should I commit?
Commit small, logical pieces of work with clear messages. Small commits are easier to review, easier to revert, and easier to bisect when you are hunting a bug.
Comments
Post a Comment