Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing __pycache__ from git repository

How can I remove all __pycache__ subdirectories in my repository using .gitignore?

like image 812
JL710 Avatar asked Jul 12 '26 13:07

JL710


1 Answers

You cannot remove files from existing commits: those commits are frozen for all time. You can make sure you do not add new files to future commits, though. Simply remove the files now, with git rm -r --cached __pycache__, and list __pycache__ or __pycache__/ in your .gitignore (creating this .gitignore file if needed). Do this for each __pycache__ directory; use your OS's facilities to find these (e.g., find . -name __pycache__ -type d). Then git add .gitignore and git commit to commit the removal.

Note that any time anyone moves from any commit that has the files—where they'll be checked out—to a commit that lacks the files, they will have their entire __pycache__ directory removed if Git is able to do that; at the least, any cached files that were committed and can be removed will be removed. So the --cached in the git rm -r --cached above only speeds things up for you by avoiding the removal of the cached compiled files this time. Others will have to rebuild their cache.

To make a new and different repository in which the __pycache__ files were ever accidentally committed in the first place, use git filter-branch (which is now deprecated) or the newfangled git filter-repo (which is not yet distributed with Git). Or, see any of these various existing questions and their answers, which you should already have found before you asked this:

  • Remove a file from a Git repository without deleting it from the local filesystem
  • Applying .gitignore to committed files
  • How can I make Git "forget" about a file that was tracked, but is now in .gitignore?
  • Remove sensitive files and their commits from Git history
like image 176
torek Avatar answered Jul 15 '26 06:07

torek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!