The .gitignore file is a text file that tells Git which folders or files to ignore in a project when sharing it through git.
What Is .gitignore?
The .gitignore text file specifies intentionally untracked files to ignore while sharing your code. To be clear, it ignores certain items from git commands like git push. Using it, you can dedicate which files/folders you don't want to share using git.
Keeping folders or files from your git records makes your code cleaner (You don't share module files that everyone will install on their own) and safer (You don't accidentally leak info or environment details). Moreover, you can remove pointless stuff from your commits like MacOS .DS_store changes.
Usually, devs use .gitignore to hide:
- log files
- temporary files
- hidden files
- personal files
However, the .gitignore file itself IS tracked by Git.
Why You Might Want .gitignore?
It is a good practice to use a .gitgnore file to protect your code from leaks and to keep it clean. It is almost mandatory on any open source projects or big collaboration projects. Otherwise, your code becomes a big pile of personal, log, or hidden files. Cluttering your code is never a good idea.
Moreover, it keeps your private files from public git records. Without it, you can accidentally leak your API key, production info, or environment settings which can hurt your project. Just to put it in perspective, over 100000 GitHub repos have leaked their API or cryptographic keys, and the average API key leak costs around $1.2 million per year.
Problems .gitignore Helps to Solve
- API Key Leak
- Poor code quality
How to Implement .gitignore?
If you are using GitHub, you can create a .gitignore file while creating new repo using GitHub templates. Just scroll down to where the license option is and select Add .gitignore
You can create your .gitignore file manually. Start with creating a blank text file and name it .gitignore. Be sure to add the dot at the start. Then each line is a pattern where:
- "*" is used as a wildcard match
- "/" is used to ignore pathnames relative to the .gitignore file
- "#" is used to add comments to a .gitignore file
- "!" is used to create an exception
- "?" matches a single non-specific character
So, for example, we want to untrack all
.log files and the
.env file. Your .gitignore file should look like this:
Look into popular Github templates and adjust them according to your needs.
You can also create a global .gitgnore file using the following command
git config --global core.excludesfile ~/.gitignore_global. Global .gitignore behaves like normal .gitignore, but it is across multiple repos.
Common Pitfalls of Implementing .gitignore
- The pattern matching is not correctly set
- Important security file is accidentally shared
- .gitignore is not set up
Resources for .gitignore
Want to write for DXKB?
Feel free to contribute. People from DXKB community will be more than happy.
A retrospective meeting is an opportunity for the team to inspect itself and create a plan for improvements to be included in the next Sprint.Read more
Meaningful Meetings are methods that see meetings as obstacles in work. Through practices like setting reasons, creating agendas, tracking outcomes, or meeting polices, you can shorten the meetings and boost your team's performance.Read more
DevOps is a set of practices that brings development and operations teams together. The collaboration helps to release software much faster.Read more
Release management is the process of managing, planning, designing, scheduling, testing, controlling and deploying of a software build through different stages and environments; in preparation for software releases.Read more
Software Development Kit (SDK)
A SDK is a set of toolchains that allow a developer to write applications for a given platform or allow them to use a specific framework to do so.Read more