Skip to content

Creating a Python .gitignore File


The .gitignore File - Python Gitignore

The .gitignore file is a powerful tool in Git that allows you to ignore specific files or directories in your project that you don’t want or need to include in your repository. Whether it’s auto-generated files, test data, or configuration files, you can use .gitignore to exclude them from version control. In this tutorial, we will learn how to create and use a .gitignore file effectively.

Creating a .gitignore file

  1. Open your terminal or command prompt.
  2. Navigate to the root directory of your Git repository.
  3. Create a new file named .gitignore using your preferred text editor.

Ignoring specific files or directories

Once you have created the .gitignore file, you can specify which files or directories you want to ignore. Each line in the .gitignore file represents a pattern that Git will use to match and ignore files. Here are some examples:

Ignore specific files:
To ignore a specific file, simply add its name to a new line in your .gitignore file. For example, to ignore a file named config.xml, add the following line:


Ignore specific directories:
To ignore an entire directory, include a trailing forward slash (/) after the directory name. For example, to ignore a directory named logs/, add the following line:


Ignore files with a specific extension:
You can also use wildcard characters to ignore files with specific extensions. For example, to ignore all .csv files, add the following line:


Ignore files in a specific directory:
If you want to ignore files in a specific directory, you can specify the directory path relative to the root of your repository. For example, to ignore all .txt files in a directory named docs/, add the following line:


What NOT to include in version control

When using Git, it is important to follow some best practices regarding what should not be included in your repositories. Here are some guidelines to keep in mind:

  1. Generated files: Only include source files in your repository, not generated files. Generated files are files that are created automatically during the development process, such as compiled code or configuration files generated by your IDE.

  2. Binary files: Be careful with binary files like JPEGs or MP3s. Git treats them differently and stores the entire file instead of a snapshot, which can lead to a bloated repository over time.

  3. Confidential information: Never include confidential or sensitive information in your repository. This includes personal information, passwords, API keys, or any other sensitive data. Be extra cautious when working with public repositories.

Resources for .gitignore files

  • A useful site that generates .gitignore files for common IDEs, operating systems, and toolchains. Simply input your preferences, and it will generate a suitable .gitignore file for you.

  • GitHub Official Repository: The official GitHub repository that contains a collection of .gitignore templates for various programming languages, frameworks, and tools. You can find templates for popular editors, operating systems, and more.

Remember, the .gitignore file is an essential part of managing your Git repository. By ignoring unnecessary files, you can keep your repository clean and focused on the source code that truly matters.