3

Note:

This is not about fixing like git rm -r --cached . && git add . I actually want to be able to list the files only.

Why:

Sometimes when I edit my .gitignore and forget the above command I end up with files that are still in the repository, but should have been ignored.

What:

What I want to accomplish is to write a shell script, that is able to list all files on my filesystem that should have been ignored, but are still present.

Outcome:

In the end, I want to implement it to a continuous integration server, that is going to check if the rules from .gitignore match the files that are inside the repository.

Is there any way to parse the .gitignore by hand?

lockdoc
  • 1,221
  • 1
  • 15
  • 27
  • If python is acceptable (or at least usable as inspiration) then [this question](http://stackoverflow.com/q/25229592/258523) might be of some help. – Etan Reisner Oct 21 '15 at 16:37

2 Answers2

9
git ls-files --cached --ignored --exclude-standard
jthill
  • 42,819
  • 4
  • 65
  • 113
  • Completely forgot about ls-files. This is the best all `git` solution to this question. – wspurgin Oct 21 '15 at 17:06
  • I have `git config --global alias.ls ls-files --exclude-standard` :-) – jthill Oct 21 '15 at 17:09
  • Nice, I'll definitely be adding an alias for this. Thanks for sharing :) – wspurgin Oct 21 '15 at 17:42
  • For this, my output is empty. I know I have a lot of local files which are being ignored and are not in the repository. But the command returns with no results – lockdoc Oct 21 '15 at 18:49
  • 1
    The `--cached` option says to only list indexed aka cached aka staged aka added aka tracked paths. If this returns no results, no tracked paths match an ignore pattern. – jthill Oct 21 '15 at 21:28
  • would love a one liner that then deletes these files. – escapecharacter Sep 14 '20 at 19:41
-1

You should look at the git check-ignore --help You can use it to check if a path should be included or excluded. From my understanding, to do what you want you should use the --no-index option.

That's at least the git side of the process.

The shell side of the process would involve more. You have many options there. You could use find to list all files and pipe them git check-ignore and based on the output of that create a list of files that should have been excluded.

That's my 2¢ anyway.

Here's an SO question that covers something similar Git command to show which specific files are ignored by .gitignore

Community
  • 1
  • 1
wspurgin
  • 1,957
  • 1
  • 12
  • 19