Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create tree without using staging area

Tags:

git

In the git internals chapter of git-scm book there is an example of how git tree can be created:

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.

And then they list commands that I can use to create the tree. My question is whether I can create a tree without using index (staging area)? For example instead of doing this:

git update-index --add --cacheinfo 100644 83baae618... test.txt

Use something like this:

git create tree --add --cacheinfo 100644 83baae618... test.txt

Update based on Ismail Badawi's anser:

$ echo 'making tree' | git hash-object -w --stdin
07dae42a0730df1cd19b0ac693c6894a02ed6ad0

and then

$ echo -e '100644 blob 07dae42a0730df1cd19b0ac693c6894a02ed6ad0 \maketree.txt' | git mktree
fatal: input format error: 100644 blob 07dae42a0730df1cd19b0ac693c6894a02ed6ad0 \maketree.txt
like image 387
Max Koretskyi Avatar asked Jan 17 '26 15:01

Max Koretskyi


1 Answers

You can use git mktree, like this:

echo -e "100644 blob 83baae618...\ttest.txt" | git mktree

(You need to write echo -e because of the literal tab. This is a shell thing, not a git thing).

Note this creates a tree that only points to 83baae618, so it's not exactly the same as your update-index invocation, which adds to the index (which typically already points to other things). You can pass multiple lines to git mktree, each line describing one blob or tree.

like image 188
Ismail Badawi Avatar answered Jan 20 '26 13:01

Ismail Badawi