Git Staging
The staging area is where you assemble exactly what should go into your next commit, one file or one change at a time.
What Is the Staging Area?
Git tracks three separate areas for your project: the working directory, where you actually edit files; the staging area, sometimes called the index, which holds a preview of your next commit; and the repository, where committed history is permanently stored. Changes only move from your working directory into history after you explicitly stage them.
Adding Files with git add
The git add command moves changes from the working directory into the staging area. You can stage a single file, several files at once, every file matching a pattern, or every change in the project. Nothing is committed yet at this point, it is only marked as ready.
Example
$ git status
Untracked files:
styles.css
index.html
$ git add index.html
$ git status
Changes to be committed:
new file: index.html
Untracked files:
styles.css- git add file.txt — stage a single file
- git add file1.txt file2.txt — stage several specific files
- git add *.css — stage every file matching a pattern
- git add . — stage every new and modified file in the current folder and below
- git add -p — interactively choose which parts of a file to stage
Example
$ git add .
$ git status
Changes to be committed:
new file: index.html
new file: styles.cssUnstaging a File
If you stage a file by mistake, or change your mind about including it in the next commit, you can remove it from the staging area without losing your edits. This does not undo the change itself, it only moves the file back to the working directory state.
Example
$ git restore --staged styles.css
$ git status
Changes to be committed:
new file: index.html
Untracked files:
styles.cssExercise: Git Staging
What does `git add <file>` actually do?