0

I want to create a new empty repository and add two files to it. However, I am stuck at adding two files with a single commit. This is my current code:

filename1="dummy1"
filename2="dummy2"
file1_content="something"
file2_content="something"
repo_name="dummy_repo"

repo = g.get_user().create_repo(repo_name)
repo.create_file(filename1, commit_message, file_content1)

The repo.create_file() takes only one file as an argument as per the docs, how do I add the second file with this commit itself?

Raj Tiwari
  • 13
  • 5

2 Answers2

1

Actually it is possible by combining the following PyGithub methods:

  • create_git_blob
  • get_git_tree
  • create_git_tree
  • create_git_commit
  • get_git_ref
  • GitRef.edit

A complete example is a bit long but can be found in this gist.

Ohav
  • 371
  • 2
  • 13
  • 1
    I tested today and can confirm this is the working solution. In addition, if you are looking to delete some files with git trees, set `sha=None` and it will be shown as `deleted` in the commit. Thanks @Ohav – Oğuzhan Akan May 03 '23 at 22:08
0

From my research, I don't believe it is possible to add multiple files at once to the same commit using pygithub. You would have to do this locally on a cloned copy and then push the commit to github, but I don't think that's the purpose of PyGithub.

It looks like this is a limitation of the Github API itself.

HPringles
  • 1,042
  • 6
  • 15