Question

Using the GitHub API, I can create a tree containing all the modified files for a commit, specify a base_tree that will be updated with this tree of changes and then commit it.

But, as the documentation says, the new tree can contain only paths for files with the modes

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.

It doesn't say what should I do if I want to mark some path as deleted, as with git rm <path>.

Was it helpful?

Solution 2

@ivan-zuzak 's answer is obsolete now that the GitHub API supports deleting files by passing null sha's.

To delete a file you would create a tree in Javascript with octokit like so:

await octokit.request('POST /repos/{owner}/{repo}/git/trees', {
  owner: 'octocat',
  repo: 'hello-world',
  tree: [
    {
      path: 'path/to/file-that-will-be-deleted.py',
      mode: '100644',
      type: 'blob',
      sha: null,
    }
  ],
  base_tree: 'sha'
})

The base_tree should contain the SHA1 of an existing Git tree object you want to delete the file from.

After that everything Ivan said still holds true, so you should construct a new commit that links to this new tree, and bump the ref to point to this new commit.

OTHER TIPS

Currently, the only way to remove an entry from the tree (either a blob or another tree) is to construct a new tree and not list that entry. After you have that tree, construct a new commit that links to this new tree, and don't forget to bump the ref to point to this new commit.

The most clean way to do this I've found is to create a new branch, create, delete and update files in it using the contents API, then create a merge commit with the master branch and finally update the master branch to this last commit (in the end you should also delete the temporary side-branch).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top