Last Updated on January 30, 2021 by sandeeppote

When any changes made to a file in working folder it is been tracked by Git to add that to staging and to repository.

There are some files which we dont want to track for e.g. log files. This are constatntly changed and hence git shall always ask to track the changed file.

So this files dont need to be tracked and that can be done by a magic file .gitignore.

.gitignore file sit in a project folder so the files in folders and subfolders can be ignored for specific project. Although the files also can be set to ignore globally, but its best to have .gitignore file at project level.

.gitignore file can have a list of files that can be ignored. We may use regular expressions to ignore the file with specific extension. For e.g. *.log

So all files with log extension wont be tracked and git wont keep poping if there should be any change in file.

We may also negate expression to track files. for e.g. ignore all files with html extension but not index.html. For ths .gitignore file should have following-

*.html

!index.html

Note that .gitignore is also a file, so git shall track this file. This file has to be commited to repositoy so that team members shall have the same fileignoring rules to the project they are working.

How to track empty folders?

Note that git does not track empty folders. It can only track files. If wish to have a empty folder in your project for any valid reasons. for e.g. log folder, so everybody in project shall have log folder but not necessary that the log files should be tracked. In this case the trick to track the folder woud be to creats a temp file which then git shall track an hence the folder itself will be tracked. Normally it best to create a .gitkeep file. This file dobt need to have any content, its just that foder now can be tracked.

Ignoring tracked files

Suppose you have a situation where the file is commited to repository but now you dont want to track this anymore. For e.g. log file. Although every developer may need to have a log file hich was create and tracked in intial commit but then once the file is in repository any logs on each developers log dont needto be puahed. So in this case the fie that is tarcked and commited shouldnt be tracked anymore. So to ignore tracked file use following-

 git rm --cached file.txt

This will remove the file fom staging and won be tracked further

Hope this helps 🙂