The Index and Refs

Updated · 11 min read
hardGit 2.53gitindexrefsupdate-refskip-worktreeinterview

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

Terminal
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 --oneline
Output
90c874a (HEAD -> main) Start the project

The Index

git ls-files --stage prints the index, one line per tracked path:

Terminal
git ls-files --stage
Output
100644 d5924b0d02d9ef4f29fef5369a7ddcaf82c830c7 0	README.md
100644 94de35974250949ae6aa2436d59df0ddab8a397e 0	settings.properties
100644 2bec87b439cb8e1072c1ec91431b301e1c2e6d68 0	src/FeeCalculator.java
ColumnMeaning
100644The mode, as in a tree (normal file)
IDThe blob the next commit will use for this path
0The stage number: 0 normally. During a merge conflict, 1, 2 and 3 hold the base, ours and theirs (see "Resolving Merge Conflicts")
PathThe 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:

Terminal
echo "Run the app." >> README.md
git add README.md
git ls-files --stage README.md
git rev-parse HEAD:README.md
Output
100644 74a620229cecfa28980795f4058da606a303344e 0	README.md
d5924b0d02d9ef4f29fef5369a7ddcaf82c830c7

Explanation: 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.

CommandDescription
git ls-files --stageIt is used to list the index with modes, blobs and stages.
git ls-files -vIt 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:

Terminal
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 -v
Output
H README.md
S settings.properties
H src/FeeCalculator.java

Explanation: 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:

NamespaceHolds
refs/heads/Local branches
refs/tags/Tags
refs/remotes/<remote>/Remote-tracking branches
refs/stashThe 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:

CommandDescription
git show-refIt 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 HEADIt 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 --allIt is used to collect refs into .git/packed-refs.
Terminal
git tag v1.0.0
git branch feature/receipts
git show-ref
git symbolic-ref HEAD
Output
96867e4c6171b50250313a3770c9f83e516d51d6 refs/heads/feature/receipts
96867e4c6171b50250313a3770c9f83e516d51d6 refs/heads/main
96867e4c6171b50250313a3770c9f83e516d51d6 refs/tags/v1.0.0
refs/heads/main

git for-each-ref prints any fields you like, which makes it the right tool for scripts (porcelain like git branch may change its format):

Terminal
git for-each-ref --format="%(refname:short) %(objecttype) %(objectname:short) %(subject)"
Output
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 app

Creating 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:

Terminal
git update-ref -m "created with update-ref" refs/heads/feature/hostel HEAD~1
git branch -v
git reflog show feature/hostel
Output
  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-ref

Explanation: 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:

Terminal
git pack-refs --all
find .git/refs -type f
cat .git/packed-refs
Output
# 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.0

The 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/.

Refs are names that point at commits, sometimes through other names

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:

Terminal
git tag feature/receipts HEAD~1
git rev-parse --short feature/receipts
Output
warning: refname 'feature/receipts' is ambiguous.
90c874a

The 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
Terminal
git update-index --no-skip-worktree settings.properties
git status -s
git ls-files -v settings.properties
Output
 M settings.properties
H settings.properties

The 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.

Quick check: The index and refs

4 questions
1What does each line of git ls-files --stage show?
2Which flag makes Git ignore local changes to a tracked settings file?
3What does git symbolic-ref HEAD print on main?
4A branch and a tag are both called release. Which does git rev-parse release use?

Key Takeaways

Quick reference

CommandIt is used to
git ls-files --stageShow the index
git update-index --skip-worktree <file>Ignore local changes to a tracked file
git show-refList refs
git for-each-ref --format="%(refname:short)"List refs for scripts
git symbolic-ref HEADShow the current branch's ref
git update-ref refs/heads/<name> <commit>Create or move a branch safely
git pack-refs --allPack refs into one file

Was this tutorial helpful?

Lesson 74 of 106