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.
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
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
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
To add files to a .gitignore
file, you can use the echo
command:
$ echo "file1.txt" >> .gitignore
$ echo "file2.txt" >> .gitignore
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
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
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
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
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