2

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
Max Koretskyi
  • 85,840
  • 48
  • 270
  • 414

1 Answers1

5

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.

Ismail Badawi
  • 31,611
  • 6
  • 76
  • 92
  • Thanks, I tried the solution but received `input format error`. Please see my udpated – Max Koretskyi Dec 04 '14 at 09:27
  • 1
    @Maximus You need a tab (`\t`) after the SHA, not a space. `07dae42a0730df1cd19b0ac693c6894a02ed6ad0\tmaketree.txt` – Ismail Badawi Dec 04 '14 at 15:40
  • Hey, I've got one more question, how can I add a tree\file to an existing tree? Is it possible? Thanks in advance! – Max Koretskyi Dec 05 '14 at 18:07
  • 2
    @Maximus Objects in git are immutable (because changing the content would change the hash). You would instead create a new tree with the old tree's content plus whatever you want to add. You can use `git ls-tree` to list the contents of a tree in a format `git mktree` understands and mess with the output to make the changes you want, or you can use the index -- `git read-tree` to read a tree's content into the index, make your changes (`git add` etc), then `git write-tree` to make a new tree. – Ismail Badawi Dec 05 '14 at 20:16
  • Great, thanks for you elaborate answer! I've managed to add second tree to the first when creating first :). Do you where I can read about those permissions that I specify when creating a tree, e.g. 100644 for blobs, 040000 for tree? – Max Koretskyi Dec 06 '14 at 06:48
  • 1
    @Maximus http://stackoverflow.com/questions/737673/how-to-read-the-mode-field-of-git-ls-trees-output – Ismail Badawi Dec 06 '14 at 23:16
  • Appreciate! So basically 100644 says that blob corresponds to a regular file which can be read and written by user. And since blobs are immutable, I'm guessing that those permissions are used when checking out a working tree to set permissions to files\directories corresponding to trees entries, correct? – Max Koretskyi Dec 07 '14 at 09:00