my-notes

Understanding_Git_Index

1 min read

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 hunk
  • n - skip this hunk
  • s - split into smaller hunks
  • e - 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

Stage Parts of File

Use git add -p to stage only specific changes.

View What's Staged

Always check git diff --cached before commit.



#git #index #staging #internals

Built with mdgarden