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. 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. 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. 3. What is a ref in Git internally?

    Correct answer: A name storing a commit hash, like refs/heads/main

    Refs live as small files or in packed-refs. HEAD is a symbolic ref pointing to another ref. Moving a branch is just writing a new hash into its ref.

  4. 4. What does git gc do?

    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. 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. 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. 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. 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. 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. 10. What is the difference between -s ours and -X ours in a merge?

    Correct answer: -s ours ignores the other side; -X ours favors us on conflicts

    The ours strategy records a merge but keeps our tree unchanged, discarding all their changes. The ours option of ort merges normally and only picks our side for conflicting hunks.

  11. 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. 12. How do you remove a leaked secret from a repository's whole history?

    Correct answer: Rewrite with filter-repo, force-push, rotate the secret

    filter-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. 13. Why is git filter-branch discouraged today?

    Correct answer: It's slow and error-prone; filter-repo is advised

    Git's own documentation warns about it. filter-repo is far faster and has safer defaults, for example for removing large files or paths.

  14. 14. A teammate force-pushed and your main diverged. 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. 15. What does a partial clone with --filter=blob:none do?

    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. 16. Why does git status get 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. 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. 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. 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. 20. How does git cherry or 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. 21. What does git rebase --update-refs help 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. 22. What problem does a merge queue solve?

    Correct answer: PRs that pass alone but break main when combined

    Two PRs can each be green against an old main yet conflict semantically. A merge queue tests each PR on top of the latest main plus the PRs ahead of it before merging.

  23. 23. Why should every commit on main build 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 bisect lands on failures unrelated to the bug. Squash merges or a "every commit is green" policy avoid that.

  24. 24. How do you handle untestable commits during git bisect?

    Correct answer: Mark them with git bisect skip

    Skipped 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. 25. What makes git bisect run ./test.sh work?

    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. 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. 27. What can a server-side pre-receive hook 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. 28. What does git notes let 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. 29. Why can a branch named feature block creating feature/login?

    Correct answer: Refs are paths; feature can't be file and folder

    Loose refs are stored as files under refs/heads/, so refs/heads/feature can't also be a directory. Teams pick a naming scheme like feat/... up front.

  30. 30. What is the index (staging area) technically?

    Correct answer: A binary file of paths, blob hashes and stat data

    .git/index stores 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. 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:file and :3:file print each one. Merge tools use them to show a three-way view.

  32. 32. How do you get exactly "their" version of a conflicted file during a merge?

    Correct answer: git checkout --theirs file

    --ours/--theirs pick a side from the index stages; then git add the file. Beware: during a rebase "ours" is the branch you're rebasing onto, so the meanings swap.

  33. 33. During git rebase main, what do "ours" and "theirs" refer to?

    Correct answer: Ours is main, the new base; theirs is your commit

    Rebase 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. 34. What does git replace do?

    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. 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 lock lets a team mark a file as being edited.

  36. 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. 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. 38. What does git clone --mirror produce?

    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 --mirror reproduces them elsewhere. It's how you move a repository between hosts.

  39. 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 maintenance keeps it up to date along with other optimizations.

  40. 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-refs make manageable.

  41. 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 CHANGE major. Tools like semantic-release or release-please then tag and publish in CI.

  42. 42. What does git log --follow struggle 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. 43. Why might git blame show the wrong author after a file was split into two?

    Correct answer: Moved lines look new unless -C detects copies

    -M detects 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. 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. 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. 46. Why do some teams forbid merge commits from main into 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. 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 main includes them. Every branch has to be rebased with --onto, which is why rewriting shared history needs coordination.

  48. 48. What is git bundle useful 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. 49. Why is git push origin main:production interesting?

    Correct answer: It pushes local main to a differently named branch

    The src:dst refspec form decides which local ref updates which remote ref. git push origin :feature (empty source) deletes feature on the remote.

  50. 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. 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. 52. What does git maintenance start set 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. 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 reads

    Mixed conventions give inconsistent history: some PRs as one commit, others as dozens. Platforms let you allow only one method per repository.

  54. 54. What is a symbolic ref?

    Correct answer: A ref that points to another ref, like HEAD

    .git/HEAD contains ref: refs/heads/main. When it contains a hash instead, you're in detached HEAD state. origin/HEAD is also symbolic, pointing to the remote's default branch.

  55. 55. How can you check that a repository's objects are not corrupted?

    Correct answer: git fsck

    fsck verifies connectivity and object integrity, and lists dangling objects (unreachable commits or blobs), which can also help recover lost work.

  56. 56. Why is git push --force to 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-lease and protect shared branches.

  57. 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. 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=fuller shows both.

  59. 59. What does git log --cherry-pick --left-right A...B reveal?

    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. 60. How do you prevent a huge package-lock.json diff from drowning the real change in review?

    Correct answer: Mark it linguist-generated in .gitattributes

    Attributes can collapse generated files in platform diffs and skip them in git diff, while the lockfile itself stays committed for reproducible installs.

  61. 61. What does a custom merge driver in .gitattributes allow?

    Correct answer: Merging a file type with your own tool or rule

    For example CHANGELOG.md merge=union keeps lines from both sides, avoiding conflicts on append-only files. The driver must also be configured in Git config.

  62. 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. 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. 64. How does git rerere interact 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. 65. Why might CI on a shallow clone compute the wrong set of changed files?

    Correct answer: The merge base with main may be missing from the history

    Diffing a PR against main needs their common ancestor. Fetch enough depth (or the base branch) before computing affected packages.

  66. 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-found lists them, and gc eventually prunes them.

  67. 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. 68. What does git switch --orphan docs create?

    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-pages branch that shares no history with the code.

  69. 69. Why can two developers see different results from the same git log command?

    Correct answer: Their local refs differ until they fetch

    Everything Git shows is local state. origin/main is as fresh as the last fetch, which explains many "works on my machine" Git confusions.

  70. 70. When is git merge --no-ff useful 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. 71. What does git range-diff give 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. 72. Why is git stash risky 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 clear or a new clone loses them. Use a WIP commit on a branch for anything you care about.

  73. 73. What does git log --merges --first-parent list on main?

    Correct answer: The merge commits on main, one per merged PR

    With merge-commit workflows this is effectively a list of merged pull requests, useful for release notes.

  74. 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. 75. Why is git worktree safer 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. 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 them

    If 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.