Gitignore pyc - Ignore .pyc files in git repository

3.2K    Asked by JenniferFraser in Devops , Asked on Jun 15, 2021

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.

Answered by Jake Sanderson

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?



Your Answer

Answer (1)

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

  *.pyc

This 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 --ignored

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

5 Months

Interviews

Parent Categories