# Create a tree

From **GitHub v3 REST API**.

`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/enterprise-server@3.0/rest/reference/git#create-a-commit)" and "[Update a reference](https://docs.github.com/enterprise-server@3.0/rest/reference/git#update-a-reference)."

## Parameters

### `owner`

- Location: path
- Required: true
- Type: `string`

### `repo`

- Location: path
- Required: true
- Type: `string`

## Request body

- Required: true
### `application/json`

- Type: `object`

```json
{
  "base_tree": "9fb037999f264ba9a7fc6274d15fa3ae2ab98312",
  "tree": [
    {
      "mode": "100644",
      "path": "file.rb",
      "sha": "44b4fc6d56897b048c772eb4087f854f46256132",
      "type": "blob"
    }
  ]
}
```

```json
{"properties":{"base_tree":{"description":"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.\nIf 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.\n","type":"string"},"tree":{"description":"Objects (of `path`, `mode`, `type`, and `sha`) specifying a tree structure.","items":{"properties":{"content":{"description":"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`.  \n  \n**Note:** Use either `tree.sha` or `content` to specify the contents of the entry. Using both `tree.sha` and `content` will return an error.","type":"string"},"mode":{"description":"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.","enum":["100644","100755","040000","160000","120000"],"type":"string"},"path":{"description":"The file referenced in the tree.","type":"string"},"sha":{"description":"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.  \n  \n**Note:** Use either `tree.sha` or `content` to specify the contents of the entry. Using both `tree.sha` and `content` will return an error.","nullable":true,"type":"string"},"type":{"description":"Either `blob`, `tree`, or `commit`.","enum":["blob","tree","commit"],"type":"string"}},"type":"object"},"type":"array"}},"required":["tree"],"type":"object"}
```

## Responses

### `201`

Response

#### Headers

##### `Location`

- Type: `string`

- Example: `https://api.github.com/repos/octocat/Hello-World/trees/cd8274d15fa3ae2ab983129fb037999f264ba9a7`

### `application/json`

- Type: `object`

The hierarchy between files in a Git repository.

```json
{
  "sha": "cd8274d15fa3ae2ab983129fb037999f264ba9a7",
  "tree": [
    {
      "mode": "100644",
      "path": "file.rb",
      "sha": "7c258a9869f33c1e1e1f74fbb32f07c86cb5a75b",
      "size": 132,
      "type": "blob",
      "url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/7c258a9869f33c1e1e1f74fbb32f07c86cb5a75b"
    }
  ],
  "truncated": true,
  "url": "https://api.github.com/repos/octocat/Hello-World/trees/cd8274d15fa3ae2ab983129fb037999f264ba9a7"
}
```

```json
{"description":"The hierarchy between files in a Git repository.","properties":{"sha":{"type":"string"},"tree":{"description":"Objects specifying a tree structure","example":[{"mode":"100644","path":"file.rb","properties":{"mode":{"type":"string"},"path":{"type":"string"},"sha":{"type":"string"},"size":{"type":"integer"},"type":{"type":"string"},"url":{"type":"string"}},"required":["path","mode","type","sha","url","size"],"sha":"44b4fc6d56897b048c772eb4087f854f46256132","size":30,"type":"blob","url":"https://api.github.com/repos/octocat/Hello-World/git/blobs/44b4fc6d56897b048c772eb4087f854f46256132"}],"items":{"properties":{"mode":{"example":"040000","type":"string"},"path":{"example":"test/file.rb","type":"string"},"sha":{"example":"23f6827669e43831def8a7ad935069c8bd418261","type":"string"},"size":{"example":12,"type":"integer"},"type":{"example":"tree","type":"string"},"url":{"example":"https://api.github.com/repos/owner-482f3203ecf01f67e9deb18e/BBB_Private_Repo/git/blobs/23f6827669e43831def8a7ad935069c8bd418261","type":"string"}},"type":"object"},"type":"array"},"truncated":{"type":"boolean"},"url":{"format":"uri","type":"string"}},"required":["sha","url","tree","truncated"],"title":"Git Tree","type":"object"}
```

### `403`

Forbidden

### `application/json`

- Type: `object`

Basic Error

```json
{"description":"Basic Error","properties":{"documentation_url":{"type":"string"},"message":{"type":"string"},"status":{"type":"string"},"url":{"type":"string"}},"title":"Basic Error","type":"object"}
```

### `404`

Resource not found

### `application/json`

- Type: `object`

Basic Error

```json
{"description":"Basic Error","properties":{"documentation_url":{"type":"string"},"message":{"type":"string"},"status":{"type":"string"},"url":{"type":"string"}},"title":"Basic Error","type":"object"}
```

### `422`

Validation failed

### `application/json`

- Type: `object`

Validation Error

```json
{"description":"Validation Error","properties":{"documentation_url":{"type":"string"},"errors":{"items":{"properties":{"code":{"type":"string"},"field":{"type":"string"},"index":{"type":"integer"},"message":{"type":"string"},"resource":{"type":"string"},"value":{"oneOf":[{"nullable":true,"type":"string"},{"nullable":true,"type":"integer"},{"items":{"type":"string"},"nullable":true,"type":"array"}]}},"required":["code"],"type":"object"},"type":"array"},"message":{"type":"string"}},"required":["message","documentation_url"],"title":"Validation Error","type":"object"}
```

## Request examples

### cURL

```shell
curl --request POST \
  --url {protocol}://{hostname}/api/v3/repos/{owner}/{repo}/git/trees \
  --header 'content-type: application/json' \
  --data '{
  "base_tree": "9fb037999f264ba9a7fc6274d15fa3ae2ab98312",
  "tree": [
    {
      "mode": "100644",
      "path": "file.rb",
      "sha": "44b4fc6d56897b048c772eb4087f854f46256132",
      "type": "blob"
    }
  ]
}'
```
