how can i create a temporary git repo for testing?

Just create temporary directory using tempfile standard library:

Change working directory to the new temp directory:

Then either use os.system("git init && touch file && git add file && git commit -m Test") or use git python library:

Cleanup by deleting the temp directory:

Easiest way to rm -rf in Python

E.g.: Create test repo like this:

import os
import tempfile

def test_repo():
    """Creates test repository with single commit and return its path."""
    temporary_dir = tempfile.mkdtemp()

    os.system("git init")
    os.system("touch file1")
    os.system("git add file1")
    os.system("git commit -m Test")

    return temporary_dir


CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top