skip to content

Commits and History

What a commit really is — a tree of blobs plus parent pointers, addressed by hash — and how the staging area, amend, rebase, and reflog let you reshape history. Interviewers ask how you would recover a commit you were sure you had lost.

on this pageshow

questions

4

Why does a version-control system keep a staging area separate from the working copy?

level: juniorimportance: must knowfreq 74%

answer

  1. Three pictures, not two
  2. One of them has not happened yet
  3. You choose what goes in
  4. A messy morning, two clean commits

basics

~20 s

The staging area holds an explicit selection of what the next commit will contain, so you can record one logical change out of a messy working copy instead of committing everything you happen to have edited.

solid answer

~40 s

A version-controlled workspace holds three separate pictures of the project: the **last recorded commit**, the **working copy** on disk, and the **staging area** between them. The staging area is a proposal - exactly what the next commit would contain if you recorded it this second. Keeping it separate makes committing a deliberate act: you review what you have, add the changes belonging to one logical unit, and leave the rest for a later commit. That is what lets an afternoon of mixed edits - a defect fix plus an unrelated rename - become two clean commits rather than one unreviewable one. It also gives you an inspection step before anything becomes permanent history. A system with only two pictures has to treat every commit as an all-or-nothing capture of whatever is on disk.

go deeper

for a junior

Be ready to name the three pictures - last recorded commit, staging area, working copy - and to say that the staging area describes the next commit rather than the current files. Knowing that staging is a choice, not a formality, is most of the junior answer.

for a middle

Explain the mechanics: the selection is explicit, it can be built up over several passes while you keep editing, and what you have staged is what you can inspect before it becomes permanent. Show concretely how you would turn one messy working copy into two commits.

for a senior

Connect the layer to the quality of history a team lives with. An interviewer expects you to talk about reviewability, undoing one change cleanly, and what history reads like months later, plus the discipline you enforce when commits arrive as intervals of time.

for a principal

Own the tradeoff between commit hygiene and throughput. Be ready to say where you would spend team attention - review expectations, tooling, or deliberately nothing - and to argue why the shape of history is or is not worth slowing a delivery team down for.

## Three pictures of the same project A version-controlled workspace holds three separate descriptions of the same project at the same moment, and most early confusion comes from collapsing two of them into one. - **The last recorded commit** - the project exactly as history already knows it. Editing a file on disk does not touch it. - **The working copy** - the files as they sit on disk right now, including half-finished experiments, stray debug output, and a rename you started and lost interest in. - **The staging area** - an explicit selection sitting between the two. It answers exactly one question: *if I recorded a commit this second, what would be in it?* | Picture | What it describes | It changes when | |---|---|---| | Last recorded commit | The project as history already has it | You record a new commit | | Staging area | The proposed contents of the next commit | You add a change to, or remove one from, the selection | | Working copy | The files as they sit on disk | You edit, create, or delete a file | The staging area is the only one of the three that describes something that has not happened yet. That is the whole idea. A commit is a permanent public statement; the staging area is the draft of it. ## Why a proposal layer is worth having A system with only two pictures - last commit and working copy - has to treat committing as an all-or-nothing capture: whatever is on disk is what gets recorded. The third picture buys three things. 1. **Selection.** You choose which of the changes on disk belong to one logical unit of work, and leave the rest for the next commit. Without this layer, the shape of your history is decided by the order in which you happened to touch files. 2. **Review before permanence.** What you have staged can be inspected as a single proposal before it becomes a commit that other people will read, revert, or search through. Catching a stray temporary file at this point costs nothing; catching it afterwards means rewriting history. 3. **Progressive assembly.** You can stage part of the work, keep editing, and stage more, building the commit over several passes while the working copy keeps moving underneath it. ## A concrete morning An engineer on a bike-share operations team spends a morning inside the dock-availability service. Two things come out of it: a genuine fix to an off-by-one in the 37-minute rebalancing window, and a rename of a badly chosen variable that touches 14 unrelated files because the name was irritating. Recorded as one commit, that is a change nobody can review. On a team where the median wait for a review is already 2 days, a reviewer opening a 15-file change containing one real line of logic will either skim it or park it for another day. And the newly-hired half of the team, reading history six months later to work out why the rebalancing window is what it is, finds the fix buried under mechanical churn. Staging the service file alone, recording it with a message about the rebalancing defect, then staging the rename and recording that separately, costs about thirty seconds and produces two commits that each explain themselves. That is what an interviewer is actually probing: not the mechanics of the layer, but whether you decide what a commit contains or let the state of your disk decide for you. ## What the staging area is not - **Not a backup.** Staged work that is never committed is not protected history; it lives in one working area on one machine and nothing else knows about it. - **Not a performance optimisation.** It exists so that you can choose, not to make recording a commit faster. - **Not shared.** Nothing you stage is visible to anyone else. Work becomes shareable only once it is committed and published. - **Not the commit itself.** Staging is a proposal; the commit is the record. Nothing enters history until you record it. - **Not mandatory to use well.** Sweeping every change on disk into the selection in one gesture is legal and extremely common - it simply throws away the only reason the layer exists. ## The habit an interviewer is listening for The strong answer connects the layer to the shape of the resulting history: the staging area exists so that a commit can be one idea rather than one interval of time. A candidate who describes it only as "the place files go before you commit" has described the mechanism and missed the point. A candidate who says "it lets me split what is on my disk into the two commits it actually is, so each one can be reviewed and reverted on its own" has answered the question.

  • Your working copy holds a defect fix and an unrelated rename. How do you end up with two commits?
    Stage only the change belonging to the fix, record it with a message about the defect, then stage the rename and record that separately. The staging area is what makes the split possible without setting the other work aside or undoing it. The reviewer gets one small change to reason about, and the mechanical rename is obvious enough to skim in seconds.
  • What is lost when someone habitually stages every change on disk in one sweep?
    The shape of history stops being a decision. Commits become intervals of time rather than units of meaning, so reviews mix signal with churn, undoing a single change means untangling it from unrelated edits, and anyone reading history later cannot tell which lines belonged to which intent.
  • Is staged-but-uncommitted work safe?
    No. Staging is a selection inside one working area on one machine. It is not published, not versioned, and not part of the record, because it never entered history. Only recording a commit makes work part of what the repository actually preserves.

The shelf is your working copy, the counter where you set the things you have decided to buy is the staging area, and the receipt is the commit.

saying these in an interview costs you the question

  • Calls the staging area a speed optimisation
  • Treats staging and committing as one operation
  • Says a commit always captures the whole working copy
  • Cannot split mixed edits into two commits
  • Believes staged work is backed up or shared
open as a page

Why is a version-control commit a full snapshot rather than a stored diff?

level: middleimportance: must knowfreq 79%

basics

~20 s

A commit records the complete state of the file set at one moment, plus a pointer to the commit it was built on and who made it and why. Differences between two commits are computed on demand, never stored.

open as a page

Why is a version-control commit's identifier derived from its own contents?

level: middleimportance: should knowfreq 46%

basics

~20 s

The identifier is a hash over the commit's snapshot, parent pointer, authorship metadata and message, so changing anything produces a different identifier. Two copies of history that share an identifier therefore share everything behind it.

open as a page

When commit history is rewritten, what happens to the original commits?

level: seniorimportance: should knowfreq 57%

basics

~20 s

Nothing edits them. A rewrite builds new commits carrying the intended content and moves the line's pointer to them; the originals stay in the repository, unreferenced, until a cleanup pass removes them. Anyone holding the old identifiers now has a diverged copy.

open as a page