DEV Community

aCompiler
aCompiler

Posted on

How to DIG in Git: Best Practices

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.

Keep reading...

There are two types of .gitignore files.

Local .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

  1. Any line in the .gitignore file starts with a hash (#) is a comment.

  2. \ character is used to escape special characters

  3. / figure indicates that the rule applies only to files and folders, which are located in the same folder

  4. An asterisk (*) means any number of characters (zero or more occurrence)

  5. Two asterisks (**) are used to specify any number of subdirectories

  6. A question mark (?) replaces zero or one character.

  7. An exclamation sign (!) indicates the inversion rules.

  8. Blank lines are ignore

  9. 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.

  1. Files with sensitive information
  2. Compiled code, such as .dll or .class
  3. System files like .DS_Store or Thumbs.db
  4. Files with temporary information like a log, cache, etc..
  5. Generated files like dist folders

Advantages of using gitignore

Here the top 3 advantages (that’s why I use .gitignore)

  1. It helps you to clean your code repository by ignoring unwanted files.
  2. Your repository size stays under control, especially if you are working on a big project.
  3. 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.

Bonus

Its time for the Bonus download, you can download the 51 git commands from here

And 35+ Git best practices from here.

So let me know did you prefer local gitignore or global gitignore file

Top comments (0)