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 Overflow
🌐
DEV Community
dev.to › nisevi › script-for-cloning-repositories-4h4m
Python script for cloning repositories - DEV Community
July 28, 2020 - pip install PyGithub · If you don't have pip installed, take a look here for setting it up properly. Finally for running the script you just need to write in the terminal of your choice: python cloner.py · Where cloner.py is the name of the file where we wrote our script. Be aware that all repositories will be cloned in the path where you are executing the script.
Discussions

Clone repository
I couldn't find this in the examples (or I overlooked it), but I can't see if it is possible to clone the repository using PyGithub. More on github.com
🌐 github.com
7
October 27, 2018
Python way to clone a git repository - Stack Overflow
Is there a Python way without using a subprocess to clone a git repository? I'm up for using any sort of modules you recommend. More on stackoverflow.com
🌐 stackoverflow.com
How i can clone my the git repository using Python and PyGithub/GitPython? - Stack Overflow
I would really like to clone,pull,commit and push my repository using python code. Yeah it not easy for beginners but I would really like to do it. Coming around i came with some python libraries for github. PyGithub and GitPython More on stackoverflow.com
🌐 stackoverflow.com
python - Cloning a private Github repo using a script - Stack Overflow
How to clone a private repository from Github using python? I found some good information about git and python, but I started learning python few days back. More on stackoverflow.com
🌐 stackoverflow.com
May 3, 2017
🌐
pythontutorials
pythontutorials.net › blog › create-clone-and-push-to-github-repo-using-pygithub-and-pygit2
How to Create, Clone, and Push GitHub Repositories Using PyGitHub & PyGit2 in Python: Step-by-Step Guide with Examples — pythontutorials.net
For public repos only: Check public_repo. Click Generate token and copy it immediately (you won’t see it again!). Security Note: Store your PAT securely (e.g., in environment variables or a .env file). Never commit it to version control! PyGitHub is a pure Python library, so install it via pip:
🌐
GitHub
github.com › PyGithub › PyGithub › issues › 951
Clone repository · Issue #951 · PyGithub/PyGithub · GitHub
October 27, 2018 - I couldn't find this in the examples (or I overlooked it), but I can't see if it is possible to clone the repository using PyGithub.
Author: PyGithub
🌐
Readthedocs
pygithub.readthedocs.io › en › latest › github_objects › Repository.html
Repository — PyGithub 0.1.dev50+g5aeb43916 documentation
This class represents Repositories · The reference can be found here https://docs.github.com/en/rest/reference/repos
🌐
Readthedocs
pygithub.readthedocs.io › en › latest › github_objects › Clones.html
Clones — PyGithub 0.1.dev50+g9152817c2 documentation
This class represents the total number of clones and breakdown per day or week for a GitHub repository.
🌐
DNMTechs
dnmtechs.com › cloning-private-github-repository-in-organization-using-python-3-and-actions
Cloning Private GitHub Repository in Organization using Python 3 and Actions – DNMTechs – Sharing and Storing Technology Knowledge
This code snippet uses the PyGithub library to authenticate with GitHub using the personal access token. It then gets the organization and repository objects and calls the `clone()` method to clone the repository.
Find elsewhere
🌐
PyPI
pypi.org › project › github-clone
github-clone · PyPI
July 4, 2021 - Clone any subdirectory of a GitHub repo with just the GitHub URL
🌐
GitHub
docs.github.com › articles › cloning-a-repository
Cloning a repository - GitHub Docs
When you create a repository on GitHub, it exists as a remote repository. You can clone your repository to create a local copy on your computer and sync between the two locations.
🌐
Readthedocs
pygithub.readthedocs.io › en › stable › github_objects › Repository.html
Repository — PyGithub 2.9.1 documentation - Read the Docs
This class represents Repositories · The reference can be found here https://docs.github.com/en/rest/reference/repos
🌐
Jan-Piet Mens
jpmens.net › 2019 › 04 › 04 › i-clone-all-repositories-i-ve-starred
Jan-Piet Mens :: I clone all GitHub repositories I've starred
April 4, 2019 - #!/usr/bin/env python3 -B from github import Github, Auth # https://github.com/PyGithub/PyGithub import json import os token = open(os.path.expanduser("~/.gist")).read().rstrip() auth = Auth.Token(token) g = Github(auth=auth) starred = [] for repo in g.get_user().get_starred(): # I find stars some of my own repositories, and I don't think # I actually did that; artefact of prior GitHub practices? if repo.owner.login != "jpmens": data = { "name" : repo.name, "owner" : repo.owner.login, "full_name" : repo.full_name, "clone_url" : repo.clone_url, } if repo.owner.name: data["owner_name"] = repo.ow
🌐
Martin Heinz
martinheinz.dev › blog › 25
All the Things You Can Do With GitHub API and Python | Martin Heinz | Personal Website & Blog
June 15, 2020 - Both snippets above use the same API endpoint to retrieve all open issues for specified repository. In both cases we start by taking GitHub token from environment variable. Next, in the example with using PyGitHub we use the token to create instance of GitHub class, which is then used to get repository and query its issues in open state.
🌐
Bitcoden
bitcoden.com › answers › create-clone-and-push-to-github-repo-using-pygithub-and-pygit2
Create, Clone, and Push to GitHub repo using PyGitHub and PyGit2
from github import Github import ... 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", ...
🌐
Kevin Sookocheff
sookocheff.com › posts › downloading directories of code from github using the github api
Downloading directories of code from Github using the Github API | Kevin Sookocheff
June 17, 2014 - To make it easier to share this code I’ve developed a small package manager that downloads code within a directory from Github to be copied in to your current project. It’s a quick and dirty alternative to cloning an entire repository, grabbing the set of files you want and placing them in your project. We’ll use the PyGithub Python library to interact with the Github API.
🌐
Medium
devopslearning.medium.com › day-13-101-days-of-devops-github-api-using-python-and-pygithub-module-c1bcbaaeada7
Day 13–101 Days of DevOps — GitHub API using Python and PyGithub Module | by Prashant Lakhera | Medium
July 16, 2021 - PyGithub is a Python library to use the Github API v3. With it, you can manage your Github resources (repositories, user profiles, organizations, etc.) from Python scripts.
Top answer
1 of 2
7

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")
2 of 2
0

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)