How to use a .gitignore file

Learn how to use a .gitignore file to keep your repository clean and prevent sensitive information such as configuration files or keys from being uploaded to GitHub.

How to use a .gitignore file
Picture by Ruca Souza, Pexels.
4 minutes to read

The .gitignore file is a special file added to a Git repository to define the files and directories you do not wish to commit to your Git repository. This is useful for files that are generated by your code, such as compiled code, or files that are specific to your development environment, such as .DS_Store files on Mac OS X, or for confidential files that you do not wish to share with the world.

How to create a .gitignore file in Pycharm

There are a couple of ways to create a .gitignore file. How you create the file depends on the development environment you are using. If you’re using an IDE, such as Pycharm, you can create a .gitignore file by right-clicking on the project folder and selecting New > File. Name the file .gitignore and click OK. You can then add the files and directories you wish to ignore to the .gitignore file.

*.pyc

How to create a .gitignore file in the command line

If you’re working from the command line you can create a .gitignore file by typing the following command:

touch .gitignore

You can then add the files and directories you wish to ignore to the .gitignore file, either in the command line or in your IDE or text editor.

*.pyc

How to add files to your .gitignore file in Pycharm

To add files to your .gitignore file in Pycharm, right click on the .gitignore file and select Open in Editor. You can then add the files and directories you wish to ignore to the .gitignore file.

*.pyc

How to add files to a .gitignore file from the command line

To add files to a .gitignore file, you can use the echo command:

$ echo "file1.txt" >> .gitignore
$ echo "file2.txt" >> .gitignore

How to prevent a directory from being committed to Git

If you have a directory that you do not wish to commit to Git, you can add the directory to your .gitignore file. For example, if you have a directory called data that you do not wish to commit to Git, you can add the following line to your .gitignore file:

data

How to prevent a file from being committed to Git

If you have a file that you do not wish to commit to Git, you can add the file to your .gitignore file. For example, if you have a file called file1.txt that you do not wish to commit to Git, you can add the following line to your .gitignore file:

file1.txt

How to prevent any file with a given suffix or extension from being committed to Git

If you have a file with a given suffix or extension that you do not wish to commit to Git, you can add the suffix or extension to your .gitignore file. For example, if you have a file called file1.txt that you do not wish to commit to Git, you can add the following line to your .gitignore file:

*.txt

How to ignore common Python files and directories

If you are working with Python, you can add the following lines to your .gitignore file to ignore common Python files and directories:

*.pyc
.DS_Store
.env
.venv
__pycache__
venv
idea

How to ensure an empty directory is committed to Git

If you have an empty directory that you wish to commit to Git, you can add a file called .gitkeep to the directory. This will ensure that the directory is committed to Git. For example, if you have a directory called data that you wish to commit to Git, you can add a file called .gitkeep to the directory:

data/.gitkeep

Matt Clarke, Monday, October 10, 2022

Matt Clarke Matt is an Ecommerce and Marketing Director who uses data science to help in his work. Matt has a Master's degree in Internet Retailing (plus two other Master's degrees in different fields) and specialises in the technical side of ecommerce and marketing.