basics
git add
Stages file changes into the index so they are included in the next commit. Supports whole files, directories, and interactive patch selection.
git add [<options>] <pathspec>... Common flags
| Flag | Purpose |
|---|---|
| -A / --all | Stage all changes including deletions across the whole tree |
| -u / --update | Stage modifications and deletions but not new files |
| -p / --patch | Interactively pick hunks to stage |
| -n / --dry-run | Show what would be added without staging |
| -f / --force | Add files that are gitignored |
Examples
git add . Stage all changes in the current directory
git add -p src/app.js Interactively stage individual hunks from a file
git add -A Stage additions, modifications, and deletions repo-wide
Gotcha
'git add .' respects the current directory only, while 'git add -A' spans the whole repo; the two are not equivalent in subfolders.
Related commands
git commit
Records the staged snapshot to the repository history along with a message and author metadata. Every commit is identified by a unique SHA hash.
git status
Shows the state of the working tree and index: staged, unstaged, and untracked files, plus branch tracking info. The first command to run when you're unsure what changed.
git restore
Modern command (Git 2.23+) for restoring working tree files from the index or a commit, or unstaging files. Replaces the confusing file-mode of 'git checkout'.
git clone
Downloads a repository from a remote URL into a new local directory, setting up the remote 'origin' automatically. Includes all commits, branches, and tags by default.
git init
Creates a new empty Git repository by initializing the .git metadata directory in the current or specified folder. Turns any directory into a version-controlled project.