While working within your repository, you might notice system or configuration files created by your operating system or text editor. These files are typically things that you don't want git to track or include in the history of your project. Although you can manually ignore those files by never committing them to the history of the project, there is an easier way for you to ignore those files. Git enables you to ignore specific files, files types, and folders through the use of a text file you can add to your repository titled .gitignore
. This file instructs Git to stop tracking changes of certain files, which means that these files will not be pushed to GitHub from your local machine.
Example files that you might want Git to ignore:
- Secrets, e.g.
*.pem
,*.key
- Binary files, e.g.
*.pdf
,*.docx
- Log files, e.g.
*.log
- Build files, e.g.
Build/*
The github/gitignore repository is a collection of .gitignore
templates based on popular programming languages.
For example, the following snippet shows some suggested files you should start ignoring in a Python project.
These templates are also available when you create a new repository:
This website is another resource for you to find appropriate .gitignore
templates when your project contains multiple programming languages.
-
In your Terminal or Git Bash,
cd
to a repository of interest and create a.gitignore
file:touch .gitignore
For Windows users who are not using a bash-based terminal:
echo >> .gitignore
-
Go to
github/gitignore
and choose a template of your choice and paste it in the file. -
To share the gitignore rules with other users who clone the repository, commit the
.gitignore
file to your repository.git add .gitignore git commit -m "add gitignore file"
-
You can also create a global
.gitignore
file to define a list of rules for ignoring files in every local Git repository.To do this, you should be in your root directory:
touch .gitignore_global
-
Let's grab an example
.gitignore
template and paste it in the file:# Compiled source # ################### *.com *.class *.dll *.exe *.o *.so # Packages # ############ # it's better to unpack these files and commit the raw source # git has its own built in compression methods *.7z *.dmg *.gz *.iso *.jar *.rar *.tar *.zip # Logs and databases # ###################### *.log *.sql *.sqlite # OS generated files # ###################### .DS_Store .DS_Store? ._* .Spotlight-V100 .Trashes ehthumbs.db Thumbs.db
-
Configure
git
to use this file:For Mac Users:
git config --global core.excludesfile ~/.gitignore_global
For Windows Users:
git config --global core.excludesfile %USERPROFILE%\.gitignore_global