5

I'm using the GitHub API in a C# webapp (with Blazor). I want to be able to create a single commit to add, delete, and edit multiple files in a folder in a repository. I know I can send a PUT request to the URL https://api.github.com/repos/[username]/[repository]/contents/[file] with these contents to create a file (and I can also edit a file by adding an SHA hash):

{
    "message": "[Commit message]",
    "content": "[Content encoded in base64]",
    "committer": {
        "name": "[Committer name]",
        "email": "[Committer email]"
    }
}

But this creates one commit for every file change. Is there any way that I can do multiple operations in a single commit (either using the GitHub API or something else)? I would use something like libgit2sharp but I don't want to be cloning the repository to a folder on the filesystem.

Merlin04
  • 1,837
  • 4
  • 23
  • 27

2 Answers2

10

Is there any way that I can do multiple operations in a single commit (either using the GitHub API or something else)?

There is the underlying Git Data API that can be used to build up a commit from scratch:

  • files are uploaded as blobs using the API
  • trees are used to indicate what the repository state should be (update paths to point to the new blobs)
  • then create a new commit using the new root tree and additional metadata
  • if you can, then update the reference (i.e. the branch) to point to this new commit
Brendan Forster
  • 2,548
  • 1
  • 24
  • 32
  • Hi Brendan, does this require (especially the tree step) the repository to be cloned, doesn't? – elect May 11 '21 at 08:41
-3

You can use the Git Flow with the GitHub API:

  1. Create a new branch by creating a ref (e.g. refs/heads/myNewBranch), using the SHA of whichever base branch you want.
  2. Make content changes in that branch (create/update or delete files). Be sure to set the branch parameter to your new branch name.
  3. Create a pull request using your new branch as the head.
  4. Merge the pull request with a merge_method of "squash". This will squash all the commits from individual content change API calls into 1 commit with your commit message of choice.
jsmartt
  • 1,404
  • 1
  • 15
  • 22