How to exclude and remove .pyc files from your git repo
If you’re working with a Git repository, you probably don’t want .pyc
files (or any other kind of compiled files) to be included in your repository. These files are generally specific to your system and aren’t useful to others. Moreover, including them can clutter your repository and make it larger and harder to manage.
The standard way to exclude certain files or file types in Git is by using a .gitignore
file.
How to exclude .pyc files using .gitignore
-
In the root directory of your repository, create a file named
.gitignore
if it doesn’t already exist. -
Open the
.gitignore
file in a text editor. -
Add the following line to the file:
*.pyc
-
Save and close the file.
-
Commit the
.gitignore
file to your repository:git add .gitignore git commit -m "Add .gitignore file"
Now, any .pyc
files will be ignored by Git. They won’t show up when you run git status
, and they won’t be included when you run git add .
.
Exclude __pycache__
from git repo
__pycache__
is another place where pyc files are stored depending on how python is configured.
It also a best practice to ignore all files in the __pycache__
directories that Python creates, you can add another line to your .gitignore
file:
*.pyc
__pycache__/
This tells Git to ignore the entire __pycache__
directory, regardless of where it appears in your repository.
Remember, it’s best to set up your .gitignore
file when you first create your repository.
How to remove existing .pyc files
If you’ve already committed .pyc
files (or any other files you want to ignore), you’ll need to remove them from your repository before Git will start ignoring them. You can do this with the git rm
command, like so:
git rm --cached *.pyc
I hope this article helped you better understand how to exclude and remove .pyc files from your project git repository.
We ❤️ Python programming! Check out more articles in our Python Basics series.