Search code examples
c#gitgithubgithub-apigit-commit

Edit multiple files in single commit with GitHub API


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.


Solution

  • 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