How can I create a new repository with PyGithub on Github?
In particular I like to know how to use the create_repo
method? How do I generate a AuthenticatedUser
?

- 5,802
- 16
- 47
- 74
-
Are you trying to create a new git repository locally? Or a new repository on github? `PyGithub` does the latter, while something like `pygit` does the former. Both seem to be well documented online. – larsks Feb 23 '15 at 13:59
-
I am trying to create a repository on Github. – ustroetz Feb 23 '15 at 14:13
-
@larsks Could you please give an example on how to create with PyGithub a repository on GitHub? – ustroetz Feb 23 '15 at 18:59
5 Answers
The solution to my question is the following
g = Github(token)
user = g.get_user()
repo = user.create_repo(full_name)

- 5,802
- 16
- 47
- 74
I stumbled across this question trying to figure out how to coax PyGithub into creating a Repository within an Organization and thought it would be relevant here.
g = Github(token)
organization = g.get_organization("org-name")
organization.create_repo(
name,
allow_rebase_merge=True,
auto_init=False,
description=description,
has_issues=True,
has_projects=False,
has_wiki=False,
private=True,
)
The full set of keyword arguments may be found here: https://developer.github.com/v3/repos/#input

- 351
- 2
- 7
I stumbled onto this question when trying to figure out how to create an AuthenticatedUser object. Turns out you get a NamedUser when you pass any argument to get_user, and if you give it no arguments, you get the AuthenticatedUser corresponding to the creds you used when creating the Github object.
As a minimal example, the following:
from github import Github
g = Github("my GitHub API token")
user = g.get_user('myname')
print user
authed = g.get_user()
print authed
yields
<github.NamedUser.NamedUser object at 0x7f95d5eeed10>
<github.AuthenticatedUser.AuthenticatedUser object at 0x7f95d5684410>
Once you have an AuthenticatedUser object, you can call CreateRepo as explained in the docs that you linked.

- 131
- 2
- 5
-
This is a better answer because it explains what the different calls return, this should be the accepted answer @ustroetz – Daniel Apr 11 '17 at 10:49
-
For the create_repo() command, do you call it on the user object? Or do you create an org? – ScottyBlades May 17 '20 at 00:05
-
1`g = Github(username, password) gituser = g.get_user(username) repo = gituser.create_repo(name)` is definitely not working for me. – ScottyBlades May 17 '20 at 00:08
To create a repository, you can use GitPython. Here is a tutorial on how to init a rep. It's as simple as:
import git
repo_dir = os.path.join(rw_dir, 'my-new-repo')
file_name = os.path.join(repo_dir, 'new-file')
r = git.Repo.init(repo_dir)
You can also use Dulwich to create a repository:
from dulwich.repo import Repo
x = Repo.init("/path/to/new/repo")
Once you have that done, you can use PyGithub to access the repositories (or stick to the APIs provided above):
from github import Github
g = Github("user", "password")
for repo in g.get_user().get_repos():
print repo.name

- 10,359
- 7
- 47
- 57
-
Thanks! So if I understand you right, I first have to create a repository locally and push it then to Github? There is no way to generate a repository in Github directly with PyGithub? – ustroetz Feb 23 '15 at 14:16
-
2There is a way using [PyGithub.Blocking.Organization.Organization.create_repo](http://jacquev6.net/PyGithub/v1/github_objects/Organization.html#github.Organization.Organization.create_repo) which calls [POST /orgs/:org/repos](https://developer.github.com/v3/repos/#create). I omitted that because there's no tutorials I could link for you. – runDOSrun Feb 23 '15 at 14:22
Answer to the question:
login via token:
g = Github(token)
user = g.get_user()
repo = user.create_repo(repo_name)
print(repo)#To
login via username and password:
g = Github("user", "password")
user = g.get_user()
repo = user.create_repo(repo_name)
print(repo)
Github Enterprise with the custom hostname.
login to Enterprise GitHub which has organizations
g = Github(base_url="https://{hostname}/api/v3", login_or_token="token")
org = g.get_organization("org name")
repo = org.create_repo(repo_name)

- 10,359
- 7
- 47
- 57

- 171
- 1
- 12