Bite-sized, standalone interview questions and answers for Git. Read 5-10 per sitting. Each answer is 2-4 sentences max and stands on its own.

35 questions shown. Filter by keyword or difficulty below.

Topic: The object model (Order 0)

Git is a content-addressed object database with a DAG of commits and a layer of movable refs (pointers) on top, not a folder of file versions. Every object — blob, tree, commit, tag — is addressed by its SHA-1 hash, and history is the set of objects reachable by following parent pointers. Those three ideas (objects, refs, DAG) explain every Git command.

Git writes each changed file as a blob (zlib-compressed bytes named by SHA-1("blob <size>\0" + content)), writes a tree listing every path and its blob/subtree hash, then writes a commit object holding that tree hash, the parent hash(es), author/committer, and message. Finally it moves the current branch ref to point at the new commit. The snapshot is reconstructed later by walking those hashes.

A blob is the raw zlib-compressed bytes of one file with no name or path; a tree lists (mode, filename, object-hash) entries pointing at blobs or subtrees, capturing one commit’s directory structure; a commit holds a tree hash, zero-or-more parent hashes, author/committer, and a message. A commit points to one top-level tree, which recursively references every file in the repo at that moment.

Every object is named by the SHA-1 of its content, so identical content always yields the same hash and is stored once, and any byte change produces a different hash. Because a commit’s hash also covers its parent hash, editing an old commit changes its hash and ripples forward to every descendant. The DAG is therefore tamper-evident: you cannot silently alter history.

A DAG (directed acyclic graph) is Git’s history: each commit points back to its parent(s) via hashes, edges run child-to-parent, and there are no cycles. A commit with two parents is a merge commit; the root commit has none. This shape is what makes branching cheap (a branch is just a pointer into the graph) and rebase possible (replaying commits re-roots subtrees).

Objects are immutable and content-addressed: once written, a blob, tree, or commit never changes, and its hash is its identity. Refs, by contrast, are mutable pointers into the object graph — moving a branch just rewrites a 40-character file under refs/heads/. That separation is the whole game: history is the set of objects reachable from a ref, and “history” changes when the ref moves, not when objects change.

Topic: Branching & history (Order 1)

A branch is not a copy of files — it is a plain ref file under refs/heads/ whose contents are the 40-char hash of the commit it points to. Creating a branch writes one tiny file; moving a branch just rewrites that file to a new commit hash, so branches are cheap. The branch “grows” because every new commit updates the ref to the new commit’s hash.

HEAD is a special ref saying where you are: normally it points at a branch name (e.g. ref: refs/heads/main), so new commits advance that branch. When HEAD points directly at a commit hash instead of a branch, you are in detached HEAD state — commits still get created as objects but no branch ref moves to track them.

A detached HEAD means HEAD holds a raw commit hash rather than a branch, which happens when you check out a tag, a past commit, or a remote ref. Commits made in this state exist as objects but are on no branch, so the tip ref does not move — unless you create a branch from them, they become unreachable and are eventually pruned. Fix it with git switch -c save-my-work before you lose the tip.

A lightweight tag is just a ref file pointing straight at a commit, storing no extra metadata. An annotated tag is a full Git object (like a commit) holding a target hash, tagger name/date, and an optional message, addressed by its own SHA-1. Use annotated tags for releases so the tag itself records who created it and when.

The working tree is the checked-out files on disk where you edit; the index (staging area) holds the next tree to be committed as a flattened path→blob-hash map; and HEAD points at the current commit. git add moves content from working tree into the index, git commit snapshots whatever is in the index, and git reset/checkout move content between them. Understanding these three splits is what makes add/reset/checkout predictable.

Topic: Remotes & collaboration (Order 2)

fetch only downloads objects and updates remote-tracking refs (origin/main) — it never touches your branches or working tree. pull is fetch plus an integration step (merge or rebase) onto your current branch, which can rewrite your working tree and surface conflicts. Fetch is safe and read-only; pull is the operation most likely to surprise you.

