Git & GitHub ยท Chapter 6 of 42

git add

The `git add` command stages changes so they will be included in your next commit. You can stage a single file, multiple files, or all changed files at once.

Staging is a required step before committing โ€” Git will not commit changes that haven't been added to the staging area, even if the file was modified.

Syntax
git add <file>
git add .
git add -A

Staging specific files

Run `git add filename` to stage one file, or list multiple filenames separated by spaces to stage several at once.

Staging everything

Run `git add .` to stage all changes in the current directory and subdirectories, or `git add -A` to stage all changes in the entire repository, including deletions.

Example 1 (bash)
git add app.js

Stages the single file app.js for the next commit.

Example 2 (bash)
git add .

Stages all modified and new files in the current directory and below.

Key points

  • git add stages a file so it can be committed.
  • You can stage single files, multiple files, or all files.
  • git add . stages everything in the current directory.
  • Files must be staged before git commit will include them.
๐Ÿ’ก Note: Use `git add -p` to interactively stage only parts of a file's changes, useful for splitting a large edit into multiple commits.

๐Ÿ“ Quick Quiz

1. What does git add do?

2. Which command stages all changes in the current directory?

3. Can Git commit a modified file that hasn't been staged?