The Index and Refs
Besides objects, a Git repository has two other kinds of data. The index (also called the staging area or cache) is a single file, .git/index, listing every tracked path with the blob that the next commit will use for it. Refs (references) are names, such as branches, tags and HEAD, each pointing to an object ID. Objects never change, so the index and the refs are the only things in a repository that move. Every porcelain command you know is, in the end, a way of updating them.
In a theatre, the scripts and costumes in the storeroom are the objects. The index is the stage manager's checklist for tonight: which costume each actor wears in the next scene. The refs are the signs on the dressing-room doors, saying who is in which room. The storeroom never changes during a show. Only the checklist and the signs do.
The Starting Point
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
mkdir src
echo "class FeeCalculator {}" > src/FeeCalculator.java
echo "theme=light" > settings.properties
git add .
git commit --quiet -m "Start the project"
git log --oneline90c874a (HEAD -> main) Start the projectThe Index
git ls-files --stage prints the index, one line per tracked path:
git ls-files --stage100644 d5924b0d02d9ef4f29fef5369a7ddcaf82c830c7 0 README.md
100644 94de35974250949ae6aa2436d59df0ddab8a397e 0 settings.properties
100644 2bec87b439cb8e1072c1ec91431b301e1c2e6d68 0 src/FeeCalculator.java| Column | Meaning |
|---|---|
100644 | The mode, as in a tree (normal file) |
| ID | The blob the next commit will use for this path |
0 | The stage number: 0 normally. During a merge conflict, 1, 2 and 3 hold the base, ours and theirs (see "Resolving Merge Conflicts") |
| Path | The full path from the top of the repository |
Note that the index is flat: it lists src/FeeCalculator.java with its full path, not a src folder. Trees are only built from it when you commit. The index also stores each file's size and timestamps, which lets git status skip reading files that haven't changed since the last check. That is why git status is fast even in a large project.
When you stage a change, only the blob for that path changes in the index:
echo "Run the app." >> README.md
git add README.md
git ls-files --stage README.md
git rev-parse HEAD:README.md100644 74a620229cecfa28980795f4058da606a303344e 0 README.md
d5924b0d02d9ef4f29fef5369a7ddcaf82c830c7Explanation: the index now points README.md to a new blob, while the last commit still has the old one. That difference is exactly what git diff --staged shows: index against HEAD.
| Command | Description |
|---|---|
git ls-files --stage | It is used to list the index with modes, blobs and stages. |
git ls-files -v | It is used to list tracked files with a status letter (lowercase means "assume unchanged", S means "skip worktree"). |
git update-index --add --cacheinfo <mode>,<id>,<path> | It is used to put a blob into the index under a path, without a file on disk. |
git update-index --chmod=+x <file> | It is used to mark a file executable in the index (useful on Windows). |
git update-index --skip-worktree <file> | It is used to make Git ignore your local changes to a tracked file. |
Ignoring Local Changes to a Tracked File
A team keeps settings.properties in the repository with default values, and Navika needs her own values locally without committing them. .gitignore doesn't help, because the file is tracked. --skip-worktree tells Git to keep using the index's version and not look at the file on disk:
git commit --quiet -m "Explain how to run the app"
git update-index --skip-worktree settings.properties
echo "theme=dark" > settings.properties
git status -s
git ls-files -vH README.md
S settings.properties
H src/FeeCalculator.javaExplanation: the status is clean, although the file changed on disk, and git ls-files -v marks it with S. Turn it off with --no-skip-worktree. A similar flag, --assume-unchanged, exists as a performance hint (for huge files that rarely change), and Git may drop it at any time. For "keep my local settings", --skip-worktree is the right one, but a better long-term design is a committed template (settings.example.properties) plus an ignored local file.
Refs
A ref is a name for an object ID. Most refs are files under .git/refs/, organized in namespaces:
| Namespace | Holds |
|---|---|
refs/heads/ | Local branches |
refs/tags/ | Tags |
refs/remotes/<remote>/ | Remote-tracking branches |
refs/stash | The newest stash |
refs/notes/ | Git notes (extra text attached to commits) |
refs/bisect/ | A bisect in progress |
HEAD is a symbolic ref: instead of an ID, it names another ref. Plumbing commands read and change refs:
| Command | Description |
|---|---|
git show-ref | It is used to list refs with their IDs. |
git for-each-ref --format="..." | It is used to list refs with a custom format, for scripts. |
git symbolic-ref HEAD | It is used to show (or set) the ref HEAD points to. |
git update-ref <ref> <id> | It is used to create or move a ref safely. |
git update-ref -d <ref> | It is used to delete a ref. |
git rev-parse <name> | It is used to resolve any name to an ID. |
git pack-refs --all | It is used to collect refs into .git/packed-refs. |
git tag v1.0.0
git branch feature/receipts
git show-ref
git symbolic-ref HEAD96867e4c6171b50250313a3770c9f83e516d51d6 refs/heads/feature/receipts
96867e4c6171b50250313a3770c9f83e516d51d6 refs/heads/main
96867e4c6171b50250313a3770c9f83e516d51d6 refs/tags/v1.0.0
refs/heads/maingit for-each-ref prints any fields you like, which makes it the right tool for scripts (porcelain like git branch may change its format):
git for-each-ref --format="%(refname:short) %(objecttype) %(objectname:short) %(subject)"feature/receipts commit 96867e4 Explain how to run the app
main commit 96867e4 Explain how to run the app
v1.0.0 commit 96867e4 Explain how to run the appCreating a Branch with Plumbing
git branch is a friendly wrapper around git update-ref. The plumbing version creates the same ref, and also records it in the reflog with a message you choose:
git update-ref -m "created with update-ref" refs/heads/feature/hostel HEAD~1
git branch -v
git reflog show feature/hostel feature/hostel 90c874a Start the project
feature/receipts 96867e4 Explain how to run the app
* main 96867e4 Explain how to run the app
90c874a (feature/hostel) feature/hostel@{0}: created with update-refExplanation: refs/heads/feature/hostel now points to the first commit, so it appears in git branch like any branch. Unlike writing the file by hand, update-ref locks the ref, writes the reflog, and can check the old value (git update-ref <ref> <new> <old> refuses if the ref doesn't currently point to <old>), which is how Git avoids lost updates.
Loose and Packed Refs
Each ref starts as a small file. Repositories with thousands of tags or branches would have thousands of files, so git pack-refs (run by git gc) moves them into one file, .git/packed-refs:
git pack-refs --all
find .git/refs -type f
cat .git/packed-refs# pack-refs with: peeled fully-peeled sorted
90c874afda6656a126322c0ae9e18cdd0479ec79 refs/heads/feature/hostel
96867e4c6171b50250313a3770c9f83e516d51d6 refs/heads/feature/receipts
96867e4c6171b50250313a3770c9f83e516d51d6 refs/heads/main
96867e4c6171b50250313a3770c9f83e516d51d6 refs/tags/v1.0.0The ref files are gone, and every ref is a line in packed-refs. Git checks both places: a loose file, if present, wins over the packed line. That is why you should read refs with git show-ref or git rev-parse, never by opening files in .git/refs/.
How Git Resolves a Name
When you type a short name such as main, Git looks for it in this order, and uses the first match: .git/<name> (for HEAD, ORIG_HEAD and similar), refs/<name>, refs/tags/<name>, refs/heads/<name>, refs/remotes/<name>, and refs/remotes/<name>/HEAD. If a branch and a tag share a name, Git warns:
git tag feature/receipts HEAD~1
git rev-parse --short feature/receiptswarning: refname 'feature/receipts' is ambiguous.
90c874aThe tag (refs/tags/ comes before refs/heads/) won, and Git warned that the name is ambiguous. Use the full name, such as refs/heads/feature/receipts, when in doubt, and avoid giving tags and branches the same name.
Common Mistakes
Editing .git/refs or .git/index by hand. Use git update-ref and git update-index, which lock files and write reflogs.
Using --assume-unchanged to keep local settings. Git may reset it. Use --skip-worktree, or better, a template file plus an ignored local file.
Forgetting a --skip-worktree flag. Changes to the file are silently ignored, even ones you want to commit. git ls-files -v | grep "^S" lists them.
Reading refs from files. Packed refs aren't in .git/refs/. Use git show-ref or git for-each-ref.
Branches and tags with the same name. Commands become ambiguous. Keep separate naming schemes, such as v1.2.0 for tags.
Under the Hood
.git/index is a binary file: a header with a version and entry count, then one entry per path (timestamps, file size, mode, blob ID, flags and the path), then optional extensions, and a checksum of the whole file. The flags hold the stage number and bits such as skip-worktree. Extensions include the cache tree, which remembers the tree IDs of folders that haven't changed since the last commit, so git commit doesn't have to rebuild them.
Refs are text: a loose ref file holds a 40-character ID and a new line, a symbolic ref holds ref: and a name, and packed-refs holds one <id> <name> line per ref, with ^<id> lines after annotated tags giving the object they point to.
Interview Corner
What is the Git index?
A binary file, .git/index, that lists every tracked path with its mode, the blob ID to commit, a stage number and cached file metadata. It represents the next commit's snapshot, and git commit builds trees from it.
What is the difference between --assume-unchanged and --skip-worktree?
Both make Git ignore changes to a tracked file. --assume-unchanged is a performance hint that Git may discard. --skip-worktree means "keep the index version, don't touch my local file", which suits local configuration changes.
What is a ref, and what is a symbolic ref?
A ref is a name that points to an object ID, such as refs/heads/main or refs/tags/v1.0.0. A symbolic ref points to another ref instead. HEAD is the usual example: ref: refs/heads/main.
What are packed refs?
Refs stored together as lines in .git/packed-refs instead of one file each, created by git pack-refs (and git gc). Loose ref files take precedence over packed entries.
How do you create a branch with plumbing commands?
git update-ref refs/heads/<name> <commit>. It writes the ref safely and records a reflog entry.
Practice
Challenge: undo the skip. settings.properties is still marked skip-worktree with local changes. Turn the flag off, and show what git status -s and git ls-files -v print now.
What does Git print? Before you open the solution, decide whether the local change to the file appears again.
Show solution
git update-index --no-skip-worktree settings.properties
git status -s
git ls-files -v settings.properties M settings.properties
H settings.propertiesThe change reappears as M: it was there all along, Git just wasn't looking. The S became H, which means a normal tracked file. To keep your local value without the flag, restore the file and move your settings to an ignored file.
Checkpoint. In a practice repository, list the index with git ls-files --stage, stage a change and compare its blob with git rev-parse HEAD:<file>. Then create, list and delete a branch using only git update-ref and git for-each-ref.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git ls-files --stage | Show the index |
git update-index --skip-worktree <file> | Ignore local changes to a tracked file |
git show-ref | List refs |
git for-each-ref --format="%(refname:short)" | List refs for scripts |
git symbolic-ref HEAD | Show the current branch's ref |
git update-ref refs/heads/<name> <commit> | Create or move a branch safely |
git pack-refs --all | Pack refs into one file |
Was this tutorial helpful?