Git Add: Staging Changes
At the end of a working day, your project folder is rarely tidy. You fixed a bug, started a new feature, corrected a spelling mistake in the README and left yourself a file of private notes. These are four different pieces of work, and they don't belong in one commit.
git add is how you choose. It copies the changes you name into the staging area, and only those changes go into the next commit. You can add one file, a folder, everything, or even a few lines out of a file.
In this lesson, you will learn each form of git add, see what it stages, and learn to stage part of a file with git add -p.
The Starting Point
Navika's Fee Tracker repository has one commit. Since then she has created four files in three places and edited the README:
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
git add README.md
git commit --quiet -m "Add project README"
mkdir src docs
echo "class FeeCalculator {}" > src/FeeCalculator.java
echo "class Student {}" > src/Student.java
echo "How to set up the project" > docs/setup.md
echo "Ideas for later" > notes.txt
echo "Tracks school fee payments." >> README.md
git status -s -uall M README.md
?? docs/setup.md
?? notes.txt
?? src/FeeCalculator.java
?? src/Student.javaOne modified file and four untracked files. (-uall asks git status to list every untracked file. Without it, a new folder is shown as one line, such as ?? src/.)
Syntax
git add <file-or-folder>...| Form | What it stages |
|---|---|
git add <file> | That one file |
git add <file1> <file2> | Each file you list |
git add <folder> | Every change inside that folder |
git add . | Every change in the current folder and the folders below it |
git add -A | Every change in the whole repository, wherever you are standing |
git add -u | Changes to tracked files only: modified and deleted files, but no new files |
git add -p | Parts of files, which you choose one by one |
git add -n <path> | Nothing. It only lists what would be staged (a dry run). |
"Every change" means new files, modified files and deleted files.
Add One File
git add src/FeeCalculator.java
git status -s -uall M README.md
A src/FeeCalculator.java
?? docs/setup.md
?? notes.txt
?? src/Student.javaOnly src/FeeCalculator.java moved. Its code changed from ?? to A : a new file, staged. This is the most careful form of git add, and a good default while you are learning.
Add a Folder
Name a folder and Git stages every change inside it:
git add src
git status -s -uall M README.md
A src/FeeCalculator.java
A src/Student.java
?? docs/setup.md
?? notes.txtsrc/Student.java is staged now too. Files outside src were not touched.
Add Only Tracked Files with -u
-u stands for "update". It stages changes to files Git already tracks and skips new files:
git add -u
git status -s -uallM README.md
A src/FeeCalculator.java
A src/Student.java
?? docs/setup.md
?? notes.txtREADME.md moved from M to M : its change is staged. The two untracked files stayed untracked. -u is useful when your folder contains new files that you are not ready to commit.
Add Everything
git add . stages every change in the current folder and below. The dot means "this folder". Because it takes everything, look first. The -n option shows what would be added without adding it:
git add -n .add 'docs/setup.md'
add 'notes.txt'The list includes notes.txt, Navika's private notes. Suppose she doesn't notice and adds everything:
git add .
git status -sM README.md
A docs/setup.md
A notes.txt
A src/FeeCalculator.java
A src/Student.javaEvery file is staged, including the one that should not be. This is the most common staging accident, and the fix is easy as long as you catch it before the commit.
Take a File Back Out
To remove a file from the staging area, use git restore --staged. The file itself is not changed or deleted. It only leaves the staging area:
git restore --staged notes.txt
git status -sM README.md
A docs/setup.md
A src/FeeCalculator.java
A src/Student.java
?? notes.txtnotes.txt is untracked again, and the other four files are still staged. Now the commit contains what Navika meant:
git commit -m "Add fee calculator, student class and setup guide"[main 7d7d752] Add fee calculator, student class and setup guide
4 files changed, 4 insertions(+)
create mode 100644 docs/setup.md
create mode 100644 src/FeeCalculator.java
create mode 100644 src/Student.javagit status suggests git restore --staged itself, in the hint under "Changes to be committed". The lessons on undoing changes cover it in full.
git add . vs git add -A
These two do the same thing when you are in the top folder of the repository. They differ when you are inside a subfolder:
git add .stages changes in the folder you are standing in, and below.git add -Astages changes in the whole repository.
Navika moves into src, creates a file there, and also edits the notes file one level up:
cd src
echo "class Payment {}" > Payment.java
echo "Call the bus contractor" >> ../notes.txt
git add .
git status -sA Payment.java
?? ../notes.txtOnly the file inside src was staged. ../notes.txt is outside the current folder, so git add . left it alone. (Paths in git status are shown from where you stand, which is why the notes file appears as ../notes.txt.) From here, git add -A would have staged both.
Stage Part of a File with -p
Sometimes one file holds two unrelated changes. Navika has a fee list in the repository:
cd ..
cat > fees.txt << 'EOF'
Fee list for 2026
Term 1 fee: $12,000
Term 2 fee: $12,500
Term 3 fee: $13,000
Bus fee: $3,000
Library fee: $500
Lab fee: $1,500
Sports fee: $700
Exam fee: $800
Uniform fee: $2,200
Late fee: $250
EOF
git add fees.txt src/Payment.java
git commit -m "Add fee list and payment class"[main 6b6d43e] Add fee list and payment class
2 files changed, 12 insertions(+)
create mode 100644 fees.txt
create mode 100644 src/Payment.javaThe cat > fees.txt << 'EOF' lines write everything up to EOF into the file. It is a way to create a file of several lines from the terminal. You can also create the file in your editor.
Later she makes two edits to fees.txt: she corrects the school year in the first line, and she adds a hostel fee at the bottom that the school has not approved yet. In real work you would make these edits in your editor. These commands make the same two edits, so that you can copy them:
sed -i.bak "1s/2026/2026-27/" fees.txt
rm fees.txt.bak
echo 'Hostel fee: $9,000' >> fees.txt
git status -s M fees.txt
?? notes.txtThe correction should be committed today. The hostel fee should wait. git add fees.txt would stage both. git add -p (short for --patch) shows each changed part of the file and asks whether to stage it. Git calls each part a hunk.
You normally type the answers yourself. So that this page can show real output, the answers y (yes) and n (no) are sent in with printf:
printf "y\nn\n" | git add -p fees.txtdiff --git a/fees.txt b/fees.txt
index 8eb0df9..1fe8ea5 100644
--- a/fees.txt
+++ b/fees.txt
@@ -1,4 +1,4 @@
-Fee list for 2026
+Fee list for 2026-27
Term 1 fee: $12,000
Term 2 fee: $12,500
Term 3 fee: $13,000
(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? @@ -9,3 +9,4 @@ Sports fee: $700
Exam fee: $800
Uniform fee: $2,200
Late fee: $250
+Hostel fee: $9,000
(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,p,P,?]?How it works
- Git shows the first hunk. A line that starts with
-was removed and a line that starts with+was added. The other lines are unchanged lines around the change. (1/2) Stage this hunk ...?is the question. The answer wasy, so the year correction is staged.- Git shows the second hunk, the new hostel line, and asks again. The answer was
n, so it stays out.
When you run git add -p yourself, Git stops at each question and waits. Your answer and a new line appear after the question mark, so the second hunk starts on its own line.
The answers you will use most:
| Key | Meaning |
|---|---|
y | Yes, stage this hunk |
n | No, don't stage it |
q | Quit, and leave the remaining hunks unstaged |
a | Stage this hunk and all later hunks in the file |
d | Don't stage this hunk or any later hunk in the file |
s | Split this hunk into smaller hunks (offered when Git can split it) |
? | Explain all the keys |
The file now has a staged part and an unstaged part:
git status -s
git commit -m "Correct the school year"
git status -sMM fees.txt
?? notes.txt
[main ef8ba5d] Correct the school year
1 file changed, 1 insertion(+), 1 deletion(-)
M fees.txt
?? notes.txtBefore the commit, fees.txt was MM: one change staged and one not. The commit took the year correction (1 insertion(+), 1 deletion(-)). After it, fees.txt is M: the hostel line is still in the file, waiting for its own commit.
Common Mistakes
Running git add . without looking. It is the fastest command and the one that causes the most regret. git status first, every time.
Thinking git add is needed only for new files. The name is misleading. git add means "put the current content of this file into the next commit", and you need it after every edit, not only the first time.
Editing after adding. The staging area holds the file as it was when you ran git add. Changes made after that are not staged. Add again.
Using git add *. The star is expanded by your terminal, not by Git. It skips files whose names begin with a dot, and it can fail on files that Git is told to ignore. Use git add ..
Trying to add an empty folder. Git tracks files. git add reports on an empty folder stages nothing and prints nothing.
Adding a path that doesn't exist. A mistyped name gives fatal: pathspec 'report' did not match any files. "Pathspec" is Git's word for the file or folder name you typed.
Under the Hood
git add does two things. It stores the content of the file inside .git/objects as a blob, and it writes the blob's ID next to the file's name in the index, which is the file .git/index.
This explains two facts from earlier lessons. The staging area holds a copy, because the blob was written at the moment of git add. And git commit is fast, because the content is already stored and the commit only has to record the list.
With git add -p, Git builds a version of the file that contains only the hunks you accepted, and stores that version as the blob. The file in your working tree is not changed.
Interview Corner
What does git add do?
It copies the current content of the named files into the staging area (the index), so that it becomes part of the next commit. It is used for new files, modified files and deleted files alike. It doesn't create a commit.
What is the difference between git add ., git add -A and git add -u?
git add . stages all changes (new, modified, deleted) in the current folder and below. git add -A stages all changes in the entire repository, whichever folder you are in. git add -u stages only modified and deleted tracked files, across the repository, and never adds new files.
How do you commit only some of the changes in a file?
With git add -p (patch mode). Git shows each hunk and asks whether to stage it: y stages it, n skips it, and s splits a hunk into smaller ones. Then git commit commits only the staged hunks, and the rest stay in the working tree.
How do you unstage a file?
git restore --staged <file> removes it from the staging area and leaves the working tree copy as it is. (Before Git 2.23, the command for this was git reset HEAD <file>, which still works.)
Why is git add -p considered good practice?
It makes you read every change before it is committed, so stray debug lines and accidental edits are caught. It also lets you split unrelated changes into separate, focused commits, which are easier to review and to undo.
Practice
Challenge: predict what -u stages. Continue in the same repository. Add the line Tested by Advitha. to README.md, create a new file src/Receipt.java, and run git add -u. Print the short status.
What does Git print? There are now four files with something to report: README.md, fees.txt, notes.txt and src/Receipt.java. Before you open the solution, write down the two-letter code of each one.
Show solution
echo "Tested by Advitha." >> README.md
echo "class Receipt {}" > src/Receipt.java
git add -u
git status -sM README.md
M fees.txt
?? notes.txt
?? src/Receipt.java-u staged the two tracked files that had changes: README.md, and fees.txt with its waiting hostel line. The two untracked files stayed untracked. If the hostel fee is still not approved, git restore --staged fees.txt takes it out again.
Checkpoint. In a practice repository, make three unrelated edits to one file, well apart from each other. Use git add -p to turn them into three separate commits. Run git status -s between the commits and watch the file go from MM to M to clean.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git add <file> | Stage one file |
git add <folder> | Stage every change in a folder |
git add . | Stage every change in the current folder and below |
git add -A | Stage every change in the repository |
git add -u | Stage changes to tracked files only |
git add -p | Choose hunks to stage, one by one |
git add -n . | List what would be staged, without staging |
git restore --staged <file> | Take a file out of the staging area |
Was this tutorial helpful?