Upload Files to GitHub with Python in less than 2 lines of code

Learn to use the module GitHup to upload your files automatically on GitHub as fast as possible

Cyril Frebel
2 min readJul 31, 2023
Photo by Merlin Lightpainting on Pexels

The first and obvious thing is to download the module on PyPi which you can do on the official page of the project or simply by doing a simple pip install like this:

pip install githup

Download a Repository

import githup as gp

gp.download_repo("https://github.com/cyrilfrl/githup")

By default the path in which the repository will be saved is the current directory of the program file but if you wish to save your file in another directory you can do as follow by adding a single parameter (and keep in mind that “https://github.com/cyrilfrl/githup” is just an example URL, you can use whatever URL satisfies you.

import githup as gp

gp.download_repo("https://github.com/cyrilfrl/githup", "folder_path")

Where folder_path is the variable containing the path to your folder

Initiate a Repository

import githup as gp

gp.init_repo(github_repository_url, folder_path="local")

If you want to use a README.md file when initiating, feel free to add the parameter add_readme=”readme” like this:

import githup as gp

gp.init_repo(github_repository_url, add_readme="readme")

Once again if you want to initiate your repository in a specific file you can by adding the parameter folder_path and setting it to your chosen path.

Make a Commit

import githup as gp

gp.commit_files(branch_name, commit_message, folder_path="local")
gp.download_repo("https://github.com/cyrilfrl/githup")

For more clarity, I did not give any value to the parameters of the function because these are clearer than using any variable or name to explain the utility, if you still don’t understand what each of them does:

branch_name => the name of the branch on which you want to commit

commit_message => the message you want to include when pushing your repo

folder_path => once again, the default value is set to local which will save commit the current folder in which your Python program is located to GitHub.

Thank you for reading this until the end, i really appreciated coding this module for one of my projects and then being able to share it

--

--

Cyril Frebel

Passionate programmer dedicated to demystifying complex technological concepts for all to understand