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:
$ cd test
$ nano file.txt
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.
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:
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:
And to add content use the following command:
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:
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:
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.
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.