How to remove git repository, in python, on windows

Question:

As the title describes, I need to remove a git repository by using python.
I’ve seen other questions about this very same topic, but none of the solutions seem to work for me.

My work:
I need to download a repository, using gitpython, followed by checking some irrelevant things. After the process is finished, I need to delete the repository to save space for whoever is using my script.

The problem:
When cloning a git repository, a .git file will be created. This file is hidden in windows, and the modules I’ve been using do not have permission to delete any of the files within the .git folder.

What I’ve tried:

import shutil
shutil.rmtree('./cloned_repo')

PermissionError: [WinError 5] Access is denied:

Any help regarding this issue would be deeply appreciated.

Asked By: Kuratorn

||

Answers:

Git has some readonly files. You need to change the permissions first:

import subprocess
import shutil
import os
import stat
from os import path
for root, dirs, files in os.walk("./cloned_repo"):  
    for dir in dirs:
        os.chmod(path.join(root, dir), stat.S_IRWXU)
    for file in files:
        os.chmod(path.join(root, file), stat.S_IRWXU)
shutil.rmtree('./cloned_repo')
Answered By: Anonymous

As noted above, this is because many of Git’s files are read-only, which Python does not delete gracefully on Windows. The GitPython module has a utility function for just this purpose: git.util.rmtree

Calling the function like so should resolve your problem:

from git import rmtree
rmtree('./cloned_repo')

You can also see their source here–it’s similar to the answer above, as of Dec 2020.

Answered By: Conor Hayes

I’ve tried all the suggestions above, but none of them worked.

I’m using the GitPython module to handle my git repo’s and I’m not convinced that I’m handling any open connections correctly. Saying that I have found a way to kill all git sessions which "fixed" my issue. (Albeit it a dirty fix)

import os
folder = "path/to/root"
git_repo = git.Repo(folder)
# do something with gitrepo

os.system(f'taskkill /IM "git.exe" /F')

This is not a good solution as you might have other git session open that you don’t want to close. One way will be to make sure you close the git session, by closing it.

git_repo = git.Repo(folder)
#do something with gitrepo
git_repo.close()

Although the better way will be to use a context manager like ‘with’

import git
with git.Repo(folder) as git_repo:
   # do something with gitrepo

The context manager makes it easier to manage the git repo, and less likely have open repo’s in session, thus less likely to have read only access errors.

Answered By: Martin Cronje

If you use tempfile.TemporaryDirectory in Python 3.8+, this will just work. The specific issue with Git and Windows was considered a bug in Python and addressed in 3.8.

For <=3.7, you’ll need to refer to one of the other answers, or to some workarounds linked in the Python issue.

Answered By: polm23
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.