Staging Area
The intermediate snapshot in Git between the working files and the next commit, which lets a developer choose exactly which changes go into each commit, down to individual lines.
The staging area, also called the index, is Git's holding area for the next commit. Git tracks three versions of a project at once: the last commit, the files on disk as the developer edits them, and the staging area in between. Running git add copies a file's current contents into the staging area. Committing then records exactly what's staged, not whatever happens to be on disk; see Commit and Git Object Model.
The point is control over what each commit contains. A developer who fixes a bug and, along the way, tidies some unrelated formatting can stage the fix alone, commit it with its own message, and commit the tidying separately. Commits that each do one thing are easier to review, to revert and to search for the source of a bug; see Atomic Commit and Git Bisect.
Staging can be finer than whole files. Interactive staging walks through the changes in a file chunk by chunk, called hunks, and asks which to include. Graphical tools and editors let developers stage single lines. The staging area therefore holds a version of the file that may never have existed on disk, which is powerful and occasionally a trap: a commit can pass review but fail to build because the staged version lacks a change still sitting unstaged; see Continuous Integration.
Under the hood, the index is a single binary file in the repository. Beyond recording what to commit, it caches information about each tracked file, such as size and modification time, so Git can tell which files changed without reading them all, which keeps status checks fast on large projects. During a merge, the index holds up to three versions of each conflicted file, from the common ancestor and both sides, which is how Git knows which files still need resolving; see Merge Conflict.
The staging area is one of the features newcomers find most confusing. Most earlier systems, and some later ones, had nothing like it: commit simply recorded every change in the working files. Mercurial had no index by default. Some newer Git-compatible tools drop the staging area altogether, treating the working copy itself as a commit that's continually amended and split afterward if needed.
Related commands interact with it constantly. Stashing saves both the staged and unstaged changes and clears them. Rewriting history before sharing often goes back to splitting or combining commits that were staged badly the first time; see Interactive Rebase. A pre-commit Git Hook can check the staged snapshot rather than the files on disk, so a Code Formatter sees exactly what is about to be recorded.
See also2
Diff
The listing of changes between two versions of a file, the algorithms that compute it, and the unified format that code review, patches, and merges are built on.
Version Control & Delivery13 connections
Commit Message
The note attached to each change in version control, conventionally a short summary line and a body explaining why, which becomes the main record of intent for anyone who later wonders why the code is the way it is.
Version Control & Delivery13 connections
Linked from10
- Atomic CommitVersion Control & Delivery
A commit containing exactly one logical change, complete and independently sound.
- CommitVersion Control & Delivery
A recorded snapshot of a project with a message explaining it.
- Commit MessageVersion Control & Delivery
The note attached to each change in version control, conventionally a short summary line and a body explaining why, which becomes the main record of intent for anyone who later wonders why the code is the way it is.
- Git HookVersion Control & Delivery
Scripts Git runs automatically at points such as committing or pushing, used to check formatting and messages locally, and on servers to enforce policy or trigger deploys.
- Git Object ModelVersion Control & Delivery
Git stores every version of a project as a graph of immutable objects named by the hashes of their contents, blobs for files, trees for directories, and commits for snapshots, which makes history tamper-evident and identical content free to store twice.
- Git StashVersion Control & Delivery
Git's shelf for uncommitted work, which clears the working tree so you can switch tasks, and the reasons a throwaway commit or a second worktree is often the better shelf.
- Interactive RebaseVersion Control & Delivery
Git's tool for rewriting a series of commits before sharing them, reordering, combining, splitting, and rewording them so the history tells a clear story rather than recording every false start.
- Squash MergeVersion Control & Delivery
Collapsing a branch's commits into one before merging, trading granularity for a clean main history.
- Three-Way MergeVersion Control & Delivery
Combining two lines of work by comparing both against their common ancestor.
- Version ControlVersion Control & Delivery
A system recording the history of a project's files, enabling branching, merging, and recovery.