Skip to main content

Section B.7 The .gitignore File

As you gain more experience with Git and GitHub, you may start wondering if there is a way to hide files from public view. Your project may come with files that are private to you/your company and are not necessary to share with everyone else. You also may have programs that generate files that are used by a second program and these intermediate files do not need to be shared. Whatever the circumstance, you can choose to block a file or folder from being tracked by Git.
The file for this is .gitignore. The dot in front of the file name is necessary as this specifies that the file is, by default, “hidden” when being viewed in a file browser (to prevent accidentaly deletion). You should be able to edit this file on GitHub or access it with your text editor.
It is possible to generate a gitignore template when creating a repo. These can be helpful but for my projects, I prefer to add the file manually and fill it in myself as I go.
All that goes in this file is a list of file/folder names and/or extensions you wish for Git to ignore. Keep one name per line. You can add comments to the file (to help break up sections) by starting a line with a hashtag (fine, a pound sign) and a space: # This is a comment. For example,
# Ignore generated images folder
generated-images/

# Random files to ignore
secret.txt
private_data.csv
As a bonus, gitignore files accept standard generalizing arguments and wildcards. For example, *.pdf will ignore anything with the .pdf file extension.
You should know that once Git tracks a file (or folder), you can’t just add that file’s name (or folder) to the gitignore file and expect it to disappear. For any files you had Git tracking that you wish it would now ignore, see the instructions in Section C.3. Note that any files you treat in this manner will still exist in the commit history of your repo and on any forks for your repo (the next time a user pulls changes from your repo, their copy of the file will be deleted). See Warning C.3.0.1