代码之家  ›  专栏  ›  技术社区  ›  Andriy Drozdyuk Pickels

Git如何知道要添加到树中的索引blob?

git
  •  -1
  • Andriy Drozdyuk Pickels  · 技术社区  · 14 年前

    编辑:如果你投了反对票,我的问题有一个体面的理由。

    Pro Git 作者说:

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

    我的问题是Git怎么知道 哪一个 要从中创建树对象的两个必需索引项中的一个?

    例如(随机数应该是40个字符的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 回复  |  直到 14 年前
        1
  •  2
  •   Jakub Narębski adamtaub    14 年前
    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... (为了安全起见,只有在耽搁了一段时间之后)。

        2
  •  1
  •   knittl    14 年前

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