Staging Changes: add, Patch Mode, Interactive Staging, rm, mv and Unstaging
Every way to prepare a commit: git add for files, folders and patterns, -A, -u and dry runs, staging parts of a file with git add -p, the git add -i menu, intent-to-add, unstaging, deleting with git rm, stopping tracking with --cached, and renaming with git mv.
kvi5hnuCode Sprout team member10 min readOn this page
Article 4 showed that a commit records the staging area. This article covers the tools for filling it precisely: whole files, folders, patterns, single parts of a file, deletions and renames, and the commands that take things out again.
git add: files, folders and patterns
git add takes paths. A folder stages everything inside it, and a quoted pattern is matched by Git against all files, including files in subfolders:
| Command | Stages |
|---|---|
git add README.md style.css | those two files |
git add trails/ | everything changed or new in trails/ and below |
git add '*.txt' | every .txt file in the repository, in any folder (the quotes stop the shell expanding the pattern first) |
git add . | every change in the current folder and below: new, modified and deleted files |
git add -A (--all) | every change in the whole repository, wherever you run it |
git add -u (--update) | changes to tracked files only, including deletions; new files are left alone |
git add -n (--dry-run) | nothing: it lists what the command would stage |
git init -q trail-guide
cd trail-guide
mkdir trails
echo "# Trail guide" > README.md
echo "Ridge loop: 8 km" > trails/ridge.txt
git add . && git commit -q -m "Start project"
echo "Notes and maps." >> README.md # change a tracked file
echo "Lake path: 5 km" > trails/lake.txt # a new file
echo "Pack water." > tips.txt # another new file
echo "--- git add -u: tracked files only"
git add -u
git status -s
echo "--- dry run of a pattern"
git add -n '*.txt'
git add '*.txt'
echo "--- after git add '*.txt'"
git status -s
--- git add -u: tracked files only
M README.md
?? tips.txt
?? trails/lake.txt
--- dry run of a pattern
add 'tips.txt'
add 'trails/lake.txt'
--- after git add '*.txt'
M README.md
A tips.txt
A trails/lake.txt
git add -u staged the README change but left both new files untracked. The quoted '*.txt' matched tips.txt at the top and trails/lake.txt in a subfolder. Without quotes, the shell would expand *.txt to tips.txt alone, the only match in the current folder, before Git ever saw the pattern.
To add everything except some paths, use an exclude pattern: git add -A -- ':!secrets.txt' stages all changes but leaves secrets.txt out. A .gitignore (article 8) is the permanent way to keep files out.
Staging part of a file with git add -p
Often one file contains two changes that belong in different commits, or a real change plus a debugging line you don't want to commit. git add -p (--patch) splits the changes into hunks, groups of nearby changed lines, shows each one, and asks whether to stage it. Unstaged hunks stay in the working tree.
The example answers the prompts with printf, which is how a script can drive the interactive prompt. In a terminal you'd type the answers, and they'd appear after each question:
git init -q trail-guide
cd trail-guide
printf 'Ridge loop: 8 km\nLake path: 5 km\nForest walk: 3 km\nSummit climb: 12 km\nRiver trail: 6 km\nCave route: 4 km\nMeadow loop: 2 km\nCanyon trail: 9 km\nPine ridge: 7 km\nCoast path: 10 km\n' > trails.txt
git add trails.txt && git commit -q -m "Add trails"
sed -i 's/Ridge loop: 8 km/Ridge loop: 8.5 km/' trails.txt # a real correction
echo "DEBUG: check sorting" >> trails.txt # a temporary debugging line
# Answer y to the first hunk and n to the second
printf 'y\nn\n' | git add -p trails.txt
echo
echo "=== staged (goes into the commit)"
git diff --staged
echo "=== not staged (stays in the file)"
git diff
diff --git a/trails.txt b/trails.txt
index e0291d0..38da923 100644
--- a/trails.txt
+++ b/trails.txt
@@ -1,4 +1,4 @@
-Ridge loop: 8 km
+Ridge loop: 8.5 km
Lake path: 5 km
Forest walk: 3 km
Summit climb: 12 km
(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? @@ -8,3 +8,4 @@ Meadow loop: 2 km
Canyon trail: 9 km
Pine ridge: 7 km
Coast path: 10 km
+DEBUG: check sorting
(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,p,P,?]?
=== staged (goes into the commit)
diff --git a/trails.txt b/trails.txt
index e0291d0..a4ebcaa 100644
--- a/trails.txt
+++ b/trails.txt
@@ -1,4 +1,4 @@
-Ridge loop: 8 km
+Ridge loop: 8.5 km
Lake path: 5 km
Forest walk: 3 km
Summit climb: 12 km
=== not staged (stays in the file)
diff --git a/trails.txt b/trails.txt
index a4ebcaa..38da923 100644
--- a/trails.txt
+++ b/trails.txt
@@ -8,3 +8,4 @@ Meadow loop: 2 km
Canyon trail: 9 km
Pine ridge: 7 km
Coast path: 10 km
+DEBUG: check sorting
The correction on line 1 is staged, and the debugging line at the end is still only in the working tree. Committing now records just the correction.
The answers you can give at each hunk:
| Key | Meaning |
|---|---|
y / n | stage this hunk / don't |
a / d | stage this hunk and all later hunks in the file / skip them all |
q | quit, leaving the remaining hunks unstaged |
s | split this hunk into smaller ones, if it contains separate groups of changes |
e | edit the hunk by hand, to stage only some of its lines |
j, J, k, K, g, / | move between hunks without deciding, or search for one by a regular expression |
p / P | print the hunk again / print it through the pager |
? | show the help |
Only the keys that make sense appear in each prompt. s appears only when a hunk can be split. When two changes are close together, Git shows them as one hunk, because the unchanged lines of context around them overlap. s splits them:
git init -q trail-guide
cd trail-guide
printf 'Ridge loop: 8 km\nLake path: 5 km\nForest walk: 3 km\nSummit climb: 12 km\nRiver trail: 6 km\n' > trails.txt
git add trails.txt && git commit -q -m "Add trails"
sed -i 's/Ridge loop: 8 km/Ridge loop: 8.5 km/; s/River trail: 6 km/River trail: 6.5 km/' trails.txt
# s = split, then y for the first piece and n for the second
printf 's\ny\nn\n' | git add -p
echo
echo "=== staged"
git diff --staged
diff --git a/trails.txt b/trails.txt
index 1728ed8..e188882 100644
--- a/trails.txt
+++ b/trails.txt
@@ -1,5 +1,5 @@
-Ridge loop: 8 km
+Ridge loop: 8.5 km
Lake path: 5 km
Forest walk: 3 km
Summit climb: 12 km
-River trail: 6 km
+River trail: 6.5 km
(1/1) Stage this hunk [y,n,q,a,d,s,e,p,P,?]? Split into 2 hunks.
@@ -1,4 +1,4 @@
-Ridge loop: 8 km
+Ridge loop: 8.5 km
Lake path: 5 km
Forest walk: 3 km
Summit climb: 12 km
(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? @@ -2,4 +2,4 @@
Lake path: 5 km
Forest walk: 3 km
Summit climb: 12 km
-River trail: 6 km
+River trail: 6.5 km
(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,p,P,?]?
=== staged
diff --git a/trails.txt b/trails.txt
index 1728ed8..fb354f3 100644
--- a/trails.txt
+++ b/trails.txt
@@ -1,4 +1,4 @@
-Ridge loop: 8 km
+Ridge loop: 8.5 km
Lake path: 5 km
Forest walk: 3 km
Summit climb: 12 km
e is for changes on adjacent lines that s can't separate. It opens the hunk in your editor: delete a + line to leave that addition out, or turn a - line into a context line (replace the - with a space) to keep the old line. Graphical tools and IDEs make line-by-line staging easier: most let you click individual lines.
-p works with other commands too. git restore -p discards selected hunks, git restore --staged -p unstages selected hunks, git stash -p stashes selected hunks, and git commit -p runs the same selection and commits straight away.
The interactive menu: git add -i
git add -i (--interactive) opens a menu-driven version of staging, showing each file's staged and unstaged line counts:
git init -q trail-guide
cd trail-guide
echo "# Trail guide" > README.md
echo "Ridge loop: 8 km" > trails.txt
git add . && git commit -q -m "Start project"
echo "Notes and maps." >> README.md
echo "Lake path: 5 km" >> trails.txt
git add trails.txt
echo "Pack water." > tips.txt
# 1 = status, then 7 = quit
printf '1\n7\n' | git add -i
staged unstaged path
1: unchanged +1/-0 README.md
2: +1/-0 nothing trails.txt
*** Commands ***
1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked
5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp
What now> staged unstaged path
1: unchanged +1/-0 README.md
2: +1/-0 nothing trails.txt
*** Commands ***
1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked
5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp
What now> Bye.
The table shows that README.md has one unstaged added line, and trails.txt has one staged added line and nothing more to stage. The menu's commands: status shows this table, update stages whole files, revert unstages them, add untracked starts tracking new files, patch runs the same hunk selection as git add -p on files you pick, and diff shows what's staged. Type a number or the highlighted letter. Most people use git add -p directly, but the menu is handy for working through many files.
Intent to add: git add -N
New, untracked files don't appear in git diff, and git add -p can't stage part of them. git add -N (--intent-to-add) records that a file will be added, without staging its contents. It then shows up in git diff like a tracked file, and git add -p can pick parts of it:
git init -q trail-guide
cd trail-guide
echo "# Trail guide" > README.md
git add README.md && git commit -q -m "Start project"
echo "Pack water and a map." > tips.txt
echo "--- git diff before -N (new files are invisible)"
git diff --stat
git add -N tips.txt
echo "--- after git add -N"
git status -s
git diff --stat
--- git diff before -N (new files are invisible)
--- after git add -N
A tips.txt
tips.txt | 1 +
1 file changed, 1 insertion(+)
The status code A means "intent to add": the file is tracked, but nothing is staged yet.
Unstaging
To take a change out of the staging area and keep it in the working tree:
git restore --staged trails.txt # Git 2.23 and later
git reset trails.txt # older equivalent, still works
git reset # unstage everything
Both forms copy the version from the last commit back into the staging area. Before the first commit, git restore --staged fails because there's no last commit to copy from, so git status suggests git rm --cached <file>, which removes the file from the staging area entirely. git reset <file> also works then.
Deleting files: git rm
Deleting a tracked file is a change like any other, and it has to be staged. You can delete the file normally, then stage the deletion with git add or git rm. Or git rm does both in one step:
git init -q trail-guide
cd trail-guide
echo "# Trail guide" > README.md
echo "old notes" > old.txt
echo "API_KEY=abc123" > .env
git add . && git commit -q -m "Start project"
git rm old.txt
echo "--- old.txt is deleted from disk and the deletion is staged"
git status -s
ls
git rm --cached .env
echo "--- .env is no longer tracked, but still on disk"
git status -s
ls -a | grep env
rm 'old.txt'
--- old.txt is deleted from disk and the deletion is staged
D old.txt
README.md
rm '.env'
--- .env is no longer tracked, but still on disk
D .env
D old.txt
?? .env
.env
git rm --cached is the tool for "I committed a file that shouldn't be in the repository": it stops tracking the file and leaves your copy alone. Commit the removal and add the file to .gitignore (article 8), or it will show up as untracked and could be added again. The file still exists in earlier commits, so a committed secret must be revoked anyway.
Other forms:
git rm -r folder/removes a folder and everything in it.git rmrefuses to delete a file with uncommitted changes, to protect them.-fforces it.git rm '*.log'removes matching files in any folder, like the quoted patterns ofgit add.
Renaming and moving: git mv
git mv old new renames the file on disk and stages the change:
git init -q trail-guide
cd trail-guide
mkdir trails
printf 'Ridge loop\nDistance: 8 km\nClimb: 240 m\nSurface: gravel\n' > ridge.txt
git add . && git commit -q -m "Add ridge trail"
git mv ridge.txt trails/ridge.txt
git status -s
git commit -q -m "Move trail notes into trails/"
echo "--- a rename made without git mv is detected the same way"
mv trails/ridge.txt trails/ridge-loop.txt
git add -A
git status -s
R ridge.txt -> trails/ridge.txt
--- a rename made without git mv is detected the same way
R trails/ridge.txt -> trails/ridge-loop.txt
Git doesn't record renames. A commit is a snapshot, and there's no "rename" step in it. When it shows a status or a diff, Git compares the old and new snapshots and detects a rename when a deleted file and a new file are similar enough: 50% similar by default. So a rename done with mv plus git add -A is exactly as good as git mv, which is only a convenience. If you rename a file and rewrite most of it in the same commit, Git may show a deletion and a new file instead. Commit the rename first, then the edits, to keep the file's history easy to follow with git log --follow (article 9).
Renaming a folder works the same way: git mv old-folder new-folder.
Changing only the capitalization of a name, such as readme.md to README.md, is a special case on Windows and macOS, whose file systems usually treat both spellings as the same name. A plain mv there changes the name on disk but Git may not notice. git mv readme.md README.md handles it correctly in current Git. Very old versions failed with "Invalid argument", and the workaround was to rename in two steps: git mv readme.md tmp && git mv tmp README.md.
What's current
git restore --staged(Git 2.23) is the modern way to unstage, andgit statussuggests it.git reset <file>remains common in older guides.git add -pkeeps improving: Git 2.47 addedP, which shows the hunk through the pager (listed in the prompt's help since 2.52), and Git 2.54 lets you go back to a file you already dealt with in the same session and shows whether the hunk on screen is already marked to be staged or skipped.- Since Git 2.0,
git add .includes deletions in the current folder. Older guides that say it only adds new and modified files describe Git 1.x.
Common questions
What's the difference between git add ., git add -A and git add -u?
git add . stages all changes, new files included, in the current folder and below. git add -A does the same for the whole repository. git add -u stages modifications and deletions of tracked files only, never new files.
I accidentally staged a file. Did I break anything?
No. Staging only prepares the next commit. Unstage with git restore --staged <file>, or git rm --cached <file> (or git reset <file>) if the repository has no commits yet.
How do I stage a file that's listed in .gitignore?
git add refuses ignored files and tells you so. If you really want it tracked, use git add -f <file>. Once tracked, the file is no longer affected by the ignore rule.
Does git rm delete a file from the history?
No. It removes the file from the next snapshot. Earlier commits still contain it. Removing a file from all history requires rewriting history (article 47).
Why does git status show a renamed file as deleted and new?
Either only one side of the rename is staged, so stage both (git add -A does that), or the content changed too much for Git to recognize it as a rename. Commit the rename separately from big edits.
Summary
git addtakes files, folders and quoted patterns.-Astages everything,-ustages only tracked files, and-nshows what would be staged.git add -pstages selected hunks of a file.ssplits a hunk andeedits it line by line. The same-pworks withrestore,stashandcommit.git add -iis a menu for staging, unstaging and patching many files.git add -Nmarks a new file as intended, sogit diffandgit add -pcan see it.git restore --staged(orgit reset) unstages, and keeps the working-tree change.git rmdeletes and stages the deletion.git rm --cachedstops tracking a file but keeps it on disk.git mvrenames and stages. Git detects renames by comparing contents, so plainmvplusgit add -Aworks too.
Practice questions
You changed app.js in two places: a bug fix near the top and a half-finished feature near the bottom. How do you commit only the bug fix?
Answer
Run git add -p app.js, answer y to the hunk with the bug fix and n to the feature hunk, check with git diff --staged, then git commit -m "Fix ...". The feature stays in the working tree, unstaged. If both changes landed in one hunk, press s to split it, or e to edit it.
A teammate committed config/local.yml, which contains personal settings. They want Git to stop tracking it, without deleting their copy. What do they run?
Answer
git rm --cached config/local.yml
echo "config/local.yml" >> .gitignore
git add .gitignore
git commit -m "Stop tracking local config"
--cached removes the file from the staging area only, so the commit records its removal while the file stays on disk. The .gitignore entry stops it from being added again. Other people's copies of the file are deleted when they pull this commit, so warn them to back it up first.
What does git status -s print at the end?
git init -q && printf 'a\nb\nc\nd\n' > notes.txt
git add notes.txt && git commit -q -m one
mv notes.txt journal.txt
git status -s
git add -A
git status -s
Answer
Before git add -A:
D notes.txt
?? journal.txt
After it:
R notes.txt -> journal.txt
Before staging, Git only sees a tracked file that's missing and a new untracked file. Once both sides are staged, Git compares them, finds identical content, and reports a rename. No git mv was needed.
Previous: The Three Areas of Git: Working Tree, Staging Area and Repository
Next: Committing: Good Messages, Amending, Empty Commits, Authors and Dates

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.