Understanding_Git_Index
Understanding Git Index
The staging area between working directory and repository.
π What is the Index?
The index (staging area) is the intermediate step between working directory and commits.
π Index Flow
graph LR
A[Working Directory] -->|git add| B[Index/Staging]
B -->|git commit| C[Repository]
C -->|git checkout| A
π View Index
List Files in Index
git ls-files
Shows all tracked files in the index.
List with Details
git ls-files -s
Shows mode, hash, and stage number.
Output format:
100644 abc123... 0 file.txt
Show Staged Changes
git diff --cached
Shows what's staged for next commit.
Show Staged File Names
git diff --cached --name-only
Lists only file names that are staged.
β Add to Index
Stage File
git add file.txt
Adds file to staging area.
Stage All
git add .
Stages all changes.
Interactive Add
git add -p
Stage specific parts (hunks) of files.
Options:
y- stage this hunkn- skip this hunks- split into smaller hunkse- manually edit
β Remove from Index
Unstage File (Modern)
git restore --staged file.txt
Removes file from staging, keeps changes.
Unstage File (Classic)
git reset HEAD file.txt
Alternative way to unstage.
Untrack File
git rm --cached file.txt
Removes from Git tracking but keeps file.
βοΈ Advanced Index Operations
Assume Unchanged
git update-index --assume-unchanged file.txt
Git ignores changes to this file.
Track Again
git update-index --no-assume-unchanged file.txt
Resume tracking changes.
Skip Worktree
git update-index --skip-worktree file.txt
Skip file in checkout operations.
π Index Stages (Conflicts)
| Stage | Meaning |
|---|---|
| 0 | Normal (no conflict) |
| 1 | Base version |
| 2 | "Ours" version |
| 3 | "Theirs" version |
View Unmerged
git ls-files -u
Shows files with merge conflicts.
π‘ Tips
Use git add -p to stage only specific changes.
Always check git diff --cached before commit.
π Related
#git #index #staging #internals