Back

Explore Courses Blog Tutorials Interview Questions
+2 votes
2 views
in DevOps and Agile by (19.4k points)
How can I ignore .pyc files in git?

If I put it in .gitignore it doesn't work. I need them to be untracked and not checked for commits.

1 Answer

+1 vote
by (27.5k points)

Add the following line to the .gitignore file in the root folder of your git repository tree right after repository initialization.:

*.pyc 

Also, in case you forgot to do it beforehand, and you just add the line to the .gitignore file, all previously committed .pyc files will still be tracked, so you'll need to remove them from the repository.

Say, you are on a Linux system (or "parents&sons" like a MacOSX), you can quickly do it with just this one line command that you need to execute from the root of the repository:

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

It means starting from the directory I am currently in, find all files whose name ends with extension .pyc, and pass file name to the command git rm -f

After *.pyc files deletion from git as tracked files, commit this change to the repository, and then you can finally add the *.pyc line to the .gitignore file.

Browse Categories

...