#6 .gitignore – Ignoring files in git

.gitignore is a file that is used to ignore some files which are less important. Suppose if we are working on an application that creates log files while running, like error.log,system-crash.log, etc. These log files are not that much important while maintaining version history so to untrack them by git you can add their name to .gitignore. So in this post, we’ll see how to ignore files using .gitignore.

  1. Ignoring Files With .gitignore

First we’ll create one error.log file and then using .gitignore we’ll see how to ignore it to getting tracked by git.

In above image I have created a blank error.log file using linux command i.e. touch. touch followed by file name creates blank file with given particular name.

Now if I run git status then git will show me error.log as untracked file as shown below.

Now, create .gitignore file using touch command.

Go to file explorer where your .gitignore file is present, open it with notepad and write error.log over there then save and close it.

Now if I run git status command then it’ll show following result.

Git status shows us only the .gitignore file as an untracked file because we created it after the last git status. Also, the git status is not showing the error.log file because we added it in the .gitignore file so the error.log file will be there but any changes in it will not affect git.

Now if there are more than one .log files like below then we can avoid them by using regular expressions.

If our software is creating more than one log file then instead of adding one one file name in .gitignore use regular expression to add all of them in one single line as shown below.

Here *.log means git will avoid each and every file whose extension is .log. So if I run git status command again then it shows me following result.

It shows only .gitignore as Untracked file. Now add this .gitignore file into staging area and then commit the changes.

Now suppose I made a change in any of the log files and then run the git status command it’ll not affect the output, it means that the .gitignore file is ignoring the files with .log extensions.

  1. Ignoring Folders With .gitignore

While software development we not only need to ignore files but need to ignore folders also. So in order to ignore those folders we need to add their name into the .gitignore file. I have created a new folder as Static, now if run git status it shows the following result.

It is showing Static folder as Untrack file.

To ignore the folder add it’s name followed by slash( / ). After that, if I fired the git status command then the .gitignore will be shown as modified as we have added the Static folder name into the .gitignore file. Slash(/) is added because we are ignoring the folder.

Leave a Comment

Your email address will not be published. Required fields are marked *