Check whether a file is ignored by git

Sometimes there is the necessity to check whether a file is ignored by git or not. There could be many reasons for this; in most cases, this check is related to a sophisticated nested rule or even a sequence of rules that are hard to grasp at first glance. Let’s see how git can help us.

To help us accomplish this task, git provides us with the check-ignore command. To get more information about this command please refer to the man page.

For example, we want to exclude all .idea folders in our project. By analyzing various projects, we can find different rules in the .gitignore file that vary between .idea and .idea/. Let’s add the second rule to the .gitignore file and run the command:

$ git check-ignore build/.idea/
build/.idea/

The output proves that a nested .idea folder is ignored. In addition, git provides another very useful -v (verbose) option. Instead of printing the excluded path, the command will print the exact rule that made the exclusion and its line number. How cool is that!

$ git check-ignore build/.idea/ -v
.gitignore:1:.idea/           build/.idea/

Comment this page: