9

I want to know which method should I call (and on which object) and how to call that method (required parameters and their meanings).

Asclepius
  • 57,944
  • 17
  • 167
  • 143
Abhishek Kumar
  • 298
  • 4
  • 10
  • Two places to start: https://developer.github.com/v3/repos/contents/ and https://github.com/PyGithub/PyGithub/blob/master/github/InputFileContent.py – Emil Vikström Nov 16 '16 at 11:41

2 Answers2

14
import github

g = github.Github(token)
# or  g = github.Github(login, password)

repo = g.get_user().get_repo("repo_name")
file = repo.get_file_contents("/your_file.txt")

# update
repo.update_file("/your_file.txt", "your_commit_message", "your_new_file_content", file.sha)

If you are using token then you should have at least repo scope of your token to do it. https://developer.github.com/v3/oauth/#scopes

See: https://developer.github.com/v3/repos/contents/ and https://github.com/PyGithub/PyGithub

Sergey Luchko
  • 2,996
  • 3
  • 31
  • 51
6

As of 2021, the PyGithub API has changed and there's an example of how to do this: https://pygithub.readthedocs.io/en/latest/examples/Repository.html#update-a-file-in-the-repository

repo = g.get_repo("PyGithub/PyGithub")
contents = repo.get_contents("test.txt", ref="test")
repo.update_file(contents.path, "more tests", "more tests", contents.sha, branch="test")
# {'commit': Commit(sha="b06e05400afd6baee13fff74e38553d135dca7dc"), 'content': ContentFile(path="test.txt")}

For .update_file, the first string is the message, the second string is the new contents of the file. Here's the API description:

update_file(path, message, content, sha, branch=NotSet, committer=NotSet, author=NotSet)
Mixchange
  • 893
  • 1
  • 8
  • 14