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 "remote" in Git?

A local backup folder
A commit message format
A type of branch
A reference to a version of the repository hosted elsewhere, like on GitHubCorrect

A remote is just a nickname (usually 'origin') that Git uses to remember where another copy of your repository lives, typically on a server like GitHub, so commands like push and pull know where to send or fetch changes.

medium

2. What does interactive rebase (`git rebase -i`) let you do?

Reorder, squash, edit, or drop commits before replaying them onto another branchCorrect
Merge two unrelated repositories
Automatically resolve all conflicts
Only delete commits

Interactive rebase opens up your recent commits as an editable list before replaying them, so instead of just moving them as-is, you can reorder them, squash several messy ones into a single clean commit, tweak a commit message, or drop a commit entirely.

hard

3. You run `git stash` before switching branches, do some work, switch back, and run `git stash pop`. Git reports a conflict during the pop, and the stash entry is NOT removed from the stash list. What's the correct way to finish safely?

Run `git stash clear`, which resolves conflicts and removes only the failed entry
Run `git stash pop` again immediately; it will retry and auto-resolve the conflict on the second attempt
Resolve the conflicting files like any merge conflict, stage them, and only then manually run `git stash drop` to remove the entry, since Git deliberately leaves it in place until you confirm the changes were applied safelyCorrect
Ignore the conflict markers and commit directly; the stash is already dropped automatically once `pop` starts

`git stash pop` applies the stashed changes and then deletes the stash entry, but only if the apply succeeds cleanly; if a conflict occurs, Git leaves the entry in the stash list as a safety net rather than risk losing it. You resolve the conflict exactly as you would in a merge, stage the result, and then explicitly run `git stash drop` once you're satisfied nothing was lost. Running `git stash clear` would wipe out every stashed entry in the whole list, not just the one that failed, which is far more destructive than intended.

easy

4. You add `config.local.yml` to your `.gitignore` file, but it was already tracked and committed before you added the rule. What happens?

Git ignores the file starting from the next commit, with no extra command needed
Git keeps tracking the file and it still shows up in `git status` when changed, because `.gitignore` rules only apply to files Git doesn't already know aboutCorrect
Git immediately removes the file from your working directory
The file is automatically untracked and deleted from the remote on the next push

`.gitignore` only tells Git which untracked files to skip when staging; it has no effect on files that are already being tracked. To stop tracking the file you have to explicitly run `git rm --cached config.local.yml` and commit that removal, after which the ignore rule takes over. This is one of the most common `.gitignore` gotchas, since people expect the rule alone to be enough.

medium

5. 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
There is no difference, both are identical
`--mixed` unstages the last commit's changes but leaves them modified in your working directory; `--hard` discards those changes from the working directory entirelyCorrect
`--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

6. What does `git reflog` track, and when is it especially useful?

A record of every file ever committed
A local log of where HEAD and branch tips have pointed over time, useful for recovering commits after a reset, rebase, or accidental branch deletionCorrect
Only remote branch history
A list of merge conflicts

Git quietly keeps a local log of every place HEAD has pointed to, even after something like a reset or rebase moves it elsewhere. That log (the reflog) is often your only way back if you accidentally lose commits, since it remembers where you WERE, not just where you are now.

easy

7. What is a repository in Git?

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

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

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

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

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. A CI pipeline uses `git clone --depth 1` to speed up builds, but a later step that runs `git merge-base` against an older release branch, or tries to compute an accurate blame across the project's full history, starts failing or returning wrong results. Why?

Shallow clones automatically expire after 30 days
Shallow clones don't include the working directory files
Shallow clones can't create new commits at all
A shallow clone only has the most recent commit(s) and lacks the earlier history operations like merge-base or full blame need, unless you run `git fetch --unshallow`Correct

A shallow clone truncates history at the requested depth, so any operation needing to walk further back, like finding a common ancestor between two far-apart branches, or attributing very old lines of code, simply doesn't have the data to do so and will error out or give incomplete answers. The fix is `git fetch --unshallow` to backfill the full history, or to avoid `--depth` entirely for jobs that need it.

easy

10. 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 `post-merge` hook
A rule in `.gitignore`
A `pre-commit` hook script in `.git/hooks/`Correct
A Git alias

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.

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.