Back to sections

Git Quiz

Test what you actually know about Git. Free sample questions below, from easy to hard, with instant explanations.

easy

1. What does `git add` do?

Uploads changes to a remote
Stages changes, marking them to be included in the next commitCorrect
Permanently saves changes to history
Creates a new branch

git add doesn't save anything permanently yet, it just marks changes as 'include this in the next commit' by moving them into the staging area. The actual permanent save happens with git commit.

medium

2. What does `git reset --soft HEAD~1` do?

Reverts the last commit with a new commit
Permanently deletes the last commit and its changes
Deletes the last branch
Undoes the last commit but keeps its changes staged, ready to be committed againCorrect

--soft is the gentlest form of undo: it moves HEAD back one commit, but it doesn't touch your actual files, and it leaves that commit's changes sitting in the staging area, ready for you to re-commit (maybe combined with something else) rather than losing them.

hard

3. You need to fix a typo introduced three commits ago, but the two commits made after it are unrelated and should stay exactly as they are, each still recording its own separate history. What's the right approach?

`git reset --soft HEAD~3` and recommit everything as one new commit
`git rebase -i HEAD~3`, mark that commit `edit`, make your fix, `git commit --amend`, then `git rebase --continue`Correct
`git cherry-pick` the two later commits onto a fixed version of the typo commit, then delete the originals
`git commit --amend`, since amend can target any past commit by SHA

Interactive rebase lets you mark any past commit as `edit`, which pauses the rebase with that commit checked out so you can amend it, then `git rebase --continue` replays the remaining commits on top of your fix, each keeping its own identity, though with new SHAs since their parent changed. Plain `git commit --amend` only ever touches the single most recent commit, HEAD, it has no way to target an older commit directly.

easy

4. You want to see only the commits authored by jane@example.com in the current branch's history. Which command does that?

`git shortlog -s`
`git blame --author=jane@example.com`
`git log --author=jane@example.com`Correct
`git log --grep=jane@example.com`

`git log --author` filters the commit list down to those whose author field matches the given pattern. `--grep` searches commit messages instead of author identity, and `git blame` shows per-line authorship for a single file rather than filtering the log.

medium

5. Your team wants every `git push` to automatically run the test suite locally first, and abort the push if any test fails, without relying on CI to catch it later. Which hook should this logic live in?

`pre-receive` (configured on the client)
`commit-msg`
`post-commit`
`pre-push`Correct

The `pre-push` hook runs locally right before a push actually sends data to the remote, and if the script exits non-zero, Git aborts the push, making it the right place for a local test-suite gate. `pre-receive` is a server-side hook that runs on the remote when it receives a push, it isn't something you configure locally, and `post-commit`/`commit-msg` run at commit time, before a push is even happening.

hard

6. You need to find which of 200 commits introduced a regression, but manually checking out and testing each candidate by hand would take hours. How can you automate the entire bisection?

`git bisect skip` on every commit until one fails
`git blame` on the affected file to see its most recent change
`git log -S"bug"` to search for the string that caused the bug
`git bisect run ./test-script.sh`, letting Git automatically check out each candidate, run the script, and use its exit code to mark good/badCorrect

`git bisect run` takes a script or command, checks out each bisection candidate automatically, runs it, and uses the exit code to decide the next step: 0 means good, any code from 1 to 124 means bad, and the special code 125 means "can't test this commit, skip it." This turns what would be manual back-and-forth into a single unattended command that finishes with the exact first-bad commit already identified.

easy

7. You want Git to automatically run a linter and reject the commit outright if it fails, before the commit object is even created. Which mechanism should you set up?

A `pre-commit` hook script in `.git/hooks/`Correct
A `post-merge` hook
A Git alias
A rule in `.gitignore`

The pre-commit hook runs right before Git finalizes a commit, letting a script inspect the staged content and abort the commit, by exiting non-zero, if something's wrong, such as a failing linter. A post-merge hook only fires after a merge has already completed, so it can't block anything, and an alias is just a shortcut for typing commands, not an enforcement mechanism.

medium

8. What does `git blame <file>` show?

A file's complete diff history
The total number of bugs in a file
A list of everyone who has ever cloned the repository
Which commit and author last modified each line of a fileCorrect

git blame goes line by line through a file and tells you exactly which commit (and which author) last touched each one, which is exactly what you want when you're trying to figure out who wrote a weird line of code and why.

hard

9. After running a history-rewriting tool to remove a huge binary file that was accidentally committed two years ago, the team is confused that `.git` is still enormous and the old commits still seem retrievable. What else needs to happen for the space to actually be reclaimed and the old blobs to truly go away?

Nothing more is needed, the rewrite already deleted the blobs, it just takes time to show up
Running `git commit --amend` one more time
Simply deleting the `.git/objects` folder and letting Git rebuild it
Old branches/tags still pointing at the pre-rewrite commits need to be removed, reflog entries referencing them need to expire (`git reflog expire --expire=now --all`), `git gc --prune=now` needs to run locally, AND every clone or fork elsewhere still holds the old blobs until it also re-clones or rewritesCorrect

Rewriting history creates new commits with new SHAs, but the old commits and the blob they reference aren't actually gone until nothing still references them: leftover refs and stale reflog entries keep them reachable, so a full cleanup needs those cleared plus `git gc --prune=now` to actually delete now-unreachable objects. Even then, that's only true locally, since anyone else's clone or fork of the repo still holds the old, unrewritten history in full until they re-clone or perform the same rewrite and force-push, which is exactly why history rewrites are so disruptive for shared repositories.

easy

10. What is the staging area (also called the index) in Git?

An intermediate area where changes are collected before being included in a commitCorrect
The remote repository
A folder for deleted files
A cloud backup service

Think of the staging area as a holding pen between your files and your commit history: git add moves changes there, and only what's actually in that holding pen is what git commit will save, which is why you can choose to commit some changes but not others.

Ready for the real thing?

Take the full timed Git quiz and see your score.

    Welcome to OpsQuiz!

    Real scenario-based DevOps questions, hands-on practice, and clear explanations for every answer.