DIG in Your GIT — Best Practices
In this article, explore .gitignore and see the different types and the basic rules.
Join the DZone community and get the full member experience.Join For Free
This is one of the easiest best practices, which you can start using in less than 2 minutes. But the impact will be huge.
Today I will share one of my all-time favorite practice - DIG in your GIT
And I have a bonus section for you, I am sharing two downloadable pdf with 51+ important git commands with a one-line summary and 35+ git best practices with you in this post.
Don’t Ignore .gitignore (DIG) in your Git repository. I have noticed in the past many developers do not use a .gitignore file.
Use the .gitignore file is one of the git best practices. And I will cover in this post how gitignore can boost your code quality with other advantages.
Also, I will explain how you can use your global .gitignore file (without adding or pushing). And this comprehensive file will be used by all your repository.
So let’s dive in…
What Is .gitignore?
There is not any git ignore command which you can use directly to ignore the unwanted files in your repository; instead, you should use .gitignore file.
In simple words, the .gitignore file is a text file that tells Git about which files to ignore.
Git checks three things in your working copy.
Untracked — Your changes that have not been staged or committed.
Tracked — Your all changes that have been previously staged or committed.
Ignored — All files which you told to Git to ignore.
It's easy to create .gitignore file, create a text file, and name it .gitignore (that’s it)
Remember to add a single dot . at the beginning of this file name.
How Many Types of .gitignore Files?
I have seen many developers use the local .gitignore file. They add the gitignore file in their project repository. But very few use the global file. In this section, I will explain both.
There are two types of .gitignore files.
Local .gitignore file
Global .gitignore file
If you add the.gitignore file in the root of your git repository, it will be considered as a local file. It means that the .gitignore file will work on that repository. And .gitignore file should be committed to your repository.
Global .gitignore File
If you place a similar file in the root of your home directory, it will act as a global .gitignore file.
What is means?
This file affects every repository you use on your machine.
The most significant advantage of using a global file is that you don’t need to commit it. And making one change affects all of your repositories.
Basic Rules for .gitignore File
Each line in the .gitignore file specifies a pattern. Here are some basic rules that help you to set up the .gitignore file
- Any line in the .gitignore file starts with a hash (#) is a comment.
- \ character is used to escape special characters
- / figure indicates that the rule applies only to files and folders, which are located in the same folder
- An asterisk (*) means any number of characters (zero or more occurrence)
- Two asterisks (**) are used to specify any number of subdirectories
- A question mark (?) replaces zero or one character.
- An exclamation sign (!) indicates the inversion rules.
- Blank lines are ignore
- You can ignore entire directories paths and adding a / on end:
What Files Should Be Ignored
You can ignore many files which are usually auto-generated, platform-specific, and other local configuration files.
- Files with sensitive information
- Compiled code, such as .dll or .class
- System files like .DS_Store or Thumbs.db
- Files with temporary information like a log, cache, etc..
- Generated files like dist folders
Advantages of Using gitignore
Here the top 3 advantages (that’s why I use .gitignore)
- It helps you to clean your code repository by ignoring unwanted files.
- Your repository size stays under control, especially if you are working on a big project.
- Your every single commit, push, and pull request will be clean.
There is no doubt that Git is powerful. But in the end, it's just another computer program.
So it's a team effort to use best practices and to keep your code repo stable and make sure you use git ignore file.
Also, you can download the 51 git commands and 35 Git best practices from here
So let me know, do you prefer local gitignore or global gitignore file?
Opinions expressed by DZone contributors are their own.
Event-Driven Architecture Using Serverless Technologies
Integrating AWS With Salesforce Using Terraform
Effortlessly Streamlining Test-Driven Development and CI Testing for Kafka Developers
Knowing and Valuing Apache Kafka’s ISR (In-Sync Replicas)