The .gitignore file
The .gitignore file tells git which files it should ignore. Ignoring the files means the VCS doesn’t track changes to them. The file is mostly used to avoid committing transient files from your working directory that aren’t useful to other collaborators, such as compilation products, temporary files IDEs create, etc. A file named .gitignore usually resides in the project’s root folder.
The .gitignore file is hidden
The .gitignore file might not be visible on your file manager (My Files, My Computer, Finder, Folder, whatever it is called) because it doesn’t actually have a name, because it starts with a dot (full stop, or period). You might need to enable showing hidden and system files appropriately to be able to see the file.
Showing the hidden files
- Linux: Ctrl+H
- Mac: CMD + Shift + Fullstop
- Windows: Follow these steps:
- From the View menu, select the Folder & Search Options option
- Switch to the Folder Options tab
- Check the Show Hidden Files option
- Uncheck the Hide Operating System Files option
How to create a .gitignore file
Some tools & IDEs will generate a .gitignore file automatically. If you have cloned a project from the remote, it probably has a .gitignore file already. If you don’t have any, you might want to create a new .gitignore file.
GitHub maintains a repository of .gitignore files for almost all languages and frameworks. You can look for the appropriate file at the https://github.com/github/gitignore repo. You need to download the appropriate file to your project’s root folder, then rename it to .gitignore. Even if you already have a .gitignore file in your project’s root folder you can look at the appropriate file and make necessary adjustments.
What to add to a .gitignore file
If you are not sure whether you should keep a file/folder or ignore it, this example will give you an idea.
1. Ignore Large Files
To limit the size of our repositories, we usually don’t add large files to the VCS. To do that, as soon as the new files or folders are added, we add them to the .gitignore file. Even if you accidentally added and then later deleted the file, the size of the repository will always be way larger than it should have been.
2. Ignore downloaded archives, libraries, etc.
Sometimes you might have downloaded a collection of assets, or an entire library or datasets to work on the project. Instead of tracking the file on the VCS, add a message on the README file how and where to find those extra files.
3. Ignore temporary files
It’s a good practice to have a folder named tmp on your project root, and ignore it by adding a new line, tmp/ in the .gitignore file. You might as well add large and non-essential files to this directory because they will not be checked into the VCS.
3. Environment-specific files
Your development environment might not match your staging or production environment. The settings of your IDE, your local database credentials, paths to your local libraries, and your SDK path might not match that of your coworkers. In that case, whenever your changes are pulled by other members of the project, the project might not build because of mismatched configuration & missing files. That’s why people keep such configuration in a single file and ignore it using the .gitignore file. One example of such files is local.properties in an android app project that includes the path where you have kept your Android SDK.
4. Credentials & Secrets
Similarly, if your project depends on third-party services you might need your secrets and credentials in order to make use of such services. You definitely don’t want such confidential information to be available to everyone in the organization. So you should not check-in such a file to the VCS. An example of such a file is the .env file in a web-based project that includes your credentials of a database server, API Keys, and App Secrets that you must keep secret.
Things to remember about a .gitignore file
- The file itself should be checked into the VCS along with the other files, which means you should not add the line /.gitignore in the file.
- If you had accidentally added that file, delete the line, then run the command
git add .gitignoreto start tracking the file again.
- Closely monitor the changes to the .gitignore file to ensure no files that need to be tracked are accidentally added to it.
- As the .gitignore file is a hidden file, it sometimes gets lost when you copy-paste the project contents to a different folder. Sometimes the entire .git folder is lost too, so take special care while copying contents from a local repository.