在不使用临时区域的情况下创建树

Max*_*kyi 2 git

在本书的git内部章节中,git-scm有一个如何创建git树的示例:

Git normally creates a tree by taking the state of your staging area or index and writing a series of tree objects from it. So, to create a tree object, you first have to set up an index by staging some files.

然后他们列出了我可以用来创建树的命令.我的问题是我是否可以在不使用索引(临时区域)的情况下创建树?例如,而不是这样做:

git update-index --add --cacheinfo 100644 83baae618... test.txt
Run Code Online (Sandbox Code Playgroud)

使用这样的东西:

git create tree --add --cacheinfo 100644 83baae618... test.txt
Run Code Online (Sandbox Code Playgroud)

基于Ismail Badawi的anser 更新:

$ echo 'making tree' | git hash-object -w --stdin
07dae42a0730df1cd19b0ac693c6894a02ed6ad0
Run Code Online (Sandbox Code Playgroud)

然后

$ echo -e '100644 blob 07dae42a0730df1cd19b0ac693c6894a02ed6ad0 \maketree.txt' | git mktree
fatal: input format error: 100644 blob 07dae42a0730df1cd19b0ac693c6894a02ed6ad0 \maketree.txt
Run Code Online (Sandbox Code Playgroud)

Ism*_*awi 5

你可以git mktree像这样使用:

echo -e "100644 blob 83baae618...\ttest.txt" | git mktree
Run Code Online (Sandbox Code Playgroud)

(你需要写echo -e因为文字标签.这是一个shell的东西,而不是一个git的东西).

请注意,这会创建一个仅指向的树83baae618,因此它与您的update-index调用不完全相同,后者会添加到索引(通常已指向其他内容).您可以传递多行git mktree,每行描述一个blob或树.

  • @Maximus git中的对象是不可变的(因为更改内容会改变哈希).您将使用旧树的内容以及您要添加的任何内容创建一个新树.您可以使用`git ls-tree`以`git mktree`格式列出树的内容并理解输出以进行所需的更改,或者您可以使用索引 - `git read-tree`将树的内容读入索引,进行更改(`git add`等),然后使用`git write-tree`创建新树. (2认同)