I need to clean up my .gitignore file. I run some commands and then go through the .gitignore file and delete any line that isn't needed anymore.
The command finds all files in a tree, runs git's check-ignore, greps out only matches that are in my .gitignore file, cleans up the line, and then uniquely sorts the results. Any rule that doesn't appear in this output is a candidate for removal.
find . -type f -not -path .git -exec git check-ignore -v '{}' \; | grep '^.gitignore:' | sed 's/.*:\(.*\)\t.*$/\1/' | sort -u >> .gitignore
This command can take quite some time, especially in repositories with a lot of files and mounted on an NFS.
Is there a simpler method that will show me rules that are not matched by any file in the tree?
You can use git status --ignored=matching to get a list of the ignored file that matches a pattern in .gitignore
.
git status -s --ignored=matching | grep "^\!\!" | awk '{print $2}' | xargs git check-ignore -v | grep '^.gitignore:' | sed 's/.*:\(.*\)\t.*$/\1/' | sort -u >> .gitignore