
How to Use Gitignore to Its Full Potential
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 template when creating a new repositoryIn 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.
Patterns
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:
# This will ignore the `node_modules` folder
node_modules
# This will ignore the `.env` file
.env 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:
| Pattern | Matches |
|---|---|
build | Matches a file or folder called build |
build/build.log | Matches builg.log inside the build folder |
*.log | Matches all files ending with .log |
build/*.log | Matches all files ending with .log inside the build folder |
build/**/*.log | Matches all files ending with .log inside any subdirectory within the build folder |
/build.log | Matches files in the root directory |
*.log | Negate build.log from being ignored |
log-[0-9].log | Match files from log-0.log all the way to log-9.log |
log-[a-z].log | Match files from log-a.log all the way to log-z.log |
\[\[path\]\].js | Escapes 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:
.git/info/exclude 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:
# MacOS
git config --global core.excludesFile ~/.gitignore
# Windows
git config --global core.excludesFile "%USERPROFILE%\.gitignore" 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:
git config --global core.excludesFile
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_modulesfolder. This folder contains dependencies for your project that can be safely ignored and should not be stored in your repository. - Caches: such as the
.cachefolder. Cache folders are used for caching to speed up builds and dev environments. These are generated programmatically. - Builds: such as a
distorbuildfolder. These files are generated at runtime, just like.cachefolders. They contain production-ready assets that do not belong in version control. - Logs: such as the
npm-debug.logfile. Logs are generated programmatically to help with debugging. These files can be regenerated on demand and they can be safely ignored. - Credentials: such as
.envfiles. 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.

Troubleshoot
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:
git rm --cached node_modulesIn 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:
*.log
+ !build.log 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:
> git check-ignore -v build.log
.gitignore:6:*.log build.log 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! π¨βπ»

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:






