Git Add: Staging Changes

Updated · 14 min read
easyGit 2.53gitgit-addstagingadd-patchinterview

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:

Terminal
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
Output
 M README.md
?? docs/setup.md
?? notes.txt
?? src/FeeCalculator.java
?? src/Student.java

One 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

Terminal
git add <file-or-folder>...
FormWhat 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 -AEvery change in the whole repository, wherever you are standing
git add -uChanges to tracked files only: modified and deleted files, but no new files
git add -pParts 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

Terminal
git add src/FeeCalculator.java
git status -s -uall
Output
 M README.md
A  src/FeeCalculator.java
?? docs/setup.md
?? notes.txt
?? src/Student.java

Only 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:

Terminal
git add src
git status -s -uall
Output
 M README.md
A  src/FeeCalculator.java
A  src/Student.java
?? docs/setup.md
?? notes.txt

src/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:

Terminal
git add -u
git status -s -uall
Output
M  README.md
A  src/FeeCalculator.java
A  src/Student.java
?? docs/setup.md
?? notes.txt

README.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:

Terminal
git add -n .
Output
add 'docs/setup.md'
add 'notes.txt'

The list includes notes.txt, Navika's private notes. Suppose she doesn't notice and adds everything:

Terminal
git add .
git status -s
Output
M  README.md
A  docs/setup.md
A  notes.txt
A  src/FeeCalculator.java
A  src/Student.java

Every 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:

Terminal
git restore --staged notes.txt
git status -s
Output
M  README.md
A  docs/setup.md
A  src/FeeCalculator.java
A  src/Student.java
?? notes.txt

notes.txt is untracked again, and the other four files are still staged. Now the commit contains what Navika meant:

Terminal
git commit -m "Add fee calculator, student class and setup guide"
Output
[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.java

git 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 -A stages changes in the whole repository.

Navika moves into src, creates a file there, and also edits the notes file one level up:

Terminal
cd src
echo "class Payment {}" > Payment.java
echo "Call the bus contractor" >> ../notes.txt
git add .
git status -s
Output
A  Payment.java
?? ../notes.txt

Only 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:

Terminal
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"
Output
[main 6b6d43e] Add fee list and payment class
 2 files changed, 12 insertions(+)
 create mode 100644 fees.txt
 create mode 100644 src/Payment.java

The 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:

Terminal
sed -i.bak "1s/2026/2026-27/" fees.txt
rm fees.txt.bak
echo 'Hostel fee: $9,000' >> fees.txt
git status -s
Output
 M fees.txt
?? notes.txt

The 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:

Terminal
printf "y\nn\n" | git add -p fees.txt
Output
diff --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

  1. 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.
  2. (1/2) Stage this hunk ...? is the question. The answer was y, so the year correction is staged.
  3. 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.

git add -p stages one hunk and leaves the other

The answers you will use most:

KeyMeaning
yYes, stage this hunk
nNo, don't stage it
qQuit, and leave the remaining hunks unstaged
aStage this hunk and all later hunks in the file
dDon't stage this hunk or any later hunk in the file
sSplit 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:

Terminal
git status -s
git commit -m "Correct the school year"
git status -s
Output
MM fees.txt
?? notes.txt
[main ef8ba5d] Correct the school year
 1 file changed, 1 insertion(+), 1 deletion(-)
 M fees.txt
?? notes.txt

Before 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
Terminal
echo "Tested by Advitha." >> README.md
echo "class Receipt {}" > src/Receipt.java
git add -u
git status -s
Output
M  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.

Quick check: Git add

4 questions
1Which command stages every change in the whole repository, even when you are in a subfolder?
2What does git add -u leave out?
3A file has two unrelated changes and you want to commit only one. What do you use?
4You staged notes.txt by mistake. Which command takes it out of the staging area and keeps the file?

Key Takeaways

Quick reference

CommandIt 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 -AStage every change in the repository
git add -uStage changes to tracked files only
git add -pChoose 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?

Lesson 12 of 106