LinuxCommandLibrary

git-mktree

Create tree object from staged files

TLDR

Build a tree object and verify that each tree entry's hash identifies an existing object

$ git mktree
copy

Allow missing objects
$ git mktree --missing
copy

Read the NUL ([z]ero character) terminated output of the tree object (ls-tree -z)
$ git mktree -z
copy

Allow the creation of multiple tree objects
$ git mktree --batch
copy

Sort and build a tree from stdin (non-recursive git ls-tree output format is required)
$ git mktree < [path/to/tree.txt]
copy

SYNOPSIS

git mktree [-z | --zero-terminate] [--missing] [--batch [--batch-check | --no-batch-check]]

PARAMETERS

-z | --zero-terminate
    Specifies that the input lines from standard input are null-terminated instead of newline-terminated. This is useful for processing filenames that may contain newlines.

--missing
    Allows the creation of a tree object even if some of the specified object IDs (blobs or subtrees) do not yet exist in the object database. This is useful in workflows where objects might be created later in a pipeline.

--batch
    Enables batch mode, allowing multiple tree creation requests to be processed in a single invocation. Each request is terminated by a blank line or a null character if -z is used. The command prints the object ID for each created tree.

--batch-check
    When used with --batch, this option ensures that all referenced objects (blobs, trees) exist in the object database before creating the tree. This is the default behavior in batch mode if neither --batch-check nor --no-batch-check is specified.

--no-batch-check
    When used with --batch, this option disables the check for existence of referenced objects. Similar to the behavior of --missing but applies to batch mode.

DESCRIPTION

git-mktree is a low-level "plumbing" command within Git, designed to create a new tree object in the Git object database. It reads lines from its standard input, with each line describing an entry (file or subdirectory) to be included in the new tree. Each line must follow a strict format: mode type object-id name. This command does not interact with the Git working directory or index directly; its sole purpose is to programmatically construct a tree object based on the provided specifications.

Upon successful creation, git-mktree prints the SHA-1 hash (or other configured object ID) of the newly formed tree object to standard output. It is a fundamental building block for scripting and higher-level Git commands like git commit-tree that need to assemble specific tree structures without relying on the current state of the working directory or index. It's particularly useful for operations that involve constructing Git history or manipulating objects directly.

CAVEATS

git-mktree is a plumbing command, meaning it's primarily intended for scripting and internal Git operations rather than direct end-user interaction. It expects highly specific input formatting, which can be prone to errors if not generated correctly.

The command does not create any actual files in the working directory; it only creates the tree object in the Git object database. The object IDs provided in the input must generally refer to existing objects in the repository's object database, unless the --missing option is used.

INPUT FORMAT

The command reads entries from standard input, with each line (or null-terminated string) representing an entry for the new tree. The format for each line is:

<mode> <type> <object-id><TAB><name>

Where:

  • <mode>: The file mode (e.g., 100644 for a regular file, 100755 for executable, 120000 for a symbolic link, 040000 for a subdirectory).
  • <type>: The type of Git object (blob for a file, tree for a subdirectory, commit for a submodule/gitlink).
  • <object-id>: The SHA-1 hash (or other object ID) of the corresponding blob or tree object.
  • <name>: The name of the file or subdirectory within the new tree.

Fields are separated by a space, except for the last field (name) which is separated from the object-id by a TAB character. For example: 100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 README.md

OUTPUT

Upon successful creation of the tree object, git-mktree prints the SHA-1 hash (or other configured object ID) of the newly created tree object to standard output, followed by a newline character. In --batch mode, it prints the ID for each tree created.

SEE ALSO

Copied to clipboard