A Git commit only includes the snapshot stored in the index, so a modified working tree can still produce an empty or incomplete commit when the needed paths are not staged. Stage the exact file paths first, then check the index before committing so unrelated work stays outside the next commit.
git add copies the current content of a path into the index, which is Git's staging area for the next commit. Running it for a file stages that file as it exists at that moment; later edits to the same file remain unstaged until git add runs again.
The path-based method fits an existing repository when the whole file should go into the next commit. For selected hunks inside one file, use partial staging instead, and unstage a path when it was added to the index by mistake.
Related: How to stage part of a file in Git
Related: How to unstage changes in Git
Related: How to amend the last Git commit
Steps to stage file changes in Git:
- Open a terminal in the repository that contains the changed file.
- Check the current working tree state.
$ git status --short M docs/notes.md ?? docs/todo.md
In short status output, the left column is the index and the right column is the working tree. A leading space before M means docs/notes.md has an unstaged modification, while ?? means docs/todo.md is not tracked yet.
- Stage the modified file that belongs in the next commit.
$ git add -- docs/notes.md
The -- marker tells Git that later words are paths, which avoids ambiguity when a path begins with a dash.
- Verify that the file moved into the index.
$ git status --short M docs/notes.md ?? docs/todo.md
The M in the left column means docs/notes.md is staged. docs/todo.md remains untracked because it was not named in the git add command.
- Inspect the staged content before committing.
$ git diff --cached -- docs/notes.md diff --git a/docs/notes.md b/docs/notes.md index 97de7e0..7fc30a0 100644 --- a/docs/notes.md +++ b/docs/notes.md @@ -1 +1,3 @@ # Project notes + +- update deployment checklist
git diff –cached shows the index compared with HEAD. The output should contain only the file content intended for the next commit.
- Stage an untracked file when it should also be included.
$ git add -- docs/todo.md $ git status --short M docs/notes.md A docs/todo.md
The A in the left column means the new path is staged for addition.
- Check the staged version of the new file.
$ git diff --cached -- docs/todo.md diff --git a/docs/todo.md b/docs/todo.md new file mode 100644 index 0000000..dbe213e --- /dev/null +++ b/docs/todo.md @@ -0,0 +1 @@ +- review release branch
- Confirm there are no remaining unstaged edits for the paths that should be ready to commit.
$ git diff -- docs/notes.md docs/todo.md
No output means the working-tree copies of those paths match the versions already staged in the index. If a file changed again after staging, run git add -- path/to/file again before committing.
Related: How to unstage changes in Git
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.