The gitignore File in Git

Abdul Jabbar Jan 30, 2023
  1. Ignore Files in Git
  2. Local .gitignore File
  3. Global .gitignore File
The gitignore File in Git

When we work with the Git repository for making commits, we decide which file we want to commit using commands git add FILENAME, and then we do the git commit.

But what if we don’t want to commit some files in the local repository, and then we accidentally commit them? Then there is the easiest way to deal with this situation. In this case, we will use the command .gitignore to resolve this issue. It will ensure that Git will not track these files, and Git will ignore them. Furthermore, we can say that the .gitignore file commands will find which files to ignore in a local repository.

Locally, a .gitignore file can be found in the root directory of our project. We can also create a global .gitignore file, ignoring any changeset through the Git repository.

For creating a local one, we have to make a text file named .gitignore, and the thing to remember is to place a "." before the file name. Afterward, we can edit it as per our needs. Additional files or folders should be listed in each new line that we want to ignore.

The entries can follow the below-mentioned pattern in the file.

  • * This can be utilized as a wildcard match
  • / This can be utilized to ignore pathnames relative to the .gitignore file
  • # This can be utilized to add comments to a .gitignore file

Ignore Files in Git

When we push files to the Git repository, we come through many files that we want to ignore; hence .gitignore consists of the list of files that tell Git files we have to ignore when changes are committed. Some of them cannot be tracked; hence, neither can we commit them nor send them to the repository by Git push command.

This procedure may become slow when a load of receiving and sending files becomes too much from the local repository. Below we are configuring Git to ignore those files we don’t desire to commit in Git.

Local .gitignore File

Firstly, we’ll develop a .gitignore file in the root directory, creating a command to ignore files and directories when we make a commit using the Git command. For other users who will clone the repository, there are some ignored rules to pull, which are those rules through committing the .gitignore file into our remote repository.

Following are the rules to access the local gitignore file.

  • Access Git Repository.
  • Go to the location of our current Git repository.
  • Make a .gitignore file for our repository.
  • Push the .gitignore file to our repository.
$ touch .gitignore

If the command mentioned above succeeds, there will be no output. If we are willing to ignore a file that is checked in, then we should delete it before adding a rule to ignore it.

$ git rm --cached FILENAME

Global .gitignore File

For every time use, or we can say globally, we can develop a .gitignore file that sets rules for ignoring files in all of our Git repositories on our personal computer. For that purpose, we can modify the file *~/.gitignore_global and make some rules for it.

Following are the rules to access the global gitignore file:

  1. Access Git Repository.
  2. Guide Git to use the exclude file by the following command.
$ git config --global core.excludesfile ~/.gitignore_global
Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn