Here's how I was able to make it work. I don't mean to indicate that this is the absolute best way to implement this, but I hope it serves as a good example for someone in the future.
from github import Github
import pygit2
# using username and password establish connection to github
g = Github(userName, password)
org = g.get_organization('yourOrgName')
#create the new repository
repo = org.create_repo(projectName, description = projectDescription )
#create some new files in the repo
repo.create_file("/README.md", "init commit", readmeText)
#Clone the newly created repo
repoClone = pygit2.clone_repository(repo.git_url, '/path/to/clone/to')
#put the files in the repository here
#Commit it
repoClone.remotes.set_url("origin", repo.clone_url)
index = repoClone.index
index.add_all()
index.write()
author = pygit2.Signature("your name", "your email")
commiter = pygit2.Signature("your name", "your email")
tree = index.write_tree()
oid = repoClone.create_commit('refs/heads/master', author, commiter, "init commit",tree,[repoClone.head.get_object().hex])
remote = repoClone.remotes["origin"]
credentials = pygit2.UserPass(userName, password)
remote.credentials = credentials
callbacks=pygit2.RemoteCallbacks(credentials=credentials)
remote.push(['refs/heads/master'],callbacks=callbacks)
I spent two days trying to work through the lack of examples to answer this question, so I hope this helps someone in the future.
Answer from Bar Smith on Stack OverflowClone repository
Python way to clone a git repository - Stack Overflow
How i can clone my the git repository using Python and PyGithub/GitPython? - Stack Overflow
python - Cloning a private Github repo using a script - Stack Overflow
Using GitPython will give you a good python interface to Git.
For cloning a new repository you can use clone_from function:
Use with AIfrom git import Repo # pip install gitpython
Repo.clone_from(git_url, repo_dir)
See the GitPython Tutorial for examples on using the Repo object.
Note: GitPython requires git being installed on the system, and accessible via system's PATH.
There is GitPython. Haven’t heard of it before and internally, it relies on having the git executables somewhere; additionally, they might have plenty of bugs. But it could be worth a try.
How to clone:
Use with AIimport git # pip install gitpython
git.Git("/your/directory/to/clone").clone("git://gitorious.org/git-python/mainline.git")
(It’s not nice and I don’t know if it is the supported way to do it, but it worked.)
Just run the git command with subprocess.check_call:
import subprocess
subprocess.check_call(["git", "clone", ...])
There is a library, libgit2, which enables git to be used as a shared library more helpful to your cause is the python binding's pygit.
To answer your question using pygit to clone a repo:
>>> from pygit2 import clone_repository
>>> repo_url = 'git://github.com/libgit2/pygit2.git'
>>> repo_path = '/path/to/create/repository'
>>> repo = clone_repository(repo_url, repo_path) # Clones a non-bare repository
>>> repo = clone_repository(repo_url, repo_path, bare=True) # Clones a bare repository
You can view the repository based docs here
This link gave me the answer: link
I thought I would update my question to let others know what the solution was.
Pretty simple:
from github import Github
# using username and password
g = Github("Username", "Password")
org = g.get_organization('orgName')
repo = org.create_repo("test name")
Below code will help you to create new Repo in an organization:
using username and password establish connection to github:
g = Github(userName, password)
org = g.get_organization('yourOrgName')
If you are using Github Enterprise then use below code to login:
g = Github(base_url="https://your_host_name/api/v3", login_or_token="personal_access_token")
org = g.get_organization('yourOrgName')
create the new repository:
repo = org.create_repo(projectName, description = projectDescription )
full code to create a Repo:
from github import Github
import pygit2
g = Github(userName, password)
org = g.get_organization('yourOrgName')
repo = org.create_repo(projectName, description = projectDescription )
Clone a repo :
repoClone = pygit2.clone_repository(repo.git_url, 'path_where_to_clone')
push code to repo:
repoClone.remotes.set_url("origin", repo.clone_url)
index = repoClone.index
index.add_all()
index.write()
tree = index.write_tree()
oid = repoClone.create_commit('refs/heads/master', author, commiter, "init commit",tree,[repoClone.head.peel().hex])
remote = repoClone.remotes["origin"]
credentials = pygit2.UserPass(userName, password)
#if the above credentials does not work,use the below one
#credentials = pygit2.UserPass("personal_access_token", 'x-oauth-basic')
remote.credentials = credentials
callbacks=pygit2.RemoteCallbacks(credentials=credentials)
remote.push(['refs/heads/master'],callbacks=callbacks)
Full code to clone,create and push to a repo:
from github import Github
import pygit2
g = Github(userName, password)
org = g.get_organization('yourOrgName')
repo = org.create_repo(projectName, description = projectDescription )
repo.create_file("/README.md", "init commit", Readme_file)
repoClone = pygit2.clone_repository(repo.git_url, 'path_where_to_clone')
repoClone.remotes.set_url("origin", repo.clone_url)
index = repoClone.index
index.add_all()
index.write()
tree = index.write_tree()
oid = repoClone.create_commit('refs/heads/master', author, commiter, "init commit",tree,[repoClone.head.peel().hex])
remote = repoClone.remotes["origin"]
credentials = pygit2.UserPass(userName, password)
#if the above credentials does not work,use the below one
#credentials = pygit2.UserPass("personal_access_token", 'x-oauth-basic')
remote.credentials = credentials
callbacks=pygit2.RemoteCallbacks(credentials=credentials)
remote.push(['refs/heads/master'],callbacks=callbacks)