How does one get a list of those files that match a rule in .gitignore file, but that have been staged or committed in the past?
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 .
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.
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.
Ignored files are usually build artifacts and machine generated files that can be derived from your repository source or should otherwise not be committed. Some common examples are: dependency caches, such as the contents of /node_modules or /packages. compiled code, such as .o , .
The documentation to ls-files
is not exactly clearly written, but it appears that the following simple alias does the job:
git config --global alias.showtrackedignored "ls-files -i --exclude-standard"
The above command creates an alias called showtrackedignored
. To use, run:
git showtrackedignored
and it will list all of the files in the current directory and subdirectories that are tracked but would be ignored if they weren't tracked.
git ls-files
Unfortunately, this doesn't work 100% reliably. Apparently Git does a good job of finding files that should not be ignored, but when searching for files that are ignored (the -i
option to git ls-files
), it doesn't list ignored files inside a directory if it's the directory that matches the ignore rules.
To work around this bug, try converting your ignore rules so that only files are matched, not directories (this isn't always possible).
(Thank you Christoph for discovering this bug and reporting it to the Git mailing list! Edit: A patch is in the works now and will make it into git 1.7.11.2 or later)
Here's a different approach. It's far more complicated and might have broken corner cases.
git config --global alias.showtrackedignored '! cd "${GIT_PREFIX}" && untracked_list=$(git rev-parse --git-dir)/ignored-untracked.txt && git ls-files -o -i --exclude-standard >"${untracked_list}" && GIT_INDEX_FILE="" git ls-files -o -i --exclude-standard | grep -Fvxf "${untracked_list}" && rm -rf "${untracked_list}"'
The alias does the following:
cd
back to the directory where git showtrackedignored
was run from (Git runs shell-based aliases from the toplevel directory, not the current directory; see the section on alias.*
in git help config
)untracked_list
. This variable holds the path to a temporary file that will contain the list of currently ignored files. This temporary file is in the .git
directory.${untracked_list}
.grep
, which filters out the files that were written to ${untracked_list}
.${untracked_list}
.Drawbacks to this approach:
.git
directory.grep
.It also suffers from the same bug as the former alias.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With