Source: https://www.pluralsight.com/guides/how-to-use-gitignore-file
Introduction
When you make commits in a git repository, you choose which files to stage and commit by using git add FILENAME
and then git commit
. But what if there are some files that you never want to commit? It’s too easy to accidentally commit them (especially if you use git add .
to stage all files in the current directory). That’s where a .gitignore
file comes in handy. It lets Git know that it should ignore certain files and not track them.
What Kind of Files Should You Ignore?
- Log files
- Files with API keys/secrets, credentials, or sensitive information
- Useless system files like
.DS_Store
on macOS - Generated files like
dist
folders - Dependencies which can be downloaded from a package manager
- And there might be other reasons (maybe you make little
todo.md
files)
You can get an idea for what sort of files to ignore on gitignore.io, by selecting your operating system, text editor or IDE, languages, and frameworks.
How .gitignore Works
Here’s how it works. A .gitignore
file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that’s what I recommend. However, you can put it in any folder in the repository and you can also have multiple .gitignore
files. The patterns in the files are relative to the location of that .gitignore
file.
Literal File Names
The easiest pattern is a literal file name, for example:
.DS_Store
This will ignore any files named .DS_Store
, which is a common file on macOS.
Directories
You can ignore entire directories, just by including their paths and putting a /
on the end:
node_modules/ logs/
If you leave the slash off of the end, it will match both files and directories with that name.
Wildcard
The *
matches 0 or more characters (except the /
). So, for example, *.log
matches any file ending with the .log
extension.
Another example is *~
, which matches any file ending with ~
, such as index.html~
You can also use the ?
, which matches any one character except for the /
.
Negation
You can use a prefix of !
to negate a file that would be ignored.
*.log !example.log
In this example, example.log
is not ignored, even though all other files ending with .log
are ignored.
But be aware, you can’t negate a file inside of an ignored directory:
logs/ !logs/example.log
Due to performance reasons, git will still ignore logs/example.log
here because the entire logs
directory is ignored.
Double Asterisk
**
can be used to match any number of directories.
**/logs
matches all files or directories named logs (same as the patternlogs
)**/logs/*.log
matches all files ending with.log
in a logs directorylogs/**/*.log
matches all files ending with.log
in the logs directory and any of its subdirectories
**
can also be used to match all files inside of a directory, so for example logs/**
matches all files inside of logs.
Comments
Any lines that start with #
are comments:
# macOS Files .DS_Store
Personal .gitignore Rules
Since the .gitignore
file gets checked into the repository, there are a couple of options if you want to ignore some files without adding it to the .gitignore
rules for the repository. For example, you may have some special files you’re working with on a particular project, or you may use a different editor than your teammates and always want to ignore those types of files.
Local Repository .gitignore Rules
If there are some files you want to ignore for just this repository, you can put them in .git/info/exclude
.
Global .gitignore Rules
If there are some files you want to ignore in all repositories on your computer, you can put them in a global .gitignore
file. First, you have to add a setting to Git with this command:
git config --global core.excludesFile ~/.gitignore
Then you can add any global rules to ~/.gitignore
.
What If I Already Have It Checked In?
Git will not ignore the file if you’ve already committed it. You’ll have to untrack the file first, then it will start ignoring it. You can untrack the file with this command:
git rm --cached FILENAME
Debugging
If you’re having trouble, you can find out why certain files are being ignored by using the git check-ignore command
with the verbose option.
git check-ignore -v example.log
The output will look something like this:
.gitignore:1:*.log example.log
In this example, the .gitignore
file at the root of the project is causing example.log
to be ignored, and the pattern that’s causing it to be ignored is *.log
on the first line.
Summary
Gitignore files are something you’ll come across in almost every project. It’s important to ignore the correct files, as well as your options for personal gitignore rules. For even more details, check out the Pro Git book’s section on gitignore.