Staging Area
In short
The staging area in Git is a holding zone between your files and the next commit, where you place exactly the changes you want that commit to include.
What is the staging area in Git?
The staging area is where Git collects the changes that will go into your next commit. When you edit a file, the change exists only in your working directory, the ordinary project folder you see on disk. Running git add records the current version of that file in the staging area, and git commit then saves everything that is staged, and nothing else, as a new snapshot.
Internally, the staging area is a single file, .git/index, which is why Git's documentation also calls it the index. It lists every tracked file together with the version that will be committed, so if you keep editing a file after staging it, the newer edits stay unstaged until you run git add again. git status shows both groups, under 'Changes to be committed' and 'Changes not staged for commit'.
The staging area is like a packing table next to a shipping box: you gather items from around the room, check them on the table, and only then seal what is on the table into the box. This lets you split an afternoon of mixed edits into several small, focused commits. git add -p goes further and lets you stage only some of the changes inside a single file.
The staging area is often confused with a commit or a stash. Staged changes are only a draft of the next commit and are not yet part of the project history, while a commit is a permanent snapshot. A stash, made with git stash, is different again: it sets unfinished work aside on a separate shelf and cleans the working directory. To take a file out of the staging area without losing your edits, use git restore --staged <file>.
Key takeaways
- The staging area holds the changes that will go into the next commit.
git addstages changes, andgit commitsaves only what is staged.- Git stores the staging area in the
.git/indexfile, so it is also called the index. git add -pstages part of a file, which helps keep commits small and focused.git restore --staged <file>unstages a file while keeping your edits.
Example
# Edit two files, then stage only one of them
git add src/login.ts
git status
# Changes to be committed: modified: src/login.ts
# Changes not staged for commit: modified: src/styles.css
# Stage only some of the changes inside a file, hunk by hunk
git add -p src/styles.css
# Review exactly what the next commit will contain, then commit it
git diff --staged
git commit -m "Fix login form validation"
# Unstage a file but keep your edits
git restore --staged src/styles.cssReaders ask
What is the difference between staged and unstaged changes?
Staged changes have been added with git add and will be included in the next commit. Unstaged changes are edits in your working directory that Git has noticed but won't commit until you stage them.
Why is the staging area also called the index?
Git stores the staging area in a file named .git/index, so Git's commands and documentation often use the word index, as in git diff --cached. Staging area, index, and cache all refer to the same thing.
Can I commit without using the staging area?
Mostly, yes. git commit -a automatically stages every modified tracked file before committing, but it still skips new untracked files, which must be added with git add first.
See also
- CommitVersion Control, p. 5A commit is a saved snapshot of a project's files in Git, recorded with a unique ID, an author, a timestamp, and a message describing what changed.
- Git DiffVersion Control, p. 14Git diff is a Git command that shows the exact line-by-line differences between two versions of your code, such as working files, staged changes, or commits.
- Git ResetVersion Control, p. 20Git reset is a Git command that moves the current branch back to an earlier commit and, depending on its mode, also resets the staging area and your files.
- Git StashVersion Control, p. 22Git stash is a command that temporarily saves uncommitted changes and cleans the working directory, so you can switch tasks and restore the work later.
- GitVersion Control, p. 10Git is a free, open-source distributed version control system that tracks changes to files over time, so developers can collaborate and undo mistakes.
- .gitignoreVersion Control, p. 1A .gitignore file is a plain-text file listing patterns for files and folders Git should not track, such as dependencies, build output, logs, and secrets.
Spotted a mistake or something missing on this page?Suggest an edit