Login

Log in with one your accounts

Contribution

We are happy you want to contribute to DXKB. Please choose your preferred way

All Articles
Jan 25, 20233 min read

.gitignore

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
  • etc.

However, the .gitignore file itself IS tracked by Git.

Ready to put this knowledge into action?

Visit our website and learn how DX Heroes can help your business succeed.

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

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

GithubSource: Github

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:

*.log

.env

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

Was the article helpful?

Want to write for DXKB?

Feel free to contribute. People from DXKB community will be more than happy.

Contribution

We are happy you want to contribute to DXKB. Please choose your preferred way