Contents

How to exclude and remove .pyc files from your git repo

0

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

  1. In the root directory of your repository, create a file named .gitignore if it doesn’t already exist.

  2. Open the .gitignore file in a text editor.

  3. Add the following line to the file:

    *.pyc
    
  4. Save and close the file.

  5. 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.

Related Packages & Articles

Understanding Python's Common Built-in Data Types

Python’s built-in data types stand as the fundamental building blocks of data manipulation. In this blog post, we’re going to dive into some of the most commonly used built-in data types in Python.

PyScaffold 4.6

The pyscaffold Python package is a project generator designed to bootstrap high-quality Python packages that are ready to be shared on PyPI and installable via pip. It encourages the adoption of the best tools and practices of the Python ecosystem, aiming to enhance productivity. After installation, it provides a putup command to generate a project template with necessary configurations and setups. The generated project is an already initialized git repository, and it uses setuptools for packaging, Sphinx for documentation, and pytest for running automated tests. The package also supports versioning and git integration, making it a comprehensive tool for Python project setup.

commitizen 3.29.1

The commitizen Python package is a release management tool designed to streamline and standardize the process of committing changes in a team setting. It assumes that your team uses a standard way of committing rules and based on that, it can automatically bump your project's version, create a changelog, and update files. By default, commitizen uses conventional commits, but it allows for customization of rules. The package provides a command-line utility to create commits with your rules, and it can display information about your commit rules.