Git Staging Environment
Between your working folder and the history sits the staging area (also called the index). You put changes there with git add, and git commit saves exactly what is staged. This two-step design lets you decide what each commit contains.
working folder --git add--> staging area --git commit--> repository (history)Think of packing a parcel: you can edit many files, but you choose which ones go in the box before you seal it.
Staging files
mkdir shop && cd shop
git init -q
echo "<h1>Shop</h1>" > index.html
echo "h1 { color: navy; }" > style.css
echo "console.log('cart');" > cart.js
git add index.html style.css
git statusOn branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: index.html
new file: style.css
Untracked files:
(use "git add <file>..." to include in what will be committed)
cart.js
Only the two staged files are under Changes to be committed. cart.js stays untracked and will not be part of the next commit.
Ways to stage
| Command | Stages |
|---|---|
git add file.txt |
one file |
git add src/ |
everything inside a folder |
git add *.css |
files matching a pattern (expanded by the shell) |
git add . |
every change in the current folder and below |
git add -A |
every change in the whole repository, including deletions |
git add -u |
only changes to already tracked files (no new files) |
git add -p |
interactively pick individual hunks (in your own terminal) |
mkdir shop && cd shop
git init -q
echo "v1" > index.html
echo "v1" > notes.txt
git add -A && git commit -q -m "Start"
echo "v2" > index.html
rm notes.txt
echo "new" > cart.js
git add -u
git status -s
echo "--- now add everything:"
git add -A
git status -sM index.html
D notes.txt
?? cart.js
--- now add everything:
A cart.js
M index.html
D notes.txt
git add -u staged the modification and the deletion but left the new cart.js untracked; git add -A took everything.
Staging is a snapshot of the file
git add copies the file's current content into the staging area. If you edit the file again afterwards, the new edit is not staged automatically:
mkdir shop && cd shop
git init -q
echo "price: 10" > item.txt
git add item.txt
echo "price: 12" > item.txt
git status -s
echo "--- staged version:"
git show :item.txt
echo "--- working version:"
cat item.txtAM item.txt
--- staged version:
price: 10
--- working version:
price: 12
AM: the file is staged as a new file (A) and modified again since (M). git show :item.txt (a colon and a path) prints the staged copy. Run git add again to stage the newer content.
Unstaging
Staged something by mistake? git restore --staged takes it out of the staging area without touching your edits:
mkdir shop && cd shop
git init -q
echo "app" > app.js && git add app.js && git commit -q -m "Add app"
echo "API_KEY=secret123" > .env
echo "app v2" > app.js
git add .
git status -s
git restore --staged .env
echo "--- after restore --staged:"
git status -sA .env
M app.js
--- after restore --staged:
M app.js
?? .env
The secret file is back to untracked and app.js is still staged. (On older Git versions you will see git reset HEAD <file> suggested for the same job.)
Tip: Run
git statusbefore every commit. It costs a second and catches staged secrets, forgotten files and debug leftovers.
Test yourself
You run git add page.html, then edit page.html again. What will git commit save?
- The newest edit
- The version from when you ran git add
- Nothing, the file is now in conflict
- Both versions
Which command stages modifications and deletions of tracked files but ignores new files?
- git add .
- git add -A
- git add -u
- git commit -a
How do you unstage .env without losing its content?
- git rm .env
- git restore --staged .env
- git checkout .env
- rm .env