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.
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
The easiest pattern is a literal file name, for example:
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:
If you leave the slash off of the end, it will match both files and directories with that name.
* matches 0 or more characters (except the
/). So, for example,
*.log matches any file ending with the
Another example is
*~, which matches any file ending with
~, such as
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.
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:
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.
**/logsmatches all files or directories named logs (same as the pattern
**/logs/*.logmatches all files ending with
.login a logs directory
logs/**/*.logmatches all files ending with
.login 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:
# macOS Files .DS_Store
.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
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
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
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:
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.
Test your skills. Learn something new. Get help. Repeat.Start a FREE 10-day trial