---
title: "Create a tree"
method: POST
path: "/repos/{owner}/{repo}/git/trees"
tags: ["git"]
---

# Create a tree

`POST /repos/{owner}/{repo}/git/trees`

The tree creation API accepts nested entries. If you specify both a tree and a nested path modifying that tree, this endpoint will overwrite the contents of the tree with the new path contents, and create a new tree structure.

If you use this endpoint to add, delete, or modify the file contents in a tree, you will need to commit the tree and then update a branch to point to the commit. For more information see "[Create a commit](https://docs.github.com/rest/git/commits#create-a-commit)" and "[Update a reference](https://docs.github.com/rest/git/refs#update-a-reference)."

Returns an error if you try to delete a file that does not exist.

## Path parameters

- `owner` string, required
- `repo` string, required

## Request body

- object
  - `tree` object[], required — Objects (of `path`, `mode`, `type`, and `sha`) specifying a tree structure.
    - `path` string — The file referenced in the tree.
    - `mode` '100644' | '100755' | '040000' | '160000' | '120000' — The file mode; one of `100644` for file (blob), `100755` for executable (blob), `040000` for subdirectory (tree), `160000` for submodule (commit), or `120000` for a blob that specifies the path of a symlink.
    - `type` 'blob' | 'tree' | 'commit' — Either `blob`, `tree`, or `commit`.
    - `sha` string, nullable — The SHA1 checksum ID of the object in the tree. Also called `tree.sha`. If the value is `null` then the file will be deleted. **Note:** Use either `tree.sha` or `content` to specify the contents of the entry. Using both `tree.sha` and `content` will return an error.
    - `content` string — The content you want this file to have. GitHub will write this blob out and use that SHA for this entry. Use either this, or `tree.sha`. **Note:** Use either `tree.sha` or `content` to specify the contents of the entry. Using both `tree.sha` and `content` will return an error.
  - `base_tree` string — The SHA1 of an existing Git tree object which will be used as the base for the new tree. If provided, a new Git tree object will be created from entries in the Git tree object pointed to by `base_tree` and entries defined in the `tree` parameter. Entries defined in the `tree` parameter will overwrite items from `base_tree` with the same `path`. If you're creating new changes on a branch, then normally you'd set `base_tree` to the SHA1 of the Git tree object of the current latest commit on the branch you're working on. If not provided, GitHub will create a new Git tree object from only the entries defined in the `tree` parameter. If you create a new commit pointing to such a tree, then all files which were a part of the parent commit's tree and were not defined in the `tree` parameter will be listed as deleted by the new commit.

## Response `201`

Response

- GitTree — The hierarchy between files in a Git repository.
  - `sha` string, required
  - `url` string, uri
  - `truncated` boolean, required
  - `tree` object[], required — Objects specifying a tree structure
    - `path` string, required
    - `mode` string, required
    - `type` string, required
    - `sha` string, required
    - `size` integer
    - `url` string

## Other responses

- `403` — Forbidden
- `404` — Resource not found
- `409` — Conflict
- `422` — Validation failed, or the endpoint has been spammed.

---

[API](https://skmtc.net/github/apis/rest-api.md) · [All operations](https://skmtc.net/github/apis/rest-api/llms.txt) · [OpenAPI document](https://skmtc-service-staging.skmtc.workers.dev/v1/apis/github/rest-api/versions/80850db290cd/schema)
