What’s a GITIGNORE File and How Do I Use One?

When working with a git repository, there is a danger of unwanted data. Thankfully, you can create a file with a GITIGNORE extension and define which files and folders should be ignored in the project. You can form a global GITIGNORE data for use with every Git repository.

How to Create GITIGNORE Files

GITIGNORE files are plain text files, so you can open them using Notepad or any text editor. Here is how to make a GITIGNORE file:

  1. Open any text editor and hit save. Change the name to .gitignore.
  2. When editing the file, each line is reserved for a single folder or a file that a git should ignore.

Use “#” to add comments to a .gitignore file

Use “*” for a wildcard match

Use #/” to ignore paths relative to the GITIGNORE file.

As an example, your GITIGNORE can look like this:

# Ignore node_modules folder


# Ignore files related to API keys


# Ignore Mac system files


# Ignore SASS config files


# Ignore all text files


As you may know, comments are optional.

Ignore directories by including their paths and using “/” at the end of every line.

For example:




While the wildcard symbol “*” can be used to ignore all files with a specific extension, you can combine it with the negation symbol “!”. Here is an example:




The above would inform the git to ignore every file with a .txt extension except for readme.txt and main.txt.

Wildcards may be used for directories. Just be careful when using them, as shown in this example:



You might think that the git will now ignore every file inside the “test“ directory except “example.txt”. However, that is not the case. It will still ignore example.txt for performance reasons, as we’ve defined that the entire “test” directory is ignored.

You have the option to use double Asterisk (**) to match any number of directories and files. For example, Test/**/*.txt will tell git to ignore only files ending with .txt in the test directory and its subdirectories.

GITIGNORE File How To Use One

Three Ways to Implement GITIGNORE Files

Use a global GITIGNORE with all your projects, either with your colleagues or alone. However, you can create a local GITIGNORE or even specify exclusion rules.

Create a Local GITIGNORE File

There are two ways to define GITIGNORE files. You can have a GITIGNORE file at the directory level or create a file in the root. In most cases, GITIGNORE includes properties file and configuration files. When your teammates are also pulling the same GITIGNORE file, be sure to use “#” to add comments for clarity.

Create a Global GITIGNORE File

If working with multiple git repositories, you can save a lot of time by defining global rules for your local repositories.

  1. Create the GITIGNORE file and define the universal rules to apply.
  2. Use the core.excludesFiles property to transform your local GITIGNORE file to global. Use the following line:

git config –global core.excludesFile <PATH TO GITIGNORE FILE>

Create Personal GITIGNORE Rules

If you have a specific repository, rules, you can modify the global or local GITIGNORE files. These rules can’t be shared or controlled with other members of your workgroup. You might use personal GITIGNORE rules for your local working directories or logger settings.

Using GITIGNORE File for Committed Files

Ignore Committed Files

Committed files integrate into the repository since they are cached. If you ignore this data, it needs to be removed first. The suggested method to do this is to delete these files and then commit and apply the changes. Once you do that, you can finally use GITIGNORE by defining a rule where the file will be ignored locally. Add this line:

git rm –cached <FILE NAME>

In this fashion, the file will be removed from the repository even though it will stay in the working directory. A GITIGNORE file with this rule should be in the working directory.

Commit Formerly Ignored File

If you wish to commit a file that has been ignored, you can perform this using the “git add” line and the “force” option. For example, if you want to remove example.txt from the GITIGNORE file while keeping it repository committed t, insert lines:

git add -f example.txt

git commit -m “Force adding example.txt”.

You’ve now added an ignored file to the repository. The final step is to modify the GITIGNORE file by removing the ignoring pattern or the rule.

What's GITIGNORE File How To Use One

Use GITIGNORE to Your Advantage

As GITIGNORE files are present in almost every project, you’ll need to learn how to use them. While the commands are simple, it’s essential to define your rules and ignore the correct files. With the proper use of GITIGNORE, you will save time and optimize your project.

How often do you need GITIGNORE files in the git repository? Do you mostly use global or local rules? Please share your experiences in the comments section.

Leave a Reply

Your email address will not be published. Required fields are marked *

Disclaimer: Some pages on this site may include an affiliate link. This does not effect our editorial in any way.