About this question
How can I ignore gitignore.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.
How can I ignore gitignore.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.
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask DevOps Expert
Answered on Jun 15, 2021
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. This way you can ignore gitignore.pyc files in git?
Ranjana Admin JanBask Expert Latest answer
Answered on Jul 19, 2024
To ignore .pyc files in your Git repository, you'll need to update your .gitignore file. .pyc files are compiled Python files that are generated automatically when Python code is executed. These files are not necessary to include in version control, as they can be regenerated from the source code.
Here’s how to set up your .gitignore file to ignore .pyc files:
Create or Edit .gitignore:
If you don’t already have a .gitignore file in your repository, create one in the root directory of your project. If you already have one, open it for editing.
Add .pyc Files to .gitignore:
Add the following line to the .gitignore file to ignore all .pyc files:
plaintext
*.pycThis line tells Git to ignore all files with the .pyc extension in the repository.
Ignore Compiled Python Files and Directories:
To ensure all compiled Python files and directories are ignored, you can add these additional lines:
plaintext
# Ignore Python bytecode files*.pyc*.pyo*.pyd# Ignore __pycache__ directories__pycache__/Remove Already Tracked .pyc Files:If .pyc files were already tracked by Git before you updated the .gitignore file, you need to remove them from the repository:bashCopy codegit rm --cached *.pycgit rm --cached -r __pycache__The --cached option removes the files from the Git index but keeps them in your working directory. After running these commands, commit the changes:
bashCopy codegit commit -m "Remove .pyc files from repository and update .gitignore"Verify .gitignore:To ensure that your .gitignore is working correctly, you can use the following command to check which files are ignored:
git status --ignoredThis will show you the files that are being ignored by Git.
By following these steps, you’ll prevent .pyc files from being tracked by Git, keeping your repository clean and focused on your source code.
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Most-read guides across JanBask
Common DevOps interview questions, answered
Guides, tips and career advice on DevOps from JanBask experts.
DevOps How to Become a DevOps Engineers: Learning Path, Critical skills & More
Looking for answer to how to become a DevOps engineer in 2025? Here is the detailed article about DevOps career path, DevOps…
DevOps Top 50+ Jenkins Interview Questions And Answers For 2025
Here are the Top 50 Jenkins interview questions frequently asked in top MNCs which will help you land an ideal job! These…
DevOps Best 37 DevOps Tools To Learn And Master In 2025: Docker, Git & More
Want to know what are Devops tools and their uses? Or looking for the best DevOps list for automation, monitoring, & development?…
DevOps DevOps Engineer Job Description: Roles and Responsibilities [2025 Updated]
Discover the essential roles and responsibilities of a DevOps Engineer by exploring the detailed DevOps job description, and…