Git Senior cheat sheet: interview answers
Every question of this level with its correct answer and explanation, on one page for a quick review before the interview.
1. Which four object types make up Git's object database?
Correct answer: Blob, tree, commit and annotated tag
Blobs hold file contents, trees map names to blobs and subtrees, commits point to a tree and parents, and annotated tags point to an object with a message. Branches are refs, not objects.
2. Why does renaming a file not duplicate its content in Git's storage?
Correct answer: The blob is addressed by content; only the tree entry changes
Identical content always has the same hash, so it's stored once. Renames are detected later by comparing trees, not recorded explicitly.
3. What is a ref in Git internally?
Correct answer: A name storing a commit hash, like
refs/heads/mainRefs live as small files or in
packed-refs.HEADis a symbolic ref pointing to another ref. Moving a branch is just writing a new hash into its ref.4. What does
git gcdo?Correct answer: Packs objects, prunes unreachable ones and expires old reflogs
Loose objects are compressed into packfiles with deltas, and objects unreachable from refs and reflogs are removed after a grace period. Git runs it automatically.
5. How do packfiles keep repositories small?
Correct answer: Similar objects are stored as deltas against each other
Loose objects are full zlib-compressed snapshots; packing finds similar blobs and stores one fully and others as deltas. That's why snapshots don't cost as much as they sound.
6. Why can a commit that's no longer on any branch still be recovered?
Correct answer: The reflog keeps it reachable until it expires
Reflog entries count as references for
gc, by default 90 days for reachable and 30 for unreachable entries. After that the objects become prunable.7. What is Git moving to as an alternative to SHA-1, and why?
Correct answer: SHA-256, because SHA-1 collisions became practical
The SHAttered attack (2017) produced a SHA-1 collision. Git uses a hardened SHA-1 that detects known attacks and supports SHA-256 repositories, with interoperability still evolving.
8. What does a three-way merge compare?
Correct answer: Both branch tips against their merge base
If only one side changed a region relative to the base, its version wins; if both changed it differently, it's a conflict. Without the base Git couldn't tell additions from deletions.
9. What is
ort?Correct answer: Git's default merge strategy since 2.34
"Ostensibly recursive's twin" replaced
recursive: same results in most cases, much faster, with better rename handling. It still handles criss-cross histories by merging merge bases.10. What is the difference between
-s oursand-X oursin a merge?Correct answer:
-s oursignores the other side;-X oursfavors us on conflictsThe
oursstrategy records a merge but keeps our tree unchanged, discarding all their changes. Theoursoption ofortmerges normally and only picks our side for conflicting hunks.11. What is an octopus merge?
Correct answer: A merge commit with more than two parents
Git uses the octopus strategy when you merge several branches at once without conflicts. If there are conflicts it refuses, and you merge them one by one.
12. How do you remove a leaked secret from a repository's whole history?
Correct answer: Rewrite with
filter-repo, force-push, rotate the secretfilter-repo(or BFG) rewrites every commit; all collaborators must re-clone, and hosting caches and forks may keep copies. That's why rotating the secret is the step that actually matters.13. Why is
git filter-branchdiscouraged today?Correct answer: It's slow and error-prone;
filter-repois advisedGit's own documentation warns about it.
filter-repois far faster and has safer defaults, for example for removing large files or paths.14. A teammate force-pushed and your
maindiverged. What is the safest recovery?Correct answer: Fetch, then rebase your own commits onto the new tip
Your old commits are in the reflog;
git rebase --onto origin/main <old-base>moves only your own commits. Then agree on branch protection to prevent it next time.15. What does a partial clone with
--filter=blob:nonedo?Correct answer: Downloads commits and trees, fetching file contents on demand
Unlike a shallow clone you keep full history for log and blame, while old file versions download lazily. It's good for huge repos, but needs a server that supports it.
16. Why does
git statusget slow in very large repositories, and what helps?Correct answer: It stats every file; fsmonitor and sparse index reduce that
Status compares the working tree with the index by checking file metadata. A filesystem monitor reports only changed files, and sparse checkout with a sparse index shrinks what's scanned.
17. What is a refspec, like
+refs/heads/*:refs/remotes/origin/*?Correct answer: A mapping from remote refs to local refs for fetch or push
This default fetch refspec copies every remote branch into
origin/*;+allows non-fast-forward updates. Custom refspecs fetch PR refs or mirror repositories.18. Submodule vs subtree: what's the key trade-off?
Correct answer: Submodules pin a separate repo; subtree copies code into yours
Submodules keep histories separate but require extra commands from everyone. Subtree merges the other project's files into your history, so clones just work, but syncing back upstream is harder.
19. Why can a revert of a merge cause trouble when the branch is merged again later?
Correct answer: Git considers those commits merged, so their changes stay reverted
The branch's commits are already ancestors of
main, so the merge brings only newer commits. To restore the original work, revert the revert commit first.20. How does
git cherryor patch-id detection help after cherry-picking?Correct answer: It finds commits whose changes already exist upstream
Cherry-picked commits have different hashes but the same patch. Rebase also uses patch-ids to skip commits that are already upstream.
21. What does
git rebase --update-refshelp with in stacked branches?Correct answer: Moves dependent branch pointers along with the rebased commits
With PR #2 based on PR #1, rebasing the top branch also moves branch #1 to its rewritten commits, so the whole stack stays consistent.
22. What problem does a merge queue solve?
Correct answer: PRs that pass alone but break
mainwhen combinedTwo PRs can each be green against an old
mainyet conflict semantically. A merge queue tests each PR on top of the latestmainplus the PRs ahead of it before merging.23. Why should every commit on
mainbuild and pass tests, not just the PR's final state?Correct answer: So bisect and reverts work on any commit
If intermediate commits are broken,
git bisectlands on failures unrelated to the bug. Squash merges or a "every commit is green" policy avoid that.24. How do you handle untestable commits during
git bisect?Correct answer: Mark them with
git bisect skipSkipped commits are excluded and Git picks a nearby one. If the culprit is adjacent to skipped commits, Git reports a range instead of one commit.
25. What makes
git bisect run ./test.shwork?Correct answer: Exit code 0 means good, 1-127 bad, and 125 skip
Git runs the script at each step and interprets its exit status. A minimal reproduction script turns a long hunt into a fully automatic one.
26. Why might a signed commit still not prove the code is safe?
Correct answer: It proves who committed, not what the code does
A signature authenticates the key holder; a compromised key or a malicious maintainer still signs. Signing is one layer of supply-chain security alongside review and protected branches.
27. What can a server-side
pre-receivehook do that client hooks can't?Correct answer: Reject pushes for everyone, since clients can't skip it
Client hooks are optional and bypassable with
--no-verify. Server hooks (or platform rules) enforce policies like no force-push or no large files for every push.28. What does
git noteslet you attach?Correct answer: Extra metadata to commits without changing their hashes
Notes live in a separate ref (
refs/notes/commits), so CI results or review links can be added to existing commits. They aren't pushed or fetched by default.29. Why can a branch named
featureblock creatingfeature/login?Correct answer: Refs are paths;
featurecan't be file and folderLoose refs are stored as files under
refs/heads/, sorefs/heads/featurecan't also be a directory. Teams pick a naming scheme likefeat/...up front.30. What is the index (staging area) technically?
Correct answer: A binary file of paths, blob hashes and stat data
.git/indexstores the next commit's tree in flat form plus cached metadata (size, mtime), so Git can skip rehashing unchanged files. During conflicts it holds three stages per path.31. During a conflict, what are index stages 1, 2 and 3?
Correct answer: The merge base, our version and their version
git show :1:file,:2:fileand:3:fileprint each one. Merge tools use them to show a three-way view.32. How do you get exactly "their" version of a conflicted file during a merge?
Correct answer:
git checkout --theirs file--ours/--theirspick a side from the index stages; thengit addthe file. Beware: during a rebase "ours" is the branch you're rebasing onto, so the meanings swap.33. During
git rebase main, what do "ours" and "theirs" refer to?Correct answer: Ours is
main, the new base; theirs is your commitRebase checks out the base and applies your commits onto it, so from Git's point of view your commits are "theirs". It's a classic source of wrong conflict resolutions.
34. What does
git replacedo?Correct answer: Makes Git use one object in place of another
Replacement refs can graft old history onto a truncated repository or patch a bad commit's view. They're local unless you push
refs/replace/*, so use them carefully.35. What does Git LFS file locking address?
Correct answer: Unmergeable binaries edited by two people at once
Two edits to the same PSD or 3D model can't be merged.
git lfs locklets a team mark a file as being edited.36. Why are huge files committed by mistake a long-term problem even after deletion?
Correct answer: They stay in history, so every clone downloads them
Only history rewriting (
filter-repo --strip-blobs-bigger-than) removes them, with all the coordination costs. Pre-receive hooks or CI checks prevent them in the first place.37. What does Git protocol v2 improve?
Correct answer: Clients ask only for the refs they need, not all
Version 0 advertised every ref up front, which is slow for repositories with many branches and PR refs. v2 also makes adding capabilities easier.
38. What does
git clone --mirrorproduce?Correct answer: A bare repository with all refs, for backups or migrations
A mirror copies every ref (including notes and PR refs) and
git push --mirrorreproduces them elsewhere. It's how you move a repository between hosts.39. What is the point of the commit-graph file?
Correct answer: It speeds up history walks like log and merge-base
It caches parents and generation numbers, so Git doesn't parse every commit object.
git maintenancekeeps it up to date along with other optimizations.40. Why are stacked pull requests used?
Correct answer: To split a big change into small dependent PRs
Reviewers see focused diffs, and early parts can land sooner. The cost is restacking after changes, which tools and
--update-refsmake manageable.41. How do Conventional Commits enable automated releases?
Correct answer: Tools derive the next version and changelog from them
fix:means patch,feat:minor,BREAKING CHANGEmajor. Tools like semantic-release or release-please then tag and publish in CI.42. What does
git log --followstruggle with?Correct answer: It follows one path and guesses renames heuristically
Renames aren't stored, so Git infers them by similarity. Renaming and heavily editing in the same commit can break the chain; keep renames in separate commits.
43. Why might
git blameshow the wrong author after a file was split into two?Correct answer: Moved lines look new unless
-Cdetects copies-Mdetects moves within a file and-C(repeatable) detects code copied from other files in the same or earlier commits, so the original author is found.44. What is a criss-cross merge history?
Correct answer: Two branches merged into each other, so several bases
With more than one best common ancestor, Git's recursive or ort strategy first merges the bases into a virtual base. It's one reason to avoid merging branches back and forth.
45. How do you produce a release whose source can be reproduced exactly?
Correct answer: Build from a signed annotated tag with a committed lockfile
A tag pins the exact tree, a lockfile pins dependencies, and the signature proves who released it. CI then rebuilds the same artifact from the same inputs.
46. Why do some teams forbid merge commits from
maininto feature branches?Correct answer: They clutter history; rebasing keeps it linear
Repeated "Merge main into feature" commits add noise to the PR. The trade-off is that rebasing rewrites commits, which matters if others share the branch.
47. What happens to open pull requests when you rewrite
main's history?Correct answer: They may show unrelated diffs or conflicts and need rebasing
PR branches still contain the old commits, so the diff against the new
mainincludes them. Every branch has to be rebased with--onto, which is why rewriting shared history needs coordination.48. What is
git bundleuseful for?Correct answer: Moving commits as a single file without network access
A bundle can be cloned or fetched from like a remote. It's used for air-gapped transfers and some backup strategies.
49. Why is
git push origin main:productioninteresting?Correct answer: It pushes local
mainto a differently named branchThe
src:dstrefspec form decides which local ref updates which remote ref.git push origin :feature(empty source) deletesfeatureon the remote.50. What risk comes with
git config --global url."git@github.com:".insteadOf?Correct answer: None by itself; it rewrites HTTPS URLs to SSH everywhere
URL rewriting is handy for private dependencies, but it applies everywhere, including CI and package managers, which can surprise people when SSH keys aren't available.
51. How are credentials best handled for HTTPS remotes?
Correct answer: A credential helper or tokens, never passwords in the URL
Tokens in URLs leak into logs, shell history and screenshots. Credential helpers use the OS keychain; CI should use short-lived, scoped tokens.
52. What does
git maintenance startset up?Correct answer: Scheduled background tasks like prefetch and repack
It registers the repo with the system scheduler to prefetch, repack incrementally and update the commit-graph, keeping large repositories responsive.
53. Why is "merge commit" vs "squash" a team decision rather than a personal one?
Correct answer: It shapes
main's history that everyone bisects and readsMixed conventions give inconsistent history: some PRs as one commit, others as dozens. Platforms let you allow only one method per repository.
54. What is a symbolic ref?
Correct answer: A ref that points to another ref, like
HEAD.git/HEADcontainsref: refs/heads/main. When it contains a hash instead, you're in detached HEAD state.origin/HEADis also symbolic, pointing to the remote's default branch.55. How can you check that a repository's objects are not corrupted?
Correct answer:
git fsckfsckverifies connectivity and object integrity, and lists dangling objects (unreachable commits or blobs), which can also help recover lost work.56. Why is
git push --forceto a shared branch dangerous even with backups on CI?Correct answer: Others' new commits vanish and may be lost
Commits pushed by others after your last fetch disappear from the branch; they exist only in their local reflogs. Use
--force-with-leaseand protect shared branches.57. What is the trade-off of a monorepo with Git compared with many repositories?
Correct answer: Atomic cross-project changes, but scaling and access control are harder
One commit can change a library and its users together. In exchange you need sparse checkout, partial clones, path-based CI and CODEOWNERS as the repository grows.
58. Why do rebased commits keep their original author date but change the committer date?
Correct answer: Git tracks who wrote a change and who applied it
Author is the creator of the patch, committer is whoever last applied it (rebase, cherry-pick, amend).
git log --format=fullershows both.59. What does
git log --cherry-pick --left-right A...Breveal?Correct answer: Commits unique to each side, ignoring equivalent patches
When fixes were cherry-picked between branches, this shows what's genuinely missing on each side, which helps verify that a release branch has every backport.
60. How do you prevent a huge
package-lock.jsondiff from drowning the real change in review?Correct answer: Mark it
linguist-generatedin.gitattributesAttributes can collapse generated files in platform diffs and skip them in
git diff, while the lockfile itself stays committed for reproducible installs.61. What does a custom merge driver in
.gitattributesallow?Correct answer: Merging a file type with your own tool or rule
For example
CHANGELOG.md merge=unionkeeps lines from both sides, avoiding conflicts on append-only files. The driver must also be configured in Git config.62. Why is rewriting history in a fork-based open-source project especially disruptive?
Correct answer: Every fork and clone keeps the old commits and diverges
You can't force-push other people's forks. Contributors must rebase their work, and old commit links in issues may break.
63. What is the main reason Git can create branches in constant time?
Correct answer: A branch is just a 41-byte file with a commit hash
No files are copied; switching updates only files that differ between the two snapshots. This cheapness is what makes branch-heavy workflows practical.
64. How does
git rerereinteract with a long rebase you abort and redo?Correct answer: Recorded resolutions are reapplied when the same conflicts recur
Rerere keys resolutions by conflict content. It's useful for long-running integration branches that are rebuilt often, but always review what it applied.
65. Why might CI on a shallow clone compute the wrong set of changed files?
Correct answer: The merge base with
mainmay be missing from the historyDiffing a PR against
mainneeds their common ancestor. Fetch enough depth (or the base branch) before computing affected packages.66. What is a "dangling commit"?
Correct answer: A commit no ref or reflog entry points to
Dangling objects appear after amends, rebases or dropped stashes.
git fsck --lost-foundlists them, andgceventually prunes them.67. Why should you avoid putting secrets in commit messages, branch names or tags?
Correct answer: They're part of history and spread to every clone
Messages and refs are copied and indexed by platforms, CI logs and notifications just like files. Removing them requires the same history rewrite.
68. What does
git switch --orphan docscreate?Correct answer: A new branch with no history and an empty index
The first commit on an orphan branch has no parent. It's used for things like a
gh-pagesbranch that shares no history with the code.69. Why can two developers see different results from the same
git logcommand?Correct answer: Their local refs differ until they fetch
Everything Git shows is local state.
origin/mainis as fresh as the last fetch, which explains many "works on my machine" Git confusions.70. When is
git merge --no-ffuseful even when a fast-forward is possible?Correct answer: To keep a merge commit that groups a feature's commits
The merge commit marks where a feature started and ended, making it easy to revert the whole feature with one
git revert -m 1.71. What does
git range-diffgive a reviewer after a force-pushed PR update?Correct answer: Which commits changed, and how, between versions
Instead of re-reading the whole PR, the reviewer sees what the author actually changed in response to feedback, commit by commit.
72. Why is
git stashrisky as long-term storage?Correct answer: Stashes are local and easy to drop or lose
Stashes aren't pushed and live only in your repository;
git stash clearor a new clone loses them. Use a WIP commit on a branch for anything you care about.73. What does
git log --merges --first-parentlist onmain?Correct answer: The merge commits on
main, one per merged PRWith merge-commit workflows this is effectively a list of merged pull requests, useful for release notes.
74. What is the cost of a very large number of refs (for example millions of PR refs)?
Correct answer: Fetch and ref advertisement get slower
Protocol v2 and packed refs help, and hosts hide PR refs from default fetches. Custom refspecs fetch only what you need.
75. Why is
git worktreesafer than cloning the repo twice for parallel work?Correct answer: Worktrees share objects and refs, so stay in sync
Commits in one worktree are instantly visible in the others, and Git refuses to check out the same branch twice, preventing conflicting edits.
76. What happens to commit hashes when you rebase onto the same base without changes?
Correct answer: They change with
--force-rebase, else Git keeps themIf the branch is already on top of the base, rebase is a no-op. Forcing it re-applies commits with new committer dates, hence new hashes.