Tracking Changes: git status and Staging with git add
Tracking Changes: git status and Staging with git add
In Git, files do not automatically jump from your text editor into the permanent commit history. You must explicitly inspect your changes using git status and stage them using git add. This deliberate workflow gives you total control over what enters your project history.
1. Checking Status with git status
The git status command displays the exact state of your working directory and staging area relative to your last commit:
Understanding Status Outputs:
When you run git status, Git categorizes files into three distinct sections:
- 1Changes to be committed (Staged): Files added to the staging area with
git add, ready for the next commit. - 2Changes not staged for commit (Modified): Tracked files that have been edited on disk, but not yet staged.
- 3Untracked files: Brand new files that have never been committed or staged before.
Example Terminal Output:
Pro Tip: Compact Short Status (-s or --short)
Output flags:
??: Untracked fileM(space then M in red) : Modified in working tree, not stagedM(green M then space) : Staged and ready to commitMM: Staged, but modified again in working directory after staging!
2. Staging Changes with git add
The git add command copies changes from the Working Directory into the Staging Area (The Index):
Common git add Variations:
git add . without checking git status first is dangerous! You risk accidentally staging sensitive environment files (.env), database passwords, or bloated dependencies (node_modules). Always review git status first.3. Advanced Staging: Interactive Patch Staging (git add -p)
What if you made 20 lines of changes in app.js, but 10 lines belong to a bug fix and 10 lines belong to an unfinished new feature? You can stage individual "hunks" (chunks) interactively using the patch flag:
Git will display each modified chunk and prompt:
y: stage this hunkn: do not stage this hunks: split this hunk into smaller piecesq: quit interactive staging
4. Unstaging Files (git restore --staged)
If you accidentally staged a file that shouldn't be in the commit:
Practice Quiz
git status?git add . do?git status -s (short status), what does a green M on the left followed by a space mean?Recording Snapshots: git commit and Semantic Commit Messages
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.