Create Git Repositories in Terminal

Dilain Gunasekara
3 min readApr 21, 2021

--

creating git repository from the terminal using python

As developers we use git and Github to version control our projects. When using git we do most of our work inside the terminal such as commits, pulls etc. But in order to create our initial github repository we need to visit github.com.

But using Github API we can create git repositories from our terminal with some help from python.

First a token needs to be created to access the Github API.

In your github profile go to Settings->Developer Settings -> Personal access token-> Generate new token

Then we need to select repo permission to this token.

Then click on generate token button.

Please make sure to store this token because it won’t be visible after the initial creation.

Now let’s code the python script to create github repositories from the terminal.

create a python script named create_git.py, you can rename this file name as you desire.

In line 7 name of the repository is taken as an argument. In line 8 using the private flag we can create the repository private.

In line 14 you need to put your Github API token.

In line 25 a POST request is sent to create the repository. Then from code line 27 and below, it clone the repository and make a readme file and do the initial commit and push it to Github.

Now we can run this script

If the -p flag is present it will create a private repository.

Now let’s make this script run from anywhere.

This will give script permission to execute.

Now copy the path to your script

Now open your bashrc or zshrc file of your shell.

Then add the path to the bottom of the file like below.

Now save the file and reopen the terminal.

Now you can execute the script from anywhere and no need to add python3 to beginning of the command.

You can get the code from my repository.

--

--

No responses yet