I’m trying to write a script for which I want to copy a git repository, but without the .git
folder. Here is my entire code:
import shutil
shutil.copytree([src], [dst], ignore=shutil.ignore_patterns('**/.git/**'), dirs_exist_ok=True)
However, this is still copying the .git
directory. I have tested the given glob with the Python glob
package, so I know it works. I understand that it’s possible to write my own function to pass in the ignore
param, however I still don’t understand why my code is not working. The documentation says specifically that ignore_patterns
accepts glob-style patterns, so this should work based on my understanding.
2
The arguments passed to the ignore
callback function are the directory name and list of files. Since you want to ignore the entire directory, you can provide your own function that just checks if the directory is .git
, rather than using a glob pattern.
def is_git_dir(dir, files):
return ".git" in dir.split('/')
shutil.copytree([src], [dst], ignore=is_git_dir, dirs_exist_ok=True)