Skip to main content
Enchanted Code

Using Git In Python

programming, python


Git is an essential tool for adding version control to your code, although can be used to add version control to anything else. Python is a easy language for most to learn and simple to use, it can be used to create small scripts to large projects.

Well what happens if you want to combine Git with Python? luckily you can use a package called ‘git-interface’, which provides a way of communicating with the git application.

Disclaimer: I am the creator/maintainer of git-interface

There is also another popular alternative GitPython, however that package currently suffers from memory leaks in long running applications.


  • Python 3.10
  • pip package git-interface 0.5.2
  • git 2.30


Ok let’s first install the pip package as shown in the command below:

python -m pip install git-interface


Now we have the package, we can write a script to clone a repository.

from pathlib import Path

from git_interface.utils import clone_repo

remote_path = ""
dst_path = Path("git-interface")

clone_repo(dst_path, remote_path)

When this is run we will get the repository for the git-interface and it will be cloned into the dst_path, which is currently set to the current working directory.

Now we have a repository cloned we can also get the commit logs.

from git_interface.log import get_logs

logs = get_logs(dst_path, max_number=5)

for log in logs:

This code will get the last 5 commits to happen in the repository cloned earlier. It will then output the subject of each commit. When I run this code I get the following output:

drop python 3.8 and below & use "Generic Alias Type"
incr version & update changelog
improve docstrings & typing hints
fix regex
fix/update readme & badges

Let’s also access one of the files in the repository tree.

from import show_file

readme_content = show_file(dst_path, "main", "").decode()

This will output the contents of the file which is too large to put here, however your output should start with the output shown below:

# Git Interface ...


As you can see this package is quite useful if you wanted to use git from python. It has many more features than what was shown here.

As the creator of git-interface I made it to power my basic-git-web-interface program which is a streamlined alternative to something like Gitlab or Gitea.