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 [<options>...] <tree> [(-F <file> | -m [<message>]... )]

PARAMETERS

-p <commit>, --parent=<commit>
    Specify parent commit ID. Repeat for multiple parents (e.g., merges).

-m <message>, --message=<message>
    Set commit message. UTF-8; repeatable for multi-part messages.

-F <file>, --file=<file>
    Read message from file. Use - for stdin.

-S[<keyid>], --gpg-sign[=<keyid>]
    GPG-sign commit using specified key or default.

--no-gpg-sign
    Suppress GPG signing even if configured.

--cleanup=<mode>
    Message cleanup: verbatim, whitespace, strip, or default.

--[no-]verify
    Bypass (--no-verify) or enforce (--verify) pre-commit hook.

DESCRIPTION

git commit-tree is a plumbing command for creating Git commit objects directly from a tree object, optional parent commits, and a message. It bypasses the working tree and index, requiring an existing tree (e.g., from git write-tree or git mktree) and explicit parents.

Primarily used in scripts, porcelain commands, or advanced operations like bare repo commits or data migrations. It supports merge commits (multiple -p), GPG signing, message preprocessing, and optional hook verification.

The command outputs the SHA-1 or object ID of the new commit to stdout. No references are updated; use git update-ref to store it. Ideal for precise control but error-prone for casual use.

CAVEATS

Low-level; does not update refs, index, or branches. Verify inputs to avoid invalid objects. Bypasses normal safeguards unless --verify. Use object IDs, not refs.

OUTPUT

Always prints the OID of the new commit object to stdout.
Example: tree=$(git write-tree); echo 'msg' | git commit-tree $tree -p HEAD

COMMON USAGE

Create initial commit: git commit-tree $(git write-tree) -m 'Initial commit'
Merge: git commit-tree newtree -p HEAD -p other

HISTORY

Introduced in Git v0.99 (2005) as core plumbing; stable since Git 1.0. Evolved with GPG support (v1.7.9), verify flag (v2.0), and OID flexibility for SHA-256.

SEE ALSO

Copied to clipboard