In this article, we will learn how to use .gitignore file to ignore the untracked files intentionally.
Total Views: 3116
In this article, we will learn how to use the .gitignore file to ignore the untracked files intentionally. In a project, there are several files like dependency cache (e.g. node_modules), build output directories (e.g. bin, obj, etc.), runtime generated files, IDE configuration file, etc. which don’t require to be tracked or push to the remote repositories. We can specify files/directories which are not required to be tracked by Git.
Git categories the files in the file system as:
1. Tracked (A file that is staged or committed)
2. Untracked (A file that is not staged or committed)
3. Ignored (A file that is specifically ignored by the Git)
How to Ignore untracked files intentionally with .gitignore file?
For demonstration, I already have a .Net Core Console project (for you, it can be any kind project or application).
Let’s initialize the Git repository with the “git init” command.
Use the “git status -u” command in order to check the status for all the untracked/tracked files. As you can see in the below image, there are several files like .vs, bin, and obj directory which we can ignore intentionally.
Let’s add a .gitignore file from the git bash terminal using the “touch .gitignore” command.
Once the file is created, open the “.gitignore” file in order to add some rules.
As you can see, I have ignored the “.vs/” directory from the untracked files.
Save the changes and run the “git status -u” command again. You will notice that the “.vs” directory is ignored by Git. Let’s ignore the bin and obj folder as well.
Open the .gitignore file and add lines as shown in the below image in order to ignore the “bin” and “obj” directory.
Some of the Rules for putting the pattern in the .gitignore file are:
1. Git ignores the blank lines.
2. Git ignores all the lines starting with “#”. A line starting with a hash symbol is treated as a comment.
3. A line or pattern ending with “/” forward slash to specify a directory.
4. A line or pattern staring with “/” forward slash to avoid recursively.
5. An optional prefix i.e., “!” exclamation is used to negate the pattern means if any file is excluded with the above patterns will be included again.
Again, check the status with the “git status -u” command. As you can see the “bin” and “obj” directories are ignored.
Now, add all the untracked files with the “git add .” command to the stage and then commit the files from stage to the “git commit” command. After the commit, use the “git status” command. No files are pending for the commit.
How to Ignore files which are already tracked by Git?
If a file that is already tracked by Git, is added to the .gitignore file, Git however not ignore that as it’s already tracked earlier by Git. To ignore it, we need to remove the file from the tracking with the “git rm” command.
Let's add the “SampleProject/Program.cs” file in the .gitignore file which is already tracked by Git. Do some changes in the “Program.cs” file.
On checking the status with “git status -u”, it still shows that changes have been done in the program.cs file as this file is already tracked by Git. Remove the file from the Git Repository with the “git rm <File_Name> --cached” command. On checking the status again, It shows that the file is deleted from the git repository (not from the local file system as we used the –cached flag). Commit the changes to remove the file from tracking. Now the program.cs file is not tracked by the git and ignored as well.
If we again remove the pattern i.e. “SampleProject/Program.cs” from the .gitignore file, and check the status again then it will be shown in the untracked files.