Git

What is git ignore

Git provides an extensive open-source version control support to store and work on the projects in a parallel manner. These repositories can be managed locally and, on the server, as well; the server support of Git has enabled millions of stakeholders to contribute to a specific project. Generally, Git observes any file in the working directory from three perspectives; they are tracked, untracked, and ignored. The tracked observations refer to the files that are committed previously and the untracked category implies to the uncommitted files. However, the ignored category contains the files or directories that you do not want to commit. The ignore support of Git keeps the directories in the untracked category until they are removed from the ignore category. In this article, we have described the way to ignore any file or directory to commit.

How git ignore works

It is noted that the ignore feature of Git is not followed by any command but as discussed above ignoring files lies in one of three core working areas of Git. The ignoring accessibility of Git can be tracked in the “.gitignore” file that resides in the root directory or in case it is not available you can create it. Afterwards, the rules for ignoring files must be written in the files that you do not want to commit. These files are then matched to check their existence and then ignored from committing. After ignoring, it is noticed that the “git status” command will not display those specific files or directories that are ignored. However, the files that Git has already tracked will not be affected by this ignore feature.

How to configure git ignore directory

This section lists down the steps to ignore the directories to commit from Git repository; for ease, we have broken down this section into several steps that you need to follow for activation of the ignore feature:

Step 1: Make new directory

After that, we have executed the following commands that will create a “test” directory in your git project and inside that a files.txt file is created:

$ mkdir test

$ cd test

$ nano file.txt

Text Description automatically generated

Graphical user interface, text Description automatically generated

Step 2: Create and edit the .gitignore file

Your present working directory must be your project’s directory and use the following command to create “.gitignore” file and “nano” editor to edit the file:

Note: You can use nano command to create and edit the file simultaneously. Moreover, you can use any other “vim”, “atom” or default editor as well.

$ nano .gitignore

The time you are editing the above file; you must specify the directory name that you do not want to commit. For instance, in our case, we want to remove “test” directory so we will specify its name in that file as can be seen in the image below:

Text Description automatically generated

Step 3: Initialize the git repository and add the changes

Once you have made the changes that are performed in above steps; you must initialize the git repository and add the changes to this repository. You can use the command mentioned below to initialize the git repository:

$ git init

And to add content use the following command:

$ git add .

Step 4: Commit the changes

Once you have initialized the repo and added the content to the repo; you must commit the changes by using the below mentioned command:

$ git commit -m “this commit is for git ignore”

A screenshot of a computer Description automatically generated

It is observed that the directory “test” has been ignored by “.gitignore” file and it is not committed: the output displays there is only one commit that is performed for “.gitignore”.

How to configure .gitignore for all repositories

The above method is specified for a specific repository; however, Git allows you to create a global access of “.gitignore”: you can add names of directories that you want to ignore during commit in a file named as “~/.gitignore_global”. You can use the git config global command to add “.gitignore_global” to its core variable section. For this, the command mentioned below can be used to perform this action:

$ git config --global core.excludesfile ~/.gitignore_global

Graphical user interface, text, application Description automatically generated

The image above shows that .gitignore_global file is set to as excludes file property and if any directory or filename inserted inside this file will be exempted from committing.

Conclusion

In this technologically rich era, the world has started to work online in a shared environment, where shared environment support is provided by several software. One of the leading packages in this regard is Git that allows you to work remotely and locally on the same projects. The local changes are fetched to remote repositories by using the commit option of git command. Here comes its distinctive feature of ignore; let’s say you have made multiple changes and you do not want to push a few files from the changes then you can use ignore to skip those files. In this informative post, we have tried to convey the basic concept of git ignore and its application also. The tracked files are not affected by this feature; only the files or directories that specify in “.gitignore” file will not be allowed to commit.

About the author

Adnan Shabbir