How to Check if gitignore is Working | Show Ignored Files
.gitignore file is a great way to disallow files from being committed to git public or private repositories.
In this blog, we will explain to you how to validate the .gitingnore file by listing the files using
git check-ignore command.
Git Show Ignored Files
Git has an inbuilt command named check-ignore to check if a file is ignored or not.
To validate the .gitignore file, first, we will look at an example .gitignore file. Also, you can take a look at the gitignore templates for reference.
*.log *.secret #ignore a specific file /app/access.log # Ignore the node_modules directory node_modules/ # Ignore the build directory /dist # The file containing environment variables .env
Now let’s look at different scenarios using the above
.gitignore file as an example.
List all git ignored file in a Repository
Execute the following command to check all the ignored files in a repository.
git check-ignore -v **/*
You will get an output with the rule and the file location as shown below.
List all git ignored file in a Directory
To check all the ignored files by git in the directory, first
cd into the directory.
Then execute the following command.
git check-ignore *
To show the ignore rule, add a -v
git check-ignore -v *
Check if a single File is ignored by git
Sometimes we might need to check if a single file is ignored by git.
To do this, execute the check-ignore command with the file path. It will output the file name if it is ignored by git.
git check-ignore app/access.log
➜ gitignore git:(master) ✗ git check-ignore app/access.log/ app/access.log/
fatal: no path specified Error [check-ignore]
When using the
check-ignore command, if you don’t specify a regular expression or a path with the command, it will throw no path specified Error
Either execute the command with a regular expression or add the directory or file path.