Git Ignore File: How and When to Use It
Artifacts are produced throughout the development process. These large binary assets can be completed builds, precompiled headers, object code, library files, etc. But sometimes you need to ignore files if you aren't making changes. And that means using git ignore file if you're working in Git.
So when and how should you use Git ignore file?
When Do You Use Git Ignore File?
The git ignore file rule allows you to ignore a file you've committed in the past. You use it when you do not want to recommit a file, for example a build artifact.
What many don’t realize is that it’s possible to add an artifact file to a repository. Then you can use the git ignore file feature to intentionally untrack the file. It may seem a little counterintuitive to add files to the version control that are chosen to be ignored, but there’s a method to the madness.
Including a git ignore file in your repository allows developers on the project to use the file. It also:
- Improves the “signal to noise ratio” for any Git command.
- Allows builds to come from one source.
- Avoids potentially costly commits of files that shouldn’t be versioned.
In short, it’s a good practice to develop a git ignore folder for every project. Then you can custom-tailor it depending on the types of files or artifacts that need to be included. You can also choose to define multiple Git ignore files using different directories in your repository. However, the simplest approach is define just one git ignore file in the root. As this git ignore files is checked un, it is versioned just like any other file.
How Do You Use Git Ignore File?
You can use git ignore file by deleting a file from the repository and adding a .gitignore rule for it.
So what should you ignore? To help decide what files to add to your .gitignore folder start by asking:
- Is the file used by your project?
- Are the files used by other people on your team?
- Is the file generated by another process?
If the files are not used by your project or other team members, it can be ignored. If the file is generated by another process, like a build runner, then it can be put into the .gitignore file. Some common file types to ignore include:
- OS files.
- Application files.
- Language files.
- Package managers.
Sample Git Ignore File
# This is an example of a comment, # whereas blank lines are ignored. *.obj *.lib !MySpecialCode.obj
Any file that ends with the *.obj and *.lib extensions –– except a file named MySpecialCode.obj –– will be ignored. For individual files, Git processes the contents of the file one line at a time. This makes it possible to develop quite precise matching rules with a little effort. And because all your artifacts are in one folder, they do not get lost.
Go Beyond Git Ignore File
Having all your assets in a repo –– even if some are ignored –– has several benefits. It is less for admins and developers to maintain. It can create a single source of truth. But with Git, this severely impacts performance. Even using tools like Git LFS for artifacts can slow you down.
You can more easily version artifacts using Helix Core –– version control by Perforce. Then you can combine these artifacts with the Git repos stored in Helix4Git. Using the power of the Perforce server, you get 80% faster builds. This accelerates development and releases.
Learn more about Perforce Git tools. Contact us to learn more about maximizing performance.