Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git command to show which specific files are ignored by .gitignore

Tags:

git

ignore

I am getting my feet wet with Git and have the following issue:

My project source tree:

/ | +--src/ +----refs/ +----... | +--vendor/ +----... 

I have code (currently MEF) in my vendor branch that I will compile there and then move the references into /src/refs which is where the project picks them up from.

My issue is that I have my .gitignore set to ignore *.dll and *.pdb. I can do a git add -f bar.dll to force the addition of the ignored file which is ok, the problem is I can not figure out to list what files exist that are ignored.

I want to list the ignored files to make sure that I don't forget to add them.

I have read the man page on git ls-files and can not make it work. It seems to me that git ls-files --exclude-standard -i should do what I want. What am I missing?

like image 674
Andrew Burns Avatar asked Jan 21 '09 20:01

Andrew Burns


People also ask

How do I see what files are ignored in git?

Git Ignoring Files and Folders Checking if a file is ignored From Git 1.7. 6 onwards you can also use git status --ignored in order to see ignored files. You can find more info on this in the official documentation or in Finding files ignored by .

Does git show ignored files?

The possible options are: traditional - Shows ignored files and directories, unless --untracked-files=all is specified, in which case individual files in ignored directories are displayed. no - Show no ignored files. matching - Shows ignored files and directories matching an ignore pattern.

How do you check if git ignore is working?

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.


2 Answers

There is a much simpler way to do it (git 1.7.6+):

git status --ignored 

See Is there a way to tell git-status to ignore the effects of .gitignore files?

like image 44
Penghe Geng Avatar answered Sep 18 '22 09:09

Penghe Geng


Notes:

  • xiaobai's answer is simpler (git1.7.6+): git status --ignored
    (as detailed in "Is there a way to tell git-status to ignore the effects of .gitignore files?")
  • MattDiPasquale's answer (to be upvoted) git clean -ndX works on older gits, displaying a preview of what ignored files could be removed (without removing anything)

Also interesting (mentioned in qwertymk's answer), you can also use the git check-ignore -v command, at least on Unix (doesn't work in a CMD Windows session)

git check-ignore * git check-ignore -v * 

The second one displays the actual rule of the .gitignore which makes a file to be ignored in your git repo.
On Unix, using "What expands to all files in current directory recursively?" and a bash4+:

git check-ignore **/* 

(or a find -exec command)

Note: https://stackoverflow.com/users/351947/Rafi B. suggests in the comments to avoid the (risky) globstar:

git check-ignore -v $(find . -type f -print) 

Make sure to exclude the files from the .git/ subfolder though.

CervEd suggests in the comments, to avoid .git/:

find . -not -path './.git/*' | git check-ignore --stdin 

Original answer 42009)

git ls-files -i 

should work, except its source code indicates:

if (show_ignored && !exc_given) {                 fprintf(stderr, "%s: --ignored needs some exclude pattern\n",                         argv[0]); 

exc_given ?

It turns out it need one more parameter after the -i to actually list anything:

Try:

git ls-files -i --exclude-from=[Path_To_Your_Global].gitignore 

(but that would only list your cached (non-ignored) object, with a filter, so that is not quite what you want)


Example:

$ cat .git/ignore # ignore objects and archives, anywhere in the tree. *.[oa] $ cat Documentation/.gitignore # ignore generated html files, *.html # except foo.html which is maintained by hand !foo.html $ git ls-files --ignored \     --exclude='Documentation/*.[0-9]' \     --exclude-from=.git/ignore \     --exclude-per-directory=.gitignore 

Actually, in my 'gitignore' file (called 'exclude'), I find a command line that could help you:

F:\prog\git\test\.git\info>type exclude # git ls-files --others --exclude-from=.git/info/exclude # Lines that start with '#' are comments. # For a project mostly in C, the following would be a good set of # exclude patterns (uncomment them if you want to use them): # *.[oa] # *~ 

So....

git ls-files --ignored --exclude-from=.git/info/exclude git ls-files -i --exclude-from=.git/info/exclude  git ls-files --others --ignored --exclude-standard git ls-files -o -i --exclude-standard 

should do the trick.

(Thanks to honzajde pointing out in the comments that git ls-files -o -i --exclude-from... does not include cached files: only git ls-files -i --exclude-from... (without -o) does.)

As mentioned in the ls-files man page, --others is the important part, in order to show you non-cached, non-committed, normally-ignored files.

--exclude_standard is not just a shortcut, but a way to include all standard "ignored patterns" settings.

exclude-standard
Add the standard git exclusions: .git/info/exclude, .gitignore in each directory, and the user's global exclusion file.

like image 191
VonC Avatar answered Sep 18 '22 09:09

VonC