How to Use Gitignore to Its Full Potential

How to Use Gitignore to Its Full Potential

Master ignoring files in Git
Ferenc Almasi β€’ 2023 April 04 β€’ Read time 5 min read
Learn how you can use the .gitignore file in and out to end up with a cleaner repository.
  • twitter
  • facebook

Git keeps track of everything to let us easily review changes, collaborate with others, and revert to previous versions of our code if necessary. But what happens if we don't want a file to be tracked?

When it comes to Git, a file can be in one of three states: tracked (either staged or committed), untracked (modified files that are not staged or committed), or ignored. To ignore files in Git, we can use the .gitignore file. In this tutorial, we are going to cover how to get the most out of .gitignore.

How to Use .gitignore

To get started, create a new .gitignore file at the root of your Git directory if you don't already have one. In case you bootstrap the project with a tech stack such as React or Svelte, you will most likely already have a .gitignore file. 

Note that .gitignore files can be placed anywhere and will affect all files within that directory. For consistent results, always place them at the root of your project.

Using templates with GitHub

If you are using GitHub to create a new repository, you will also have the option to use a predefined .gitignore template that already includes the most common ignore patterns for your project.

.gitignore templates by GitHub
Use a predefined .gitignore template when creating a new repository

In case you are not starting from scratch, you can find the predefined .gitignore files in this repository that collect useful .gitignore templates for various projects.


So, what should go inside a .gitignore file? Git is capable of handling glob patterns. However, most commonly, you can simply specify files or directories in the following way:

Copied to clipboard!
# This will ignore the `node_modules` folder

# This will ignore the `.env` file
How to ignore directories and files in Git

To use comments in a .gitignore file, start the line with a hashtag.

Nevertheless, let's take a look at some more examples of how you can use glob patterns within .gitignore to match certain files:

buildMatches a file or folder called build
build/build.logMatches builg.log inside the build folder
*.logMatches all files ending with .log
build/*.logMatches all files ending with .log inside the build folder
build/**/*.logMatches all files ending with .log inside any subdirectory within the build folder
/build.logMatches files in the root directory
Negate build.log from being ignored
log-[0-9].logMatch files from log-0.log all the way to log-9.log
log-[a-z].logMatch files from log-a.log all the way to log-z.log
\[\[path\]\].jsEscapes glob pattern. Matches file named [[path]].js

Creating personal ignore rules

In case you want to ignore specific files on your local machine but don't want to commit them in your .gitignore file, you can create personal ignore rules on your system. If you only care about your working repository, add your ignore rules to the following file within your project:

Copied to clipboard!
Adding personal ignore rules for local projects

In case you need to add global ignore rules (such as specifying excludes for your editor across multiple repositories), you can add a global .gitignore file using one of the following commands:

Copied to clipboard!
# MacOS
git config --global core.excludesFile ~/.gitignore

# Windows
git config --global core.excludesFile "%USERPROFILE%\.gitignore"
Adding personal ignore rules globally

To verify if your global .gitignore file has been created successfully, you can run the same command from the terminal without specifying the path to the .gitignore file:

Copied to clipboard!
git config --global core.excludesFile
Verify if the global .gitignore has been created successfully

This command will return the path to the global .gitignore file. If no path or an invalid path is returned from the above command, it means your global .gitignore won't work. Now you can add your global ignore rules to this file.

What Files Should be Ignored?

When it comes to ignored files, the question comes: what should be ignored in the first place? Here is a list of the 5 most common cases that you should include in your .gitignore file:

  • Dependencies: such as the node_modules folder. This folder contains dependencies for your project that can be safely ignored and should not be stored in your repository.
  • Caches: such as the .cache folder. Cache folders are used for caching to speed up builds and dev environments. These are generated programmatically.
  • Builds: such as a dist or build folder. These files are generated at runtime, just like .cache folders. They contain production-ready assets that do not belong in version control.
  • Logs: such as the npm-debug.log file. Logs are generated programmatically to help with debugging. These files can be regenerated on demand and they can be safely ignored.
  • Credentials: such as .env files. These can include things like API keys or secrets containing sensitive information.

As we can see, most commonly, you want to ignore programmatically generated files that can be recreated on the fly.

Never commit sensitive information in your repository.

Looking to improve your skills? Check out our interactive course to master JavaScript from start to finish.
Master JavaScriptinfo Remove ads


When it comes to ignored files, there may be cases where you need to ignore an already committed file or do the other way around: commit an already ignored file.

Ignore a committed file

To ignore an already committed file, we first need to remove the file from the repository, then add a new .gitignore rule for it. This can be achieved with the git rm command in the following way:

Copied to clipboard!
git rm --cached node_modules

In this example, if you have accidentally committed the node_modules folder, this command will remove it from the repository but keep it in your file system. You can then add a new .gitignore rule to start ignoring it.

In case you want to delete the local file from your system, you can omit the --cached option.

Commit an ignored file

In case you have an ignored file from the past that you want to track from now on, you can simply remove the rule from the .gitignore file to let Git track it again. If you have a general rule that ignores certain file extensions, such as log files, then you will need to add an exclusion:

Copied to clipboard!
+ !build.log
Exclude previously ignored files

Debug ignored file

Lastly, it's also possible to debug an ignored file to find out why it is being ignored by Git. Use the git check-ignore command with the -v (verbose) flag on a file to check the reason for the ignore:

Copied to clipboard!
> git check-ignore -v build.log
.gitignore:6:*.log      build.log
Check why build.log is ignored

This will return the line in the .gitignore file that is responsible for ignoring the file passed to the command. This can come in handy when you have multiple complex patterns that make it difficult to track down ignore rules.

Now you know everything you need to know about the .gitignore file. Thank you for reading this tutorial, happy coding! πŸ‘¨β€πŸ’»

  • twitter
  • facebook
Did you find this page helpful?
πŸ“š More Webtips

Rocket Launch Your Career

Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies:



This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.