git/list[1] front-page[2] threads[3] people[4] search[5] about
 

git index: how does it work?

From
Shaun Cutts <shaun@cuttshome.net>
Date
Aug 5, 2009, 16:21 UTC
Message-ID
<loom.20090805T160528-69@post.gmane.org>
Hello,

I am wondering if someone could explain and/or point me to an explanation of how the git index works.

For instance, suppose I have a tracked file: "foo.c"
1) [I modify "foo.c"]
2) git add foo.c
3) [modify again]
4) git commit -m "blah blah"

Since I don't include the "-a" switch, the version I added on step 2 is committed. But how does the index keep track of these changes? Does the index file actually contain the hunks of "foo.c" that have been modified? Or is there a "temporary" blob created, which the index points to?

In either case, is there some interface to access these hunks and/or get a reference to the blob?

Thanks,
-- Shaun

PS I'm considering writing an extension to git where the "diff" understands the semantics of certain types of files: hunks wouldn't just be textual blobs but would try to represent a minimal change from one version to the next based on an edit distance, so that, e.g. changing the location of a function would be represented by a "move" edit, rather than two text changes.

I have been building a prototype as a wrapper around git, intervening to store extra information, etc before passing commands on to git. Blobs, commits, etc are nice abstractions I can leave as is, but the index seems sort of foggy to me. Any advice appreciated!

Next: Junio C Hamano
Message 1 of 9 in “git index: how does it work?”
  1. Shaun CuttsAug 5, 2009
  2. Junio C HamanoAug 5, 2009
  3. Shaun CuttsAug 12, 2009
  4. Sverre RabbelierAug 12, 2009
  5. Shaun CuttsAug 12, 2009
  6. Björn SteinbrinkAug 12, 2009
  7. Junio C HamanoAug 12, 2009
  8. Sverre RabbelierAug 5, 2009
  9. Shaun CuttsAug 5, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.