What is the Git Staging area (or index)? The Proposed next commit
About
The staging area is a tree file called index that stores information to create the next commit.
The index is like a pre-commit.
The index holds a snapshot of the content of the working tree.
You must use the add command to add any new or modified files to the index (ie staging area)
- after making any changes to the working directory
- and before running the commit command
Command
Location
- By default, the .git/index file
- or the GIT_INDEX_FILE environment variable
Add (Staging)
Reset (Unstaging/Delete)
Git reset will revert the head to the last commit state.
git reset --hard
HEAD is now at xxxx commit
git cat-file -p master^{tree}
List the files
For the next commit (staged)
git diff-index HEAD
In the index
To list the files the contents of the index with the stage information:
- The mode of the file
- The id of the object,
- The stage of the file (in next commit or not)
- and the pathname of the file
git ls-files --stage
100644 70e413f03019cd301ebb955a3cc4d1ce9cad3cba 0 .gitattributes
100644 28a0ee7ed92f8962153865cf3eebf44e14c5c70e 0 .gitignore
100644 652381035ef9a14fdfb0e138ea33f2b777f1d06e 0 README.md
100644 7796e9eaf8ba750963df16029fd043b1905cbadc 0 conf/local.php
100644 cab897e88d877554458012df4447ea35f05dfb35 0 media/android-chrome-192x192.png
100644 6a4da3d8a25a9ba97b72d609c23ccc9fa8de43c4 0 media/android-chrome-512x512.png
To add file system information (modified date, …), add the debug option.
git ls-files --debug
100644 3eb4c7064f05a84ba975d45065b51114bcffef29 0 pages/slot_header.txt
ctime: 1681850743:763163236
mtime: 1681850743:763163236
dev: 2049 ino: 155378090
uid: 1002 gid: 1001
size: 557 flags: 0
Commit
Restore
git restore to restore a file to the last state
Remove
With rm:
- Remove a file from staging
git rm --cached path/to/file
- Remove all files from staging area
git rm --cached -r .
where:
- --cached remove the paths from staging and the index without removing the files themselves
- -r operates on directories recursively.