Hello I have an git repository with an existing .gitignore file. Something seems off since some of the paths and/or files in the .gitignore file are under source control.
To clean that up I was looking for a way to list all files that are under source control that should be ignored because they are included in the .gitignore file.
There is no general way in saying if the files needs to be ignored or included so I need this list to review every file manually.
Does anyone know a way how to do that?
Example: This is very simplified, we are talking about 1000 of files here. A folder under source control have 4 files in it
Contents of .gitignore
.gitignore
test.*
test.cpp is already under source control so changes are tracked. This is how it should be. However test.cpp is clearly included in the .gitignore file. Therefore I like to modify the gitignore file so that it contains
.gitignore
test.*
!test.cpp
so what I want is a list that shows me that test.cpp is under source control while gitignore is exluding it so that I can modify the gitignore file to exactly reflect the current state.
The result of the list in this example should be.
In my more complex scenario this should also work with files in subfolders.
I managed to find a solution for myself (I think).
It looks like this command does exactly what I intended to do:
comm -12 <(git ls-files | sort) <(find . | cut -sd / -f 2- | sort | xargs git check-ignore --no-index)
comm -12 <(input list 1) <(input list 2)
: a very useful command, I should remember that one, excludes files only present in one of the input lists, so only files are listed that are present in both lists.
git ls-files
: shows all files under git control.
find .
: shows all files in the current folder and subfolders.
cut -sd / -f 2-
: since find
prints ./
in front of every filepath we need to get rid of that since git ls-files
does not. Basically we tell cut
that /
is a delimiter. cut
will then split the file path into parts. -f 2-
tells cut to output every file path starting from the second part to the end. Since the first part is .
this is exactly what I wanted.
sort
: since comm needs sorted lists to function (no worry - it will tell you if the lists are not sorted) we sort the list.
xargs git check-ignore --no-index
: For each file I run git check-ignore
with the xargs
tool. --no-index
is very important here because if not present check ignore would not return files that are covered by gitignore but already under source control.
I hope this is somewhat useful to someone like me who came to an old big git repository and has to clean up the gitignore file.