Git如何知道要添加到树中的哪个Index blob?

时间:2010-04-27 14:23:45

标签: git

编辑:如果你对我的问题进行了投票,请说明原因。

Pro Git Ch9中,作者说:

  

Git通常通过获取暂存区域或索引的状态并从中写入树对象来创建树。

我的问题是git如何知道哪两个的两个连续索引条目来创建Tree对象?

例如(随机数字应该是40-char SHA1 - 我刚刚制作它们):

$ 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??

它只是看blob时间戳吗? 此外 - 创建的第一个blob会发生什么 - 没有人引用它。它会被摧毁还是被遗忘?

2 个答案:

答案 0 :(得分:2)

  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”将修剪(移除)松散的悬空物.git/objects/1f/755a7fffe4...(出于安全原因,仅在延迟一段时间后)。

答案 1 :(得分:1)

git从文件.git/index创建提交树,索引存储当前树和所有相关信息。您在.git/objects/…中看到的是文件one.txt的实际blob对象,而不是索引对象