Git Exercises with Answers
Practise Git with 110 interactive exercises: multiple choice, fill-in-the-blank, output prediction and debugging. Every exercise gives instant feedback, a hint and the full solution.
Topic All topics Introduction init, add & commit log, diff & .gitignore Moving & Removing Files Config & Aliases Branches Merging & Conflicts Merge Conflicts Undoing Changes Stash Remotes, fetch & pull Tags & Releases Rebase Cherry-Pick Reflog & Recovery Bisect & Blame Git Hooks Submodules Worktrees Team Workflows Git Partial Staging and the Index Git HEAD, Detached HEAD and Revision Syntax Git .gitattributes and Line Ending Normalization Git Large File Storage and Binary Asset Workflows Sign and Verify Git Commits and Tags Git Patches with diff, apply, format-patch and am Git Sparse Checkout and Partial Clone Git Bundle Files for Offline Transfer and Recovery Reuse Git Conflict Resolutions with Rerere Git Refspecs and Explicit Force-With-Lease Checks
All types Fill in the blanks Multiple choice Predict the output
Any level Easy Medium Hard
Score 0 / 110
A A copy of Git B A saved snapshot of your project with a message C A folder on GitHub D A type of branch
Commits are the snapshots that make up a project's history.
Show answer
Why can you commit without an internet connection?
A Commits are stored on GitHub later B Git saves commits in the browser C Every repository contains the full history on your own computer D You can't; Git always needs the internet
Git is distributed, so commits are created locally.
Show answer
What does git --version show if Git is installed?
A Nothing B Your user name C The list of commits D A line like "git version 2.46.0"
It prints the installed Git version.
Show answer
Which command shows the version of Git you have installed?
A git --version B git version --show C git -v install D git status
It prints something like git version 2.45.0.
Show answer
Which command shows which files are staged, modified or untracked?
A git log B git show C git check D git status
git status describes the current state of your files.
Show answer
You changed index.html and style.css but only want to commit style.css. What do you run?
A git add style.css then git commit -m "..." B git add . then git commit C git commit style.css D git init style.css
Stage only the file you want, then commit.
Show answer
You create notes.txt and run git status without adding it. Where is it listed?
A Changes to be committed B Untracked files C Changes not staged for commit D It isn't listed
New files are untracked until you add them.
Show answer
You changed three files but only want to commit one. What do you do?
A git commit -a B git add that one file, then git commit C git add . D Delete the other two files
Only staged changes go into the commit.
Show answer
Which command shows the changes that will go into your next commit?
A git diff B git log C git status --all D git diff --staged
--staged compares the staging area with the last commit.
Show answer
Which line in a .gitignore file ignores every file ending in .log?
A *.log B log C .log/ D ignore .log
* is a wildcard that matches any name.
Show answer
In git diff output, what does a line starting with + mean?
A A comment B A line that was removed C A line that was added D A line that is unchanged
+ is added, - is removed.
Show answer
Which command shows the exact changes made in one commit?
A git log B git status C git diff --staged D git show <hash>
git show displays the commit's details and its diff.
Show answer
Complete the command that renames a file and stages the change.
git about. htm about. html
git mv renames or moves a file in one step.
Check answer Show solution
What does git rm --cached secrets.txt do?
A Deletes the file from disk and Git B Moves the file to the cache folder C Encrypts the file D Stops Git tracking the file but keeps it on disk
--cached only affects the staging area.
Show answer
Which command shows what git clean would delete, without deleting anything?
A git clean -n B git clean -f C git clean --show D git status --clean
-n is a dry run.
Show answer
What does git status --short show after git mv a.txt b.txt?
A D a.txt and ?? b.txt B M b.txt C Nothing D R a.txt -> b.txt
The rename is staged and shown with R.
Show answer
Why can't files removed by git clean -f be recovered with Git?
A They're encrypted B They were untracked, so Git never stored a copy C git clean also deletes history D They're moved to the reflog
Only committed (or stashed) content can be recovered.
Show answer
Complete the command that sets your global editor to VS Code.
git config core. editor "code --wait"
--global applies the setting to every repository for your user.
Check answer Show solution
Where are --global settings stored?
A In each repository's .git folder B On GitHub C In the system's Program Files only D In ~/.gitconfig in your home folder
Local settings go in .git/config.
Show answer
Global user.name is "Sam" and the repository's local user.name is "Sam (Work)". What does git config --get user.name print inside that repository?
A Sam B Sam (Work) C Both names D Nothing
Local settings override global ones.
Show answer
Complete the command that creates an alias git co for git switch.
git config --global switch
Aliases are stored under alias.<name>.
Check answer Show solution
Which setting helps when Windows and Mac users edit the same files?
A color.ui B core.autocrlf C pull.rebase D init.defaultBranch
It converts between CRLF and LF line endings.
Show answer
How can you tell which branch you're on in git branch output?
A It's listed first B It's in capital letters C It has a * next to it D It's called HEAD
The current branch is marked with an asterisk.
Show answer
You commit on feature-x, then switch to main. Is the new commit on main?
A No, it's only on feature-x until you merge B Yes, commits go to every branch C Only if you pushed it D Only the files, not the message
Branches are independent lines of commits.
Show answer
What does git branch -d old-idea do if old-idea has commits that were never merged?
A Deletes it silently B Merges it first C Renames it D Refuses, and suggests -D if you're sure
-d protects unmerged work; -D forces the delete.
Show answer
Which command deletes a branch that has already been merged?
A git branch -D name B git branch -d name C git delete name D git switch -d name
-d refuses if the branch isn't merged; -D forces it.
Show answer
When does Git do a fast-forward merge?
A When the current branch has no new commits since the other branch split off B When the branches have conflicts C When you use --fast D Every time
Git can simply move the label forward.
Show answer
After fixing a conflict in app.js, what do you run before committing?
A git merge --continue app.js B git resolve app.js C git add app.js D git push
Staging the file tells Git the conflict is resolved.
Show answer
What happens after git merge --abort during a conflict?
A The conflicting branch is deleted B Git keeps both versions of every file C The merge finishes with the current branch's version D Your files go back to how they were before the merge started
abort cancels the merge completely.
Show answer
When can Git do a fast-forward merge?
A Always B When the target branch has no new commits since the other branch started C Only when there are conflicts D Only on remote branches
Git simply moves the branch label forward.
Show answer
Complete the command that keeps your own branch's version of a conflicted file.
git checkout -- style. css
--ours keeps the version from the branch you're on.
Check answer Show solution
In conflict markers, what's between <<<<<<< HEAD and =======?
A The incoming branch's version B Your current branch's version C The original version D A comment from Git
HEAD is the branch you're on.
Show answer
After editing a conflicted file, how do you mark it as resolved?
A git resolve file B git add file C git commit file D git merge --continue file
Staging the file tells Git the conflict is fixed.
Show answer
What does the zdiff3 conflict style add?
A Colours B Line numbers C Automatic resolution D The original lines from the common ancestor
Seeing the original makes the right combination clear.
Show answer
During git rebase main, what does --theirs refer to?
A main B The remote branch C Your own commit being replayed D The common ancestor
In a rebase, ours and theirs are swapped compared with a merge.
Show answer
A bad commit has already been pushed and your teammates have pulled it. How should you undo it?
A git revert <hash> B git reset --hard C git commit --amend D Delete the repository
revert adds a new commit and doesn't rewrite shared history.
Show answer
What does git reset --soft HEAD~1 do?
A Deletes the last commit and its changes B Undoes the last push C Moves to the previous branch D Removes the last commit but keeps its changes staged
--soft keeps everything staged, ready to commit again.
Show answer
After git stash, what does git status show?
A A clean working tree B Your changes as staged C A merge conflict D An error
The changes are saved on the stash shelf, not in your files.
Show answer
A bad commit is already pushed and shared. What's the safest way to undo it?
A git reset --hard HEAD~1, then force push B Delete the repository C git revert <hash> D git commit --amend
revert adds a new commit that undoes the change, without rewriting shared history.
Show answer
What's the difference between git stash pop and git stash apply?
A pop removes the stash after applying it; apply keeps it B There's no difference C apply removes the stash; pop keeps it D pop only works on main
Use apply if you might reuse the stash.
Show answer
Predict the outputmedium Stash You run git stash twice. Which stash is stash@{0}?
A The first one you made B The most recent one C Whichever is larger D Neither; numbering starts at 1
Stashes form a stack with the newest on top.
Show answer
Multiple choicemedium Stash Which option includes untracked files in a stash?
A --all-files B --keep-index C -p D -u (--include-untracked)
Plain git stash only saves tracked files.
Show answer
What does git stash branch fix-wip stash@{0} do?
A Deletes the stash B Pushes the stash to GitHub C Creates a branch from the stash's original commit, applies the stash and drops it D Renames the stash
It's a clean way out when a stash conflicts with current work.
Show answer
Complete the command that downloads new commits without changing your files.
git
fetch only updates remote-tracking branches like origin/main.
Check answer Show solution
A A remote-tracking branch recording where main was on the remote at your last fetch B Your local main branch C A second copy of your files D A branch you should commit to
It's a read-only bookmark updated by fetch.
Show answer
A Only uploads your commits B Fetches and then integrates the remote branch into your current branch C Only downloads commits D Deletes your local changes
It's fetch plus merge (or rebase).
Show answer
git status says "Your branch is ahead of 'origin/main' by 3 commits." What should you do to share them?
A git fetch B git pull --force C git push D Nothing, they're already shared
Ahead means the remote doesn't have your commits yet.
Show answer
What's safer than git push --force when you must update your own rebased branch?
A git push --delete B git push --all C git push -u D git push --force-with-lease
It refuses if someone else pushed in the meantime.
Show answer
Complete the commands that replay your feature branch onto main.
git switch feature
git main
rebase main replays your commits on top of main's latest commit.
Check answer Show solution
What happens to the hashes of rebased commits?
A They stay the same B They're deleted C They're merged into one D They change, because the commits get new parents
Rebased commits are new commits with the same changes.
Show answer
After fixing a conflict during a rebase, what do you run?
A git merge --continue B git add <files>, then git rebase --continue C git commit D git rebase --skip
--skip would drop the commit instead.
Show answer
Predict the outputmedium Rebase In an interactive rebase, a commit is marked fixup. What happens to it?
A It's deleted entirely B Its message is changed C Its changes are combined into the previous commit and its message is discarded D It's moved to the end
squash would keep both messages.
Show answer
What does the golden rule of rebasing say?
A Don't rebase commits that others have already based work on B Always rebase main onto feature branches C Rebase only on Fridays D Never use merge
Rewriting shared history causes duplicated, tangled commits for everyone else.
Show answer
Complete the command that copies commit a1b2c3d onto the current branch.
git a1b2c3d
cherry-pick applies a single commit's changes as a new commit.
Check answer Show solution
Does cherry-pick move the original commit?
A Yes, it moves it to the current branch B No, it creates a copy with a new hash; the original stays where it was C It deletes the original D It renames the original branch
The original commit is untouched.
Show answer
What does the -x option add?
A An extra file B A new branch C A tag D A line in the commit message recording the original commit's hash
It makes copies traceable.
Show answer
Which commits does git cherry-pick A..C apply, if the history is A then B then C?
A B and C B A, B and C C Only C D A and C
A..C excludes A; use A^..C to include it.
Show answer
When is cherry-pick a poor choice?
A Copying an urgent fix to main B Backporting a fix to an old release C Bringing a whole feature branch up to date with main D Rescuing one commit from an abandoned branch
Use merge or rebase for whole branches.
Show answer
Complete the command that shows where HEAD has been.
git
The reflog lists every recent move of HEAD.
Check answer Show solution
A The second commit ever B The second branch C Where HEAD pointed two moves ago D Two commits ahead
Reflog entries count moves, not commits on a branch.
Show answer
Which of these can the reflog help you recover?
A Edits you never saved B Commits lost after git reset --hard C Changes you never committed D A repository you deleted from disk
It can only find commits that still exist in the local repository.
Show answer
Git says Deleted branch fix (was 7c6b5a4). Which command restores it?
A git restore fix B git branch fix 7c6b5a4 C git reflog fix D git undo branch fix
Recreate the branch label at the same commit.
Show answer
What does ORIG_HEAD point to after a rebase?
A The remote branch B The first commit of the repository C The newest commit on main D Where your branch was before the rebase started
Git sets it before risky operations like rebase, merge and reset.
Show answer
What search method does git bisect use?
A Binary search, halving the range each step B Checking every commit in order C Random sampling D Searching commit messages
That's why it needs so few steps.
Show answer
About how many tests does bisect need to search 1,000 commits?
A 1,000 B 100 C About 10 D 2
2 to the power of 10 is 1,024.
Show answer
In git bisect run, what exit code means "skip this commit"?
A 125 B 0 C 1 D 255
0 is good, 1 to 127 (except 125) is bad.
Show answer
Which command finds commits that added or removed the text "sendEmail"?
A git blame sendEmail B git grep --history sendEmail C git bisect sendEmail D git log -S "sendEmail"
-S is the pickaxe option.
Show answer
Complete the command that makes a hook script executable.
+ x . git / hooks/ pre- commit
chmod +x gives the file permission to run.
Check answer Show solution
Which hook runs before a commit is created?
A post-commit B pre-commit C pre-push D post-merge
pre-commit can block the commit.
Show answer
How does a hook stop a commit?
A By deleting the files B By printing "stop" C By running git reset D By exiting with a non-zero code, such as exit 1
exit 0 means continue; anything else stops.
Show answer
What does a commit-msg hook receive as $1?
A The branch name B The author's email C The path of a file containing the commit message D The commit hash
The hook reads the message from that file.
Show answer
Why should important rules also be checked in CI, not only in hooks?
A Hooks can be skipped with --no-verify and aren't installed automatically in every clone B Hooks are slow C CI runs faster D Hooks only work on Linux
CI runs on the server, where nobody can skip it.
Show answer
Complete the command that adds a submodule.
git add https: // github. com/ acme/ ui- kit. git vendor/ ui- kit
git submodule add clones the repository and registers it in .gitmodules.
Check answer Show solution
What does the parent repository store for a submodule?
A A full copy of its files B The URL and one exact commit to use C Only the URL D The submodule's branches
It's pinned to a commit until you update it.
Show answer
You cloned a project and the submodule folder is empty. What do you run?
A git pull --all B git clone again C git submodule add D git submodule update --init --recursive
It clones and checks out every submodule at its recorded commit.
Show answer
After git submodule update --remote, what else must you do so teammates get the new version?
A Nothing B Delete .gitmodules C git add the submodule path and commit in the parent D Run git clean
The new pointer is a change that must be committed.
Show answer
When is a package manager usually a better choice than a submodule?
A When the shared code is published as a versioned package B When the code isn't published anywhere C When you need an exact commit D Never
Package managers handle versions and updates more smoothly.
Show answer
Complete the command that creates a worktree with a new branch based on main.
git worktree . . / shop- hotfix -b hotfix/ login main
git worktree add creates the folder and checks out the branch.
Check answer Show solution
A A copy of the repository on GitHub B A list of commits C An extra folder with its own checked-out branch, sharing the same repository D A type of stash
All worktrees share one history.
Show answer
Why does git worktree add ../x feature/search fail if feature/search is open in your main folder?
A The folder name is wrong B A branch can be checked out in only one worktree at a time C Worktrees can't use existing branches D You need --force
Create a new branch, or use --detach for a read-only copy.
Show answer
You deleted a worktree folder with your file manager. What should you run?
A git worktree add B git clean -fd C git branch -D D git worktree prune
prune removes Git's records of worktrees that no longer exist.
Show answer
Why can a worktree be better than stashing for an urgent fix?
A Stashing deletes work B Your feature's files, editor and running tools stay untouched while you fix the bug in another folder C Worktrees are faster to push D Stashes can't be applied
There's no switching back and forth in one folder.
Show answer
Complete the command that starts a branch for a fix.
git switch fix/ login- redirect
switch -c creates a new branch and moves to it.
Check answer Show solution
In the feature branch workflow, where does new work happen?
A On a short-lived branch that's merged through a pull request B Directly on main C On the develop branch only D On a tag
main stays working and releasable.
Show answer
What makes trunk-based development different?
A It never uses main B Branches last for months C Everyone integrates into main at least daily, hiding unfinished work behind feature flags D It doesn't use commits
It keeps everyone close to the same code.
Show answer
Which commit message follows Conventional Commits?
A fix: correct total when basket is empty B Fixed the bug C FIX!!! D bugfix - stuff
type: short summary in the imperative.
Show answer
Which project best suits Git Flow?
A A personal blog deployed on every push B A mobile app with scheduled, numbered releases and hotfixes C A startup deploying twenty times a day D A one-off script
Its release and hotfix branches fit versioned releases.
Show answer
After the lab's second save, which price would an immediate commit record?
A 12, because commit always reads the latest saved file B 10, because that is the staged price C Both prices, because MM creates two commits
The index still contains ten; the later edit is unstaged.
Show answer
In the supplied lab, what does inspected..main list after creating inspected at v1?
A Both commits because the dots request every snapshot B No commits because creating a branch copies main's tip C The Second snapshot commit only
The range excludes history reachable from inspected, leaving main's second commit.
Show answer
Under the practice rules, which ending is requested for deploy.cmd at checkout?
A CRLF B LF because every Git file must use LF on disk C No checkout rule because .cmd files are always ignored
The explicit text eol=crlf rule governs matching command files.
Show answer
What should git show :banner.bin display after the lab stages the matching asset?
A The complete demoasset content as an ordinary tracked blob B An LFS pointer describing the content object C An empty file because LFS ignores binary paths
The clean filter supplies the pointer to the index while the working file remains real content.
Show answer
In the lab, which principal should %GS report after successful verification?
A The name of the main branch B The path notes.txt C learner@example.test
The allowed-signers entry associates the public key with learner@example.test.
Show answer
Why can git ls-tree show apps/api/server.txt after the lab hides that path?
A Sparse checkout created a deletion commit B The committed tree still contains the API file C ls-tree fetches files from an unrelated project
Sparse selection controls materialization without changing the commit tree.
Show answer
Why can restored import the incremental.bundle in the worked task?
A Every incremental bundle includes all history automatically B The notes.txt filename supplies the missing commit C It already contains the initial prerequisite commit
The receiver has the excluded base history from the complete bundle.
Show answer
After the second merge in the lab, why does git ls-files -u still list entries?
A autoupdate is false, so the reused working resolution still needs staging B Rerere deleted the working file C The first merge never recorded any resolution
The lab deliberately separates working-file reuse from index approval.
Show answer
Why does Bob's first push fail even though he uses force-with-lease?
A Bob's file cannot be tracked by Git B The remote main no longer matches the captured expected object C A lease always rejects every push
Alice advanced the remote after Bob captured its old tip.
Show answer
No exercises match those filters.