Automate GitHub repository creation and initialization using Python

Lavanya Gowda
3 min readMar 13, 2021

In this article, we will see how we can automate GitHub repository creation using python in a simple way which helps us to focus on more productive things by saving our time.

Every time when we want to work on a new project, we will create a new repository in the GitHub and we will clone it into our local machine and then our actual process will start.

We will be following the below points in this automation process when we type create <project name> in the terminal

  • [x] Navigate to specific directory in your machine where you want to create new project directory
  • [x] Create new project directory with the given name in current directory
  • [x] Navigate into project directory
  • [x] Using PyGithub module, create a new repository in the GitHub by the given project name
  • [x] Start the git process using the following commands
git init
touch README.md
git add .
git commit -m 'Initial Commit'
git remote add origin https://github.com/<github-username>/$1.git
git push -u origin master

Step 1:- Create a custom terminal command using Shell:

We will create a new bash script to keep our custom command

Create a hidden file by appending (.) in front of the filename as it helps to prevent important data from being accidentally deleted.

Go to home directory in terminal, cd ~ will take you there. Now, create a new bash script by using the following command:
touch .my_custom_commands.sh

Now open the .my_custom_commands.sh file in any editor [Sublime Text Editor: subl ~/.my_custom_commands.sh] and paste the below code

#!/bin/bash

function create() {
cd
python <your-python-script-filepath> $1
cd <directory-path-where-project-folder-to-be-created>/$1
git init
touch README.md
git add .
git commit -m 'Initial Commit'
git remote add origin https://github.com/<github-username>/$1.git
git push -u origin master
}

By default, a newly created file has only read permission. If you try to execute this file [command to execute:./.my_custom_command.sh], you will see “permission denied” message. So give permission to the file using following command

chmod +x .my_custom_commands.sh

We can execute the above file by using function name also, but we need to set some other permissions before that, now restart the terminal and go to home directory then run the following command.

source ~/.my_custom_commands.sh

Now you can execute the file using function name, in our case create $1 , Here $1 is an argument where you can pass the project name. But you have to run the above command on every terminal restart, so just go to ~/.bashrc or ~/.zshrc file and set the above command after the last line or anywhere you want. Now by default it will work on every terminal sessions.

Step 2:- Writing a python script:

Create a new python script file and install PyGithub and python-dotenv packages by running following commands in terminal:

pip install PyGithub python-dotenv

and now paste the following code in the new python file:

import sys
import os
from dotenv import load_dotenv
from github import Github
load_dotenv()path = os.getenv('FOLDERPATH')
access_token = os.getenv('GITHUB_ACCESSTOKEN')
def create_project():
folder_name = str(sys.argv[1])
newpath = os.path.join(path,folder_name)
if not os.path.exists(newpath):
os.makedirs(newpath)
user = Github(access_token).get_user()
repo = user.create_repo(name=folder_name, private=True)
print(f"Succesfully created repository {folder_name}")
if __name__ == '__main__':
create_project()

Create a .env file and paste the following code:

FOLDERPATH="your-folder-path"
GITHUB_ACCESSTOKEN="your-github-accesstoken"
GITHUB_USERNAME="your-github-username"

How to generate GitHub access token?

Login to GitHub then follow the below points

Go to settings->Developer settings->Personal access tokens
Then generate one access token with desired scopes
Then copy the access token before changing the tab

And that’s it, now you can run create <project-name> in the terminal and it should do the work for you. you can click here to get the source code.

--

--