Git

Git Command to Show Which Specific Files are Ignored by .gitignore

Git allows users to push their local content to the GitHub repository and pull the remote content to the local directory. During these operations, sometimes, users want to keep their sensitive or specific files private. In this situation, Git users are permitted to ignore specific files explicitly. Whenever they want to view the list of ignored files, different Git commands are available.

This article will explain:

How to Ignore Particular Git Files?

In order to ignore specific files in Git, first, navigate to the local repository and select the file that needs to be ignored. Then, add that particular file to the .gitignore file. After that, run the “git rm –cached <file-name>” command to delete the file from the local directory. Lastly, commit all new changes.

Step 1: Move to Particular Git Directory

First, use the “cd” command and switch to the desired directory:

$ cd "C:\Git\new_repos"

 

Step 2: View List of Files

Next, check out the list of available files in the repository:

$ ls

 
As it can be observed that there are three files in the current repository. Select the one that must be ignored. For instance, we have selected the “new.txt” file:


Step 3: Add File to .gitignore

Now, add the selected file to the .gitignore folder using the “echo” command:

$ echo new.txt >> .gitignore

 

Step 4: Delete File From Repository

Then, run the “git rm” command along with the file name to remove that file from the repository:

$ git rm --cached new.txt

 
Here, the “–cached” option is used to delete the file from the local repository only. However, it will exist in the working directory as an ignored file:


Step 5: Commit Changes

Lastly, save all the added changes using the “git commit” command along with the desired message:

$ git commit -m "Start ignoring new.txt"

 

How to Display Particular Files that are Ignored by .gitignore?

Git provides multiple commands to check which particular files are ignored by .gitignore, such as:

    • git check-ignore * or git check-ignore -v *
    • cat .gitignore
    • git status –ignored
    • git clean -ndx

Method 1: Show Specific Files Ignored by .gitignore Using “git check-ignore *” Command

To view the list of all ignored files in the directory, execute the below-stated command:

$ git check-ignore *

 
Here, it can be seen that the “new.txt” file is the ignored file in the directory:


Furthermore, to check the ignore rule, add a “-v” option with the previous command:

$ git check-ignore -v *

 
The below output indicates that the file is ignored by the “.gitignore” rule:

Method 2: Show Specific Files Ignored by .gitignore Using “cat .gitignore” Command

Run the following command to show the specific file name that is ignored by .gitignore:

$ cat .gitignore

 

Method 3: Show Specific File Ignored by .gitignore Using “git status –ignored” Command

Another way to check if there are ignored files in the directory, check the Git status along with the “–ignored” option:

$ git status --ignored

 
Here, the “–ignored” flag is used to show ignored files:

Method 4: Show Specific File Ignored by .gitignore Using “clean -ndx” Command

The last but not least command to list the ignored files is the “git clean” command along with the “-ndx” option:

$ git clean -ndx

 

We have explained the process of ignoring files in Git and demonstrated different methods to show specific ignored files in Git.

Conclusion

Git allows users to ignore sensitive information or specific files in the repository through the .gitignore, which is the file/folder with an extension or specific file name. Whenever users need to view the specific files that are ignored by .gitignore, they can use multiple commands, such as “check-ignore *”, “git status –ignored”, and many more. This blog illustrated the method of showing ignored specific files.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.