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.
.DS_Store
on macOSdist
folderstodo.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.
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.
The easiest pattern is a literal file name, for example:
1.DS_Store
This will ignore any files named .DS_Store
, which is a common file on macOS.
You can ignore entire directories, just by including their paths and putting a /
on the end:
1node_modules/
2logs/
If you leave the slash off of the end, it will match both files and directories with that name.
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 /
.
You can use a prefix of !
to negate a file that would be ignored.
1*.log
2!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:
1logs/
2!logs/example.log
Due to performance reasons, git will still ignore logs/example.log
here because the entire logs
directory is ignored.
**
can be used to match any number of directories.
**/logs
matches all files or directories named logs (same as the pattern logs
)**/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.
Any lines that start with #
are comments:
1# macOS Files
2.DS_Store
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.
If there are some files you want to ignore for just this repository, you can put them in .git/info/exclude
.
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:
1git config --global core.excludesFile ~/.gitignore
Then you can add any global rules to ~/.gitignore
.
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:
1git rm --cached FILENAME
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.
1git check-ignore -v example.log
The output will look something like this:
1.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.
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.
Explore these Git courses from Pluralsight to continue learning: