LinuxCommandLibrary

git-commit-tree

Create a new commit object

TLDR

Create a commit object with the specified message

$ git commit-tree [tree] -m "[message]"
copy

Create a commit object reading the message from a file (use - for stdin)
$ git commit-tree [tree] -F [path/to/file]
copy

Create a GPG-signed commit object
$ git commit-tree [tree] -m "[message]" [[-S|--gpg-sign]]
copy

Create a commit object with the specified parent commit object
$ git commit-tree [tree] -m "[message]" -p [parent_commit_sha]
copy

SYNOPSIS

git commit-tree <tree> [-p <parent>...] [-S[<keyid>]] [-m <message>] [-F <file>] [-b <gpgsign_opt>]...

PARAMETERS

<tree>
    The SHA-1 ID of the tree object that this new commit will point to. This tree represents the complete snapshot of the repository's contents at the time of the commit.

-p <parent>
--parent=<parent>

    Specifies the SHA-1 ID of a parent commit. This option can be used multiple times to specify multiple parents, which is typical for merge commits. For the initial commit in a repository, no parent should be specified.

-m <message>
--message=<message>

    Provides the commit message directly on the command line. If multiple -m options are given, their values are concatenated into separate paragraphs of the commit message.

-F <file>
--file=<file>

    Reads the commit message from the specified <file>. Use - as the <file> to read the message from standard input.

-S[<keyid>]
--gpg-sign[=<keyid>]

    GPG-sign the commit. An optional <keyid> can be provided to specify the GPG key to use for signing. If no <keyid> is given, the default signing key will be used.

-b <gpgsign_opt>
--gpg-sign-opt=<gpgsign_opt>

    Passes additional, specific arguments to the `gpg --sign` command when signing the commit. This option can be specified multiple times.

DESCRIPTION

The git-commit-tree command is a low-level "plumbing" command in Git, used to create a new commit object directly in the Git object database.

It takes the SHA-1 ID of a tree object (representing the state of the directory), zero or more parent commit IDs (to define the commit's history), and the commit message. After successfully creating the commit object, it prints its SHA-1 ID to standard output.

Unlike the higher-level git commit command, git-commit-tree does not automatically stage changes, nor does it interact with the index or worktree. It requires the user to explicitly provide all necessary object IDs, making it suitable for scripting or specialized Git operations rather than routine day-to-day use.

CAVEATS

git-commit-tree is a low-level "plumbing" command intended primarily for scripting and advanced Git operations. It does not perform any of the automatic staging, indexing, or worktree interactions that the more common git commit command handles.

Using this command incorrectly can lead to a corrupted repository history or unreferenced objects. It requires precise knowledge of Git's object model and object IDs.

USAGE SCENARIO

A common scenario for using git-commit-tree directly is when programmatically creating commits without a traditional working directory or index, or when building custom Git tools. For example, one might first use git write-tree to create a tree object from a set of files, and then use git-commit-tree to turn that tree into a commit.

HISTORY

git-commit-tree is fundamental to Git's design, being one of the core low-level commands that manipulate Git's object database. It has been a part of Git since its early development, reflecting Linus Torvalds' initial approach of building Git from a set of simple, composable tools. Its stable interface allows higher-level commands and external scripts to reliably create commit objects.

SEE ALSO

Copied to clipboard