Back to sections

Git Interview Questions & Answers

A sample of real Git interview questions with full answers and explanations - practice for interviews or certification exams.

easy

1. What is a repository in Git?

A project's full set of files plus its complete tracked historyCorrect
A remote server only
A user account
A single file

A Git repository isn't just your current files, it's your files PLUS the entire history of every change ever made to them, all stored together in the hidden .git folder.

medium

2. Before opening a pull request, you want to combine five messy "WIP" commits into one clean commit, keeping only the first commit's message. During `git rebase -i`, which action should you mark on the later four commits?

`edit`, so Git pauses at each one and lets you split it into separate commits
`reword`, so only their commit messages change without touching any code
`squash` (or `fixup` if you also want to discard their commit messages), so each one's changes get folded into the commit listed above itCorrect
`drop`, so their changes are removed entirely and only the first commit remains

In an interactive rebase, `squash` combines a commit's changes into the one above it and opens an editor so you can merge both commit messages together, while `fixup` does the same merge but silently discards the later message. `drop` would throw away the actual code changes, which isn't what's wanted here, and `reword` only edits a message without touching content at all. Knowing the distinct rebase todo verbs (pick, reword, edit, squash, fixup, drop) is what separates a clean PR history from an accidental loss of work.

hard

3. What is a "dangling commit," and how can you typically recover one?

A commit that exists in the repository's object database but is no longer referenced by any branch or tag, often recoverable via `git reflog` or `git fsck`Correct
A commit made on a deleted remote
A commit that failed to push
A commit with a syntax error

Git doesn't delete commit data the instant nothing points to it anymore, it just becomes 'dangling', still sitting in Git's storage but orphaned from any branch or tag. Since it's not actually gone yet, tools like git reflog or git fsck can often help you find and recover it before it eventually gets cleaned up.

easy

4. What does `git log` show?

The list of staged files
A list of remote repositories
The commit history, including author, date, and message for each commitCorrect
Uncommitted changes

git log is your project's diary: it lists every commit in order, newest first, along with who made it, when, and what message they left, so you can trace exactly how the project got to where it is.

medium

5. What does `git cherry-pick <commit>` do?

Deletes a specific commit from history
Merges an entire branch
Renames a commit
Applies the changes from one specific commit onto your current branch, as a new commitCorrect

Cherry-pick lets you grab just the changes from ONE specific commit somewhere else and replay them onto your current branch as a brand-new commit, without dragging along everything else that commit's original branch has done.

hard

6. What problem does the `-m` (mainline) option address when cherry-picking a merge commit?

It automatically resolves conflicts during cherry-pick
It merges the commit instead of cherry-picking it
A merge commit has multiple parents, so cherry-pick needs to know which parent to diff against to determine what changes to apply; -m specifies that parent numberCorrect
It has no special purpose for merge commits

A normal commit has exactly one parent, so Git always knows what changed. A merge commit has two or more parents (since it combined two branches), so when you cherry-pick it, Git has no way to know which parent's changes you actually want, unless you tell it with -m which parent to compare against.

easy

7. What does `git add` do?

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

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

8. You run `git reset --mixed HEAD~1` on one checkout, and separately `git reset --hard HEAD~1` on another. Both move the branch pointer back one commit. What's the key difference in what happens to your files?

`--mixed` deletes the commit from the reflog; `--hard` keeps it there
`--mixed` unstages the last commit's changes but leaves them modified in your working directory; `--hard` discards those changes from the working directory entirelyCorrect
There is no difference, both are identical
`--mixed` only works on the current branch; `--hard` can reset any branch

All reset modes move HEAD and the branch pointer, but differ in how far they reach: `--soft` leaves the index and working directory untouched, `--mixed` (the default) also resets the index but leaves your working directory files modified, and `--hard` additionally overwrites the working directory to match the new HEAD, discarding those changes completely. That's why `--hard` is the dangerous one: uncommitted work in your files is gone, not just unstaged.

hard

9. What does the `ORIG_HEAD` reference typically point to, and when is it useful?

The commit HEAD pointed to immediately before a potentially history-altering operation like a reset, merge, or rebase, letting you recover the prior stateCorrect
The current remote tracking branch
The commit with the oldest timestamp
The very first commit in the repository

Before Git lets a risky operation (like reset or rebase) move HEAD somewhere new, it quietly saves where HEAD WAS into ORIG_HEAD. So if that operation turns out to be a mistake, you can jump straight back with `git reset --hard ORIG_HEAD` instead of hunting through history.

easy

10. On GitHub, some commits show a green 'Verified' badge next to them. What does that badge indicate?

The commit was cryptographically signed (GPG or SSH) with a key matching a verified identity on the accountCorrect
The commit only changed already-approved files
The commit was reviewed and approved by another team member
The commit has passed all CI tests

A signed commit carries a cryptographic signature made with the author's GPG or SSH key, and GitHub shows 'Verified' once it can confirm that signature against a key registered to that account, proving the commit genuinely came from that person and wasn't altered afterward. It says nothing about CI status or code review, those are tracked separately through checks and pull request approvals.

Want to practice under real conditions?

Try the full timed Git quiz.

    Welcome to OpsQuiz!

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