Multiple file upload via Github REST API

I use GrahamCampbell/Laravel-GitHub (which is actually a Laravel-wrapper for KnpLabs/php-github-api) to commit files from my application to Github via REST API.

With $client->api('repo')->contents()->create(...) and $client->api('repo')->contents()->update(...) I can modify individual files in my Github repo.

Now I would like to upload a whole directory (including files and subdirectories) to my repo, in one commit. I have tried the following sequence:

  • Create a tree with $client->api('gitData')->trees().
  • Commit the tree with $client->api('gitData')->commits().
  • Update heads/main with $client->api('gitData')->references().

But this only works as long as I upload my directory to the repo root directory. As soon as I try to commit to a Github subdirectory, everything in the root directory is overwritten. I’ve been experimenting for a while now, consulting google and perplexity, but I’m not getting anywhere.

Specifically, I would like to achieve the following two things:

  • Commit a directory structure to an (arbitrary) Github subdirectory
    • retaining the old files/directories in this directory structure
    • discarding the old files/directories only in the affected directory structure (i.e. all other Github directories should be retained)

This is basically similar to emptying folders and inserting content in the local file explorer, or inserting content and overwriting existing files.

Perhaps there is already a ready-made solution or PHP class for this, I would be very grateful for a reference. Otherwise, it would help me if someone could list the correct procedure for this:

  • Do I have to read and commit all existing trees with every commit (even to a deeper Github subdirectory)? I actually want the commit not to touch all existing files, but only the new or changed ones.
  • Which tree must my base_tree-SHA point to if I want to commit to my/sub/dir?

Many thanks in advance for your help

John Nazarov