tags:

views:

79

answers:

2

EDIT: If you downvote my question have a decency to say why.

In Pro Git Ch9 the author says:

Git normally creates a tree by taking the state of your staging area or index and writing a tree object from it.

My question is how does git know which of two consequitive index entries to create the Tree object from?

For example (the random numbers are meant to be 40-char SHA1's - I just made em up):

$ echo 'First change' > one.txt
$ git add one.txt 
$ find .git/objects -type f
.git/objects/1f/755a7fffe4   //first index entry

$ echo 'Second change' > one.txt
$ git add one.txt
$ find .git/objects -type f
.git/objects/2d/234asdf2   //second index entry

$ git commit -a -m "Initial commit"
$ git cat-file master^{tree}
100644 blob 2d234asdf2 one.txt  //How did it know not to take 1f755??

Does it just look at the blob timestamps? Also - what happens to the first blob created - no one is referencing it. Does it just get destroyed or forgotten?

+1  A: 

git creates the commit-tree from the file .git/index, the index stores the current tree and all related information. what you see in .git/objects/… are the actual blob objects of your file one.txt, not index objects

knittl
Yes I know that. But how does it know to take the SECOND blob not the first? Is there some pointer to the latest blob in the Index?I mean when you ADD the file there is no TREE object created.
drozzy
+1  A: 
  1. $ echo 'First change' > one.txt
    $ git add one.txt 
    $ find .git/objects -type f
    .git/objects/1f/755a7fffe4...   # first index entry (repository database)
    $ git ls-files --cached --stage --exclude-standard one.txt
    100644 1f755a7fffe4... 0       one.txt   # in 'the index'
    
  2. $ echo 'Second change' > one.txt
    $ git add one.txt
    $ find .git/objects -type f
    .git/objects/2d/234asdf2...     # second index entry (repository database)
    $ git ls-files --cached --stage --exclude-standard one.txt
    100644 2d234asdf2... 0       one.txt     # in 'the index'
    
  3. $ git commit -a -m "Initial commit"
    $ git cat-file -p master^{tree}
    100644 blob 2d234asdf2... one.txt  # the one from 'the index'
    
  4. "git gc" would prune (remove) loose dangling object .git/objects/1f/755a7fffe4... (only after some delay, for safety reasons).

Jakub Narębski
So... it keeps only the LAST "added" blob to index?
drozzy
@drozzy: Index references LAST "added" blob, git-commit pick up the state described by index.
Jakub Narębski
Got any proof of that? :-) By last you mean - chronologically last? What if I turn my clock backwards...
drozzy
@drozzy: Last as in "last in the sequence". Each "git add file" *overwrites* (updates to the new value) blob reference in the index.
Jakub Narębski
That makes sense. Cool!
drozzy