Git interview questions and answers, from basics to recovery scenarios
Git interview questions check that you can explain what a commit and a branch really are, choose between merge and rebase, undo mistakes without damaging shared history, and recover work that looks lost. Expect quick definition questions, a few "which command would you use" situations, and at least one recovery scenario such as a bad force push or a hard reset. Strong answers say what each command does to the commit graph, not only which flags to type.
By Evin JohnPublished 8 min read
How Git shows up in technical interviews
Few companies run a whole round on Git. Instead it appears as a block of questions in a developer screen, a DevOps or release engineering interview, or a practical task where you clone a repository and are asked to tidy a branch before opening a pull request. Junior candidates are asked definitions and everyday commands. Experienced candidates are asked about team workflow: how they keep history readable, how they handle a broken main branch, and what they do when someone force-pushes over a colleague's work. A software engineer interview loop often includes one or two of these questions without warning.
Core concepts: what Git actually stores
| Question | What it tests | What a strong answer shows |
|---|---|---|
| What is a commit? | Object model | A snapshot of the whole project (a tree), plus author, message and a pointer to one or more parent commits. Its ID is a hash of that content, so changing anything changes the ID. |
| What are blobs, trees and commits? | Internals | A blob is file content, a tree maps names to blobs and other trees, and a commit points at one top-level tree. Annotated tags are a fourth object type. |
| What is a branch? | Refs | A small file holding one commit ID. Committing moves the current branch forward. Creating a branch is cheap because nothing is copied. |
| What is HEAD, and what is a detached HEAD? | Refs | HEAD normally points at a branch. Detached means it points straight at a commit, so new commits belong to no branch and are easy to lose unless you create one. |
| What are the working tree, the index and the repository? | Three areas | You edit files in the working tree, stage a chosen set into the index with git add, and git commit records the index as a snapshot. |
git fetch versus git pull? | Remotes | Fetch downloads new commits and updates remote-tracking branches such as origin/main without touching your work. Pull is fetch followed by a merge or a rebase. |
What does git diff --staged show that git diff does not? | Daily use | Plain diff compares the working tree with the index; --staged compares the index with the last commit, which is what your next commit will contain. |
| I added a file to .gitignore but Git still tracks it. Why? | Ignore rules | Ignore rules only apply to untracked files. Stop tracking it with git rm --cached <file> and commit. |
| Lightweight versus annotated tags? | Releases | A lightweight tag is just a name for a commit. An annotated tag is an object with a tagger, date and message, and it can be signed. Use annotated tags for releases. |
| What is a fast-forward merge? | Merging | When the target branch has not moved since you branched, Git just moves its pointer to your tip. No merge commit is created unless you ask for one with --no-ff. |
Merge versus rebase
This is the question almost every Git block contains. Merging joins two lines of work with a new commit that has two parents; nothing existing is rewritten, so it is safe on shared branches and history shows exactly when work was combined. Rebasing replays your commits one by one on top of another base, creating new commits with new IDs; the result is a straight line that is easier to read and bisect, but anyone who had the old commits now has a diverged copy. The usual team rule: rebase your own unpublished feature branch to keep it current, merge (or squash-merge) into main, and never rebase a branch other people have pulled.
git switch feature/search
git fetch origin
git rebase origin/main # replay my commits on the latest main
# on a conflict: fix the files, then
git add src/search.ts
git rebase --continue # or: git rebase --abort to go back
git push --force-with-lease # refuses if someone else pushed in the meantime- Why `--force-with-lease` and not `--force`? A plain force push overwrites whatever is on the remote. With a lease, Git refuses if the remote branch moved since you last fetched, so you do not silently delete a teammate's commit.
- What does interactive rebase let you do? Reorder, squash, edit, reword or drop commits before you share them.
git commit --fixup <sha>followed bygit rebase -i --autosquashfolds review fixes into the commit they belong to. - Squash merge, merge commit or rebase merge for pull requests? Squash gives one commit per pull request and a clean main, but loses the individual steps. A merge commit keeps everything and records the merge. Rebase-and-merge keeps each commit in a straight line. Teams pick one and enforce it in the repository settings.
- What is `git pull --rebase` for? It rebases your local unpushed commits on top of what you fetched, instead of creating a small merge commit every time you sync.
- What is cherry-pick, and when is it a smell? It copies one commit onto the current branch as a new commit. Fine for a hotfix to a release branch; a habit of cherry-picking between long-lived branches usually means the branching model is wrong.
Undoing changes: reset, revert, restore
| Situation | Command | Why |
|---|---|---|
| Throw away edits to one file | git restore <file> | Copies the file back from the index. git restore --staged <file> unstages it instead. |
| Fix the message or contents of the last local commit | git commit --amend | Replaces the last commit with a new one. If it was already pushed, others will need the force-pushed version. |
| Undo the last commit but keep the changes staged | git reset --soft HEAD~1 | Moves the branch back one commit; index and working tree are untouched. |
| Undo the last commit and unstage its changes | git reset HEAD~1 | The default mixed mode resets the index too, leaving the edits in your files. |
| Discard the last commit and its changes entirely | git reset --hard HEAD~1 | Moves the branch and overwrites index and files. Uncommitted work is gone; the commit itself can still be found in the reflog. |
| Undo a commit that is already on a shared branch | git revert <sha> | Adds a new commit that applies the inverse change. History is not rewritten, so nobody else is disturbed. |
| Undo a merge that is already on main | git revert -m 1 <merge-sha> | -m 1 keeps the first parent (main) as the line to return to. Re-merging the same branch later needs the revert reverted first. |
Recovery and debugging questions
- What is the reflog? A local log of every position HEAD and each branch tip has had: commits, resets, rebases, checkouts. It is not pushed or shared, and old entries expire (after 90 days by default for most entries), but it is usually how lost work comes back.
- How do you get back a branch you deleted? Find the tip in
git reflog(or the hash printed when you deleted it) and rungit branch <name> <sha>. - You rebased and the result is wrong. How do you go back?
ORIG_HEADpoints at where the branch was before the rebase, sogit reset --hard ORIG_HEADright afterwards undoes it. Later, find the pre-rebase entry in the reflog. - How do you find the commit that introduced a bug?
git bisect: mark a known good and a known bad commit, and Git binary-searches between them. With a test script,git bisect rundoes it unattended. - A password was committed and pushed. What now? Rotate the secret first, because it is already exposed. Then remove it from history with a tool such as git filter-repo, force-push, ask everyone to re-clone, and add a secret scanner to stop it recurring.
- You need to switch branches but have half-finished work.
git stash push -m "note"shelves it andgit stash popbrings it back, or commit it as a work-in-progress commit you amend later. A second worktree (git worktree add) avoids switching at all. - Who changed this line and why?
git blamegives the commit, andgit log -Lfollows how a range of lines evolved. Read the commit message and the linked pull request, not only the name.
git reset --hard HEAD~3 # oops: three commits gone from the branch
git reflog # find the line from before the reset
# a1b2c3d HEAD@{1}: commit: add search ranking
git reset --hard HEAD@{1} # move the branch back to it
# or keep the current branch and park the old tip elsewhere:
git branch rescue a1b2c3dgit bisect start
git bisect bad HEAD
git bisect good v2.3.0
git bisect run ./check-price.sh # any script: exit 0 = good, 1 = bad
git bisect reset # return to where you startedConflicts and branching strategy questions
| Question | What it tests | What a strong answer shows |
|---|---|---|
| How do you resolve a merge conflict? | Conflict handling | Read both sides between the markers, decide what the combined code should be (often neither side as written), remove the markers, run the tests, then stage and continue. |
| How do you reduce conflicts on a busy team? | Workflow | Small, short-lived branches merged often, formatting enforced by a tool, ownership of hot files, and rebasing onto main daily. |
| Trunk-based development versus Git Flow? | Strategy | Trunk-based keeps everyone close to main with short branches and feature flags, which suits continuous delivery. Git Flow adds develop, release and hotfix branches, which suits versioned releases supported in parallel. |
| How do feature flags change branching? | Delivery | Unfinished work can merge to main switched off, so branches stay short and releases are decoupled from merges. |
| How would you protect main? | Governance | Branch protection: required reviews, required status checks, no direct or force pushes, and optionally a linear history or signed commits. |
| Monorepo or many repositories? | Architecture | A monorepo makes cross-project changes atomic and tooling shared, at the cost of build and checkout scale. Many repositories give clear ownership but make coordinated changes harder. |
| How do you handle large binary files? | Scale | Keep them out of normal history: Git LFS or an artifact store. Every version of a binary in history is kept forever and slows every clone. |
| Submodules or a subtree? | Dependencies | A submodule pins another repository at a commit and needs extra commands to update. A subtree copies the code in, simpler for consumers but harder to send changes back upstream. |
| How do you speed up cloning a huge repository? | Scale | A shallow clone (--depth 1) for CI, a partial clone (--filter=blob:none), and sparse checkout to fetch only the directories you need. |
| What do Git hooks do? | Automation | Client hooks such as pre-commit and commit-msg run checks locally and can be skipped. Server-side checks and CI are the ones you can rely on. |
Fictional example
- Interviewer
- You pull this morning and two of your commits from yesterday are no longer on the shared release branch. What do you do?
- Candidate
- First I avoid making it worse: I do not pull again or reset my local copy. My local branch or my reflog still has those commits, so I find their hashes with
git reflogand check them withgit show. Then I talk to the teammate before pushing anything, because they may have rewritten the branch on purpose and we need one agreed version. Usually the fix is to rebase or cherry-pick my two commits onto the new tip and push normally. Afterwards I would suggest branch protection on release branches so force pushes are blocked, and--force-with-leaseas the team habit for personal branches. What I want the team to take away is that this is a process gap, not one person's mistake.
Short questions for experienced developers
- Why do commit hashes change after a rebase even if the code is identical? The hash covers the parent and the timestamps, and both change.
- What is `git rerere`? "Reuse recorded resolution": once enabled, Git remembers how you resolved a conflict and applies the same fix next time it appears, which helps with long rebases.
- How do you write a good commit message? A short imperative summary line, a blank line, then why the change was made. The diff already shows what.
- What does `git gc` do? Packs loose objects and removes unreachable ones that have expired, which is why lost commits are only recoverable for a while.
- How would you sign commits and why? With a GPG, SSH or S/MIME key, so a host can show that a commit came from a verified key. It proves the key, not that the code is good.
Git practice plan
- Draw the graphIn a scratch repository, make commits on two branches and draw the graph after every merge, rebase and reset.
git log --oneline --graph --allchecks your drawing. - Break things on purposeHard-reset away three commits, delete a branch, botch a rebase, then recover each one using only the reflog.
- Create and resolve conflictsEdit the same lines on two branches, resolve once with merge and once with rebase, and notice how many times you resolve during the rebase.
- Bisect a real bugPlant a failing test somewhere in twenty commits and let
git bisect runfind it. - Say the answers aloudExplain merge versus rebase and reset versus revert in under a minute each, with when you would use each one.
Git questions sit beside command-line and design topics. Linux interview questions covers the rest of the shell work, microservices interview questions is where repository and deployment strategy meet, and REST API interview questions covers the service contracts those branches ship.
Most Git answers fail on precision, not knowledge: saying "reset" when you mean "revert" changes the whole answer. Natively, an AI interview assistant for Mac and Windows, gives you a transcript of a mock round so you can catch those slips afterwards. During practice it can read a screenshot of a git log graph or a conflict and explain what happened, and with Pro or the free trial it can use the résumé and job description you add. Every session is saved with its notes for later review.
Questions people ask
What Git questions are asked in interviews?
What a commit and a branch are, fetch versus pull, merge versus rebase, reset versus revert, how to resolve conflicts, how to recover lost commits with the reflog, and which branching strategy a team should use.
What is the difference between git merge and git rebase?
Merge joins two branches with a new commit and rewrites nothing. Rebase replays your commits onto another base as new commits, giving a straight history. Do not rebase commits other people already have.
What is the difference between git reset and git revert?
Reset moves the branch pointer back and can discard commits, so it is for local history. Revert adds a new commit that undoes an earlier one, so it is safe on shared branches.
How do I recover a commit after git reset --hard?
Run git reflog, find the entry from before the reset, and reset back to it or create a branch at that hash. Uncommitted changes are not in the reflog and cannot be recovered this way.
Where to go next
Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.
Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.
