How To Ignore Files In Git

You may not always want files in your GIT repository to be committed or to be “committable”, in fact, and the easiest way to do this is to set the files you want to leave outside of this process to be ignored in GIT.

The good news is that GIT makes this process easy, and you can set up a file called .gitignore that identifies all the files that should be ignored.

By the way, if you’re still trying to get to grips with Git, you should read our Guide to GitHub before you read the rest of this article as it will give you the basics you can build upon here.

What You Must Have Done Before You Can Ignore Files In Git

Before you start ignoring files in Github, you’re going to want to have carried out the following steps:

  1. You should have GitHub Desktop installed. This makes it super easy to carry out tasks in Git and to do so graphically. It’s available for download for Ubuntu at Once you’ve downloaded it, you will need to install and configure it too. There’s a full tutorial available on and it’s out of the scope of this article.
  2. You should have a Github Account. You can’t validate any of the commands that we touch on in this tutorial without one.
  3. You should have a local repository. If you want to see the commands that you learn here in action, you want a local repository to play with.

The Problem

You’ve got a code repository but there are files in there that you need but which you don’t want to commit at any stage of the process.

The Solution

This means using .gitignore to create the patterns that you want to ignore files in your repository. Now, it is possible that you haven’t already created a .gitignore file for your repository and if that’s the case you will need to navigate to the folder in your local repository called send-email.

Then execute

$ nano .gitignore

Once you’ve created the file you can start adding text to that file.

Git works with globbing patterns in order to match file names in the repository, this means you can construct patterns to ignore using globbing patterns.

Example PatternWhat This Means
**/logsYou can put a pair of asterisks in front of a pattern, and this allows GIT to match to a directory that exists anywhere in your repository.
**logs/debug.logYou can use that pair of asterisks to match files using the name of the file and the parent directory.
*.logYou can use a single asterisk to match a total of zero or more than zero characters.
*.log !important.logThe exclamation mark signifies that the matching pattern should be ignored. So, if a file matches the first pattern but also matches the second, it is not ignored.
*.log !important.log trace.*This allows you to place exclusions to negated files, so if the first pattern matches, it is ignored, but if the second pattern matches, it is not, but if the third pattern then matches it will ignored.
logsIf you don’t use a slash after this command, it looks for matches to both files and categories and will ignore both.
logs/The slash indicates that the pattern is only a directory, any matched directories will be ignored as will all of their contents.
logs/ !logs/important.logThis is important. While you would expect that logs/important.log would be negated thanks to that exclamation mark, there’s a peculiarity to the use of Git and you can’t negate files that are ignored due to a pattern that matches a directory!
/debug.logThe slash here means that it only matches files to ignore in the repository root directory.
debug?.logQuestion marks match only a single character.
debug[0-9].logSquare brackets match a single character in a user defined and specified range
debug[01].logSquare brackets are used here to match a single defined character from the set specified (here 0 or 1).
debug[!01}.logHere the exclamation mark allows to match a single character but NOT one from the set specified (again 0 or 1 here).
debug[a-z].logAnd yes, you can use square brackets to define alphanumeric ranges.
logs/**/debug.logYou can use the two asterisks here to match zero (or more) directories.
logs/*day/debug.logYou can also use a single asterisk as a wildcard in a directory name.
logs/debug.logIf you specify a file in a particular directory the patterns is relative to the repository root directory. (There’s no need to prepend a slash to this command but if it makes you feel better go right ahead – it still works).

So, it’s time to try this out for yourself.

A good example test for your repository could be run by adding the following to your .gitignore file:





This would ignore all contents in the temp folder, the test folder and all Excel files (of the .xlsx extension) and all comma-separated variable files.

Save the file and then run the status of your repository.

$ git status

You should see that .gitignore isn’t tracked by the repository now.

Then it’s time to get it into tracking:

$ git add .gitignore

$ git status

Now, you should see that .gitignore is not committed.

Then commit the file using:

$ git commit – m “ignored files and folders created”

You can now check the pattern of your .gitignore file for say test.csv

$git check-ignore -v test.csv

Now that you know how to implement ignoring files and directories, you can start to build up more complex patterns and test them yourself.

I’d love to hear about your results as you implement this.



Leave a Reply