Gitignore File For Your Python Project: How Does It Look Like?

Claudio Sabato
7 min readDec 8, 2022

A gitignore file contains information about files and directories that Git should ignore. In this tutorial, you will learn how to use gitignore files in your Python project!

What is Gitignore and Why Do You Need It?

A file in a Git repository can be either tracked or untracked. Tracked files are files Git knows about, everything else is untracked. Git will not start tracking untracked files unless you request to do so.

A gitignore file plays a crucial role in explicitly specifying the files Git should ignore and helps avoid pushing by mistake files that shouldn’t be tracked in a repository.

Examples of files that shouldn’t be tracked in a Git repository are files that contain sensitive data (e.g. passwords), log files, or files generated by a build system.

A gitignore file consists of generalized patterns that can match a wide variety of files. The name of this file is actually .gitignore. It starts with a dot (.).

As a general rule, add a .gitignore file to your repository when you create it and before starting to make any changes to your project.

Let’s learn how to edit .gitignore files manually and write gitignore patterns.

Create gitignore files with Glob Patterns

Glob patterns are extensively used in gitignore files to denote a wide range of possible patterns to ignore. If you have ever worked with regular expressions, you are already familiar with some of them.

We will test these patterns while going through them…

Create a directory called git_ignore on your machine, access the directory, and then use the git init command to add this directory under Git control.

$ mkdir git_ignore
$ cd git_ignore
$ git init
Initialized empty Git repository in /opt/git_ignore/.git/

Let’s examine some of the most common globbing expressions:

One of the most commonly used gitignore patterns is the asterisk (*). An asterisk stands for zero or more characters (except slashes).

Claudio Sabato

I’m a Software Engineer and Programming Coach. I want to help you in your journey to become a Super Developer!