create_file: Create a file in a new commit

Description Usage Arguments Details Value Examples

Description

This function adds a file in a repository in GitHub by creating a new commit on the specified branch. If the branch does not already exist a parent commit must be specified and a new branch is created from it. If the file already exists create_file() throws an error.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
create_file(
  content,
  path,
  branch,
  message,
  repo,
  parent,
  author,
  committer,
  ...
)

Arguments

content

(string) The content of the file specified as a single string.

path

(string) The path to create the file at, within the repository.

branch

(string) The name of the branch to make the new commit on.

message

(string) The commit message.

repo

(string) The repository specified in the format: owner/repo.

parent

(string, optional) If creating a new branch the the parent commit must be specified as either a SHA, branch or tag.

author

(list, optional) A the name and email address of the user who wrote the changes in the commit.

committer

(list, optional) A the name and email address of the user who created the commit.

...

Parameters passed to gh_request().

Details

The author and committer arguments are optional and if not supplied the current authenticated user is used. However, if you want to set them explicitly you must specify a named list with name and email as the elements (see examples).

Note: The GitHub API imposes a file size limit of 1MB for this request. For larger files use the upload_files() function.

For more details see the GitHub API documentation:

Value

create_file()returns a list of the commit properties.

Commit Properties:

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
## Not run: 

  # Create a new file on the main branch
  create_file(
    content = "# This is a new file\\n\\n Created by `create_file()`",
    path    = "new-file.md",
    branch  = "main",
    message = "Created a new file with create_file()",
    repo    = "ChadGoymer/githapi"
  )

  # Create a new file on a new branch
  create_file(
    content = "# This is a new file\\n\\n Created by `create_file()`",
    path    = "new-file.md",
    branch  = "new-branch",
    message = "Created a new file with create_file()",
    repo    = "ChadGoymer/githapi",
    parent  = "main"
  )

  # Create a new file on the main branch specifying an author and committer
  create_file(
    content   = "# This is a new file\\n\\n Created by `create_file()`",
    path      = "new-file.md",
    branch    = "main",
    message   = "Created a new file with create_file()",
    repo      = "ChadGoymer/githapi",
    author    = list(name = "Bob",  email = "bob@acme.com"),
    committer = list(name = "Jane", email = "jane@acme.com")
  )


## End(Not run)

ChadGoymer/githapi documentation built on Oct. 22, 2021, 10:56 a.m.