Git Course 0%

Staging with git add

Beginner Git CLI VS Code UI IntelliJ UI ≈ 10 min

What you will learn

  • The forms of git add and what each one includes
  • How git add . decides what to stage, and why reading the status first matters
  • Staging part of a file with -p, and unstaging with restore --staged

After this lesson you can

  • I can build a commit that contains exactly one logical change, even when I edited more

Why this matters

git add is where you decide what history will say. Stage everything blindly and your commits become "Tuesday's stuff": hard to review, hard to revert, and occasionally containing a secret. Stage deliberately and each commit tells one story. The command is simple; the habit is the skill.

The forms

Command Stages Use it for
git add docs/faq.md one file (new, modified or deleted) the normal case
git add docs/ every change under a folder a documentation reorganization
git add . every change in the current folder and below: new, modified and deleted files (ignored files excluded) "commit everything I did", after reading git status
git add -A the same for the whole repository, wherever you are same, from a subfolder
git add -u modified and deleted tracked files only; no new files updating without picking up stray new files
git add -p interactively, hunk by hunk committing part of a file

git add on an untracked file starts tracking it; on a modified file it copies the new content to the staging area; on a deleted file it stages the deletion. The word "add" is historical; think "stage".

Everything, carefully

Terminal
$ git status -s
 M README.md
 M docs/faq.md
?? docs/notes.md
Terminal
$ git add .
$ git status -s
M  README.md
M  docs/faq.md
A  docs/notes.md

The M moved to the first column (staged) and the untracked file became A (added). Whatever git status listed before, git add . staged. That is why the status comes first: if the list contains something that does not belong (a stray file, a test data change), stage files individually instead.

Part of a file

You fixed a typo and drafted a new section in the same file, and want to commit the typo fix now. git add -p walks through the file's hunks and asks about each:

Terminal
$ git add -p README.md
diff --git a/README.md b/README.md
index 19ed536..6516ba6 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
 # Trailguide
 
-A tiny command-line program that prints hiking trail information for the **Northwind Trails** guide.
+A small command-line program that prints hiking trail information for the **Northwind Trails** guide.
 It exists so that people learning Git have something realistic to practice on: documentation to edit,
 data to update, a small program to run, and tests that can pass or fail.
 
(1/1) Stage this hunk [y,n,q,a,d,e,p,?]? 

Answer y to stage the hunk, n to skip it, s to split a large hunk into smaller ones, q to stop. The file ends up both staged (the hunks you said yes to) and modified (the rest), which is the "both groups" situation from lesson 5.1, on purpose.

Unstaging

Staged the wrong thing? Take it back out; the edit stays in the working tree:

Terminal
$ git restore --staged docs/notes.md

git status prints this exact hint next to every staged file. Older tutorials use git reset HEAD <file>, which does the same.

In the tools

Everything above. A habit worth building: git add <file> for each file you mean, then git status to confirm, then git diff --staged before committing.

Common mistakes

  • Editing after staging. The commit takes the staged version. git status shows the file in both groups; git add again.
  • git add on a folder while a generated subfolder sits inside it. Check the status; add the pattern to .gitignore.
  • Using git restore <file> to unstage. Without --staged it discards the edit. The hint in git status has the right form.
  • Staging a deletion by accident with git add . after deleting a file you meant to keep. git restore --staged <file> && git restore <file> brings it back before the commit.

Try it yourself

Goal: turn one messy working tree into two clean commits.

  1. In the playground, edit README.md (one word), add a line to docs/faq.md, and create docs/notes.md with a line.
  2. Stage only the FAQ and commit it with a docs: message.
  3. Stage the README and commit it separately.
  4. Leave docs/notes.md untracked; confirm with git status -s and git log --oneline -2.

Expected result: two commits, each touching one file; ?? docs/notes.md still in the status.

Show solution

Terminal
$ git add docs/faq.md
$ git commit -m "docs: add FAQ entry"
$ git add README.md
$ git commit -m "docs: reword README intro"
$ git status -s
?? docs/notes.md
The untracked file is untouched by both commits. Delete it or ignore it as you prefer.

Check yourself

1. What does git add . stage?
2. You staged a file, then edited it again. What does the next commit contain?
3. Which command takes a file out of the staging area without touching its content?

Key terms

Staged Staging area Untracked Restore