A remote is a named, bookmarked URL (default origin) for another repository you fetch from or push to. Git stores remotes under refs/remotes/<remote>/ as read-only tracking refs that mirror the remote’s branches after a fetch. The remote is just a pointer to a location; the actual data lives in your local object database once fetched.

pull is fetch plus integration, and a conflict means the same region of a file changed differently on both sides so Git couldn’t auto-pick a winner. Git wrote both versions into the file with <<<<<<< / ======= / >>>>>>> markers and paused in a MERGING state. Edit the file to the correct result, git add it to clear the marker, then continue — don’t abort blindly.

A remote-tracking ref (origin/main) is a read-only mirror updated by fetch — you never commit to it directly. A tracking branch is a local branch configured to follow that remote-tracking ref, so ahead/behind counts and default push/pull targets work automatically. Upstream is the linked remote-tracking ref, stored as branch.<name>.remote and branch.<name>.merge — purely local metadata.

A remote accepts a push only if it is a fast-forward — your history must contain the remote’s current tip — so it never silently clobbers others’ work. A non-fast-forward is rejected, and force-push (--force, or safer --force-with-lease) overrides this and can discard the remote’s commits. The rule: never rewrite commits that have left your machine; only force-push history nobody else has pulled.

git stash temporarily shelves your working-tree and index changes onto a stack (the ref refs/stash) without committing, so you can switch branches and reapply later. Under the hood Git creates objects for the working-tree and index states, sometimes as a merge commit to capture untracked files. git stash pop reapplies and drops the entry; git stash list shows what is saved.

Topic: Rebase vs merge (Order 3)

Merge combines two divergent histories by creating a commit with two parents — the tips of the branches being joined. The merge commit’s tree is the result of a three-way merge using the two tips and their nearest common ancestor (the merge base). Because the branch refs are untouched, merge preserves the original DAG exactly as it happened.

A fast-forward happens when the target branch has no commits the current branch lacks and the current branch is an ancestor of the target; instead of creating a merge commit, Git simply moves the ref forward to the same commit. The history stays linear and no new object is created. Pass --no-ff to force a real merge commit even when a fast-forward is possible, preserving a record that a feature branch existed.

Rebase replays a sequence of commits onto a new base by computing each commit’s diff against its parent, then applying that diff in order onto the new base and creating new commits with new hashes. The old commits still exist in the object database (reachable via reflog) until pruned, but the branch ref now points at the rewritten chain. The history is rewritten, not moved.

A commit’s hash covers its parent hash, so replaying a commit onto a new base changes its parent, which changes its hash. The content diff is preserved but the object identity is not — the same logical edits get fresh SHA-1s. This is the root of the “never rebase a shared branch” rule: teammates’ old hashes no longer exist on your branch.

Prefer merge for shared history and rebase to tidy local work. Rebase rewrites history with new hashes, so doing it on a branch teammates already pulled makes their history diverge and pushes get rejected as non-fast-forward. Rebase private feature branches onto main to get a clean linear history; merge shared branches to preserve the actual divergence that happened.

Cherry-pick applies the diff introduced by a single existing commit onto the current HEAD as a brand-new commit with a new hash — effectively a one-commit rebase. Git computes the change between that commit and its parent, then replays it. The result is the same logical edit but a different object with its own author/committer timestamps.

Because rebase creates new commit hashes, your branch’s history now diverges from theirs: their old hashes are “missing” from your branch and a push is rejected as non-fast-forward. Your teammate’s local history no longer lines up with the rewritten chain, so they must reconcile. The rule is absolute: never rebase commits that have left your machine.

Topic: Internals & recovery (Order 4)

The reflog is a per-ref (and global HEAD) append-only log of every place a ref has pointed, with a timestamp and the command that moved it. It is local to your repository and is what makes “lost” commits recoverable — a hard reset or rebase leaves the old tips in HEAD’s reflog for 30–90 days. git reflog is the standard escape hatch when you think you destroyed commits.