git remove .pyc file from repository

How to Remove .pyc file from Git Repository

When you run a python website or application, you will find that the python interpreter creates many .pyc files in your code folders. These files are the compiled python code, used for faster execution. If your code is managed by git, you will also find that git tracks these files in your repository. However, it is unnecessary to track .pyc files on a git repository since they are compiled files that are not human readable, and you already have .py scripts for them. In this article, we will learn how to remove .pyc file from git repository.


How to Remove .pyc file from Git Repository

Here are the steps to remove .pyc files from git repository.


1. Add .pyc to .gitignore file

.gitignore (note the dot at the beginning of filename) is a file placed at the root folder of your git repo, which contains a list of file extensions, file paths and folder paths that are to be excluded from the git repository. Git will not track any file or folder mentioned in this file. Go to the root folder of your git repository and run the following command to create a .gitignore fil.

$ vi .gitignore

Add the following line to it, to exclude .pyc files from git repository.

*.pyc

Save and close the file. From now on, git will not include .pyc files in your repo. If you do this step immediately after the initialization of git repository then git will not track .py files from the subsequent commits.

However, if you have already have a few commits beforehand, which track .pyc files then you will need to remove them from repository. For this purpose, perform the next step.


2. Remove .pyc file

Run the following command to find .pyc files in your repository and execute ‘git rm -f’ command on them. ‘git rm -f’ command is used to stop tracking a specific file from your repository as well as delete from your filesystem.

$ find . -name "*.pyc" -exec git rm -f "{}" \;

If you want to remove the files from git repository but not delete them, then use –cached option in the above command.

$ find . -name "*.pyc" -exec git rm -f --cached "{}" \;


3. Commit Changes

If you have removed .pyc file from your repository, commit the changes.

$ git commit -am "type commit message here"

That’s it. In this article, we have learnt how to remove .pyc files from git repository. You can use the same steps to remove any other kind of files (images, documents, pdf, etc.) as per your requirement. Basically you need to add the file name or extension to .gitignore file at repository root folder, and run ‘git rm’ command to remove them from your repository & disk.

Also read:

How to Reset Local Git Repository to Remote
Cannot Access NGINX from Outside
How to Undo Git Rebase
How to Stop Tracking Folder in Git Without Deleting
Bash Loop Through Files in Directory & Subdirectories

Leave a Reply

Your email address will not be published. Required fields are marked *