Split work into focused commits
One file has a bug fix mixed with unrelated refactoring. Use git add -p to stage only the fix hunks and commit them first.
Git · Command
git add copies the current content of new or changed files into the index (the staging area), which holds what the next commit will contain. A plain git commit records only what has been staged.
Last updated
The index sits between your working tree and your commit history. When you run git add, Git stores the file's content as it is at that moment and points the matching index entry at it. Later edits to the file are not staged until you run git add again. Pathspecs choose which files are affected. Naming a directory makes the index match that whole directory, including files added or deleted in it. Options control the details: -u updates only files Git already tracks, -A covers all files, -p lets you pick individual hunks, and -N records an empty placeholder entry.
git add [--verbose | -v] [--dry-run | -n] [--force | -f] [--interactive | -i] [--patch | -p] [--edit | -e] [--[no-]all | -A | --[no-]ignore-removal | [--update | -u]] [--sparse] [--intent-to-add | -N] [--refresh] [--ignore-errors] [--ignore-missing] [--renormalize] [--resolved] [--chmod=(+|-)x] [--pathspec-from-file=<file> [--pathspec-file-nul]] [--] [<pathspec>...]| Name | Type | Description |
|---|---|---|
<pathspec>... | pathspec | The files to stage. Accepts file globs such as *.c and directory names. Naming a directory stages new, modified and deleted files inside it. |
-n, --dry-run | flag | Lists what would be added or ignored without changing the index. |
-f, --force | flag | Stages files that would otherwise be ignored. Also stages a submodule update when that submodule is set to ignore=all. |
-p, --patch | flag | Lets you choose, hunk by hunk, which changes between the index and the working tree to stage. |
-i, --interactive | flag | Opens the interactive menu (status, update, revert, add untracked, patch, diff). |
-e, --edit | flag | Opens the diff against the index in your editor and stages the patch you save. |
-u, --update | flag | Updates only paths the index already tracks, staging their modifications and deletions but never adding new files. |
-A, --all | flag | Makes the index match the working tree by staging additions, modifications and deletions. |
--no-all, --ignore-removal | flag | Stages new and modified files but leaves files deleted from the working tree alone. |
-N, --intent-to-add | flag | Records that the path will be added later by placing an entry with no content in the index. |
--renormalize | flag | Runs the clean filter again on all tracked files, for example to fix line endings. Implies -u. |
--resolved | flag | Marks unmerged paths as resolved when no conflict markers remain in them. Cannot be combined with -u or -A. |
#!/usr/bin/env bash
set -euo pipefail
# Preview what would be staged
git add --dry-run src/
# Stage one file completely
git add README.md
# Pick individual hunks from a file you edited
git add -p src/app.py
# Stage edits and deletions of tracked files only (no new files)
git add -u
# Mark a script as executable in the repository only
git add --chmod=+x scripts/deploy.sh
# Quote the glob so Git also matches files in subdirectories
git add 'docs/*.md'
# Review the staged changes, then commit them
git diff --cached --stat
git commit -m "Update docs and app logic"The script previews what would be staged, stages files in several different ways, checks the staged result with git diff --cached, and then commits it.
One file has a bug fix mixed with unrelated refactoring. Use git add -p to stage only the fix hunks and commit them first.
After removing the conflict markers by hand, run git add (or git add --resolved) on those paths to mark them resolved before you commit the merge.
After changing core.autocrlf or .gitattributes, run git add --renormalize . so every tracked file is stored with the corrected line endings.
Write the paths to a file, or pipe them in with --pathspec-from-file=- and --pathspec-file-nul, so the command line does not get too long.
git add . stages new, modified and deleted files under the current directory, because a directory pathspec makes the index match that directory. -u touches only files already in the index, so it stages edits and deletions but never new files. -A stages additions, edits and deletions, and with no pathspec it applies to the entire working tree.
Follow-up: What does --no-all do? It stages new and modified files but skips deletions, which is how older Git versions behaved, and it does nothing if you give no pathspec.
The version you staged, not the later edit. git add copies the file's content at the moment you run it, and a plain git commit records only the index. git status will list the file as both staged and modified.
Follow-up: How do you include the newer edit? Run git add on the file again before committing, or use git commit -a, which updates the index with all changes to tracked files before committing.
It records only an intent to add the file: an index entry with no content. The file then counts as tracked, so git diff shows its contents as unstaged changes and git commit -a will include it.
Follow-up: How is that different from git add -p on a brand-new file? -p offers hunks only for differences between the index and the working tree, so add the new file with -N first, and then -p can stage parts of it.
git statusgit commitgit diffgit resetgit restoregit rmgit mvgit update-index