git check-ignore -v filename
See the man page for more details.
Original answer follows:
git does not currently provide anything like this. But after seeing your question I did some googling and found that back in 2009 this feature was requested and partially implemented. After reading the thread, I realised it would not be too much work to do it properly, so I have started work on a patch and hope to have it finished in the next day or two. I will update this answer when it is ready.
UPDATE: Wow, that was a lot harder than I expected. The innards of git
's exclude handling are quite cryptic. Anyway, here's an almost finished series of commits which apply to today's upstream master
branch. The test suite is 99% complete, but I haven't finished handling of the --stdin
option yet. Hopefully I'll manage that this weekend, and then submit my patches to the git mailing list.
In the meantime, I'd definitely welcome testing from anyone who's able to do so - just clone from my git
fork, check out the check-ignore
branch, and compile it as normal.
UPDATE 2: It's done! Latest version is on github as per above, and I have submitted the patch series to the git mailing list for peer review. Let's see what they think ...
UPDATE 3: After several more months of hacking / patch reviews / discussions / waiting, I'm delighted to be able to say that this feature has now reached git's master
branch, and will be available in the next release (1.8.2, expected 8th March 2013). Here's the check-ignore
manual page. Phew, that was way more work than I expected!
UPDATE 4: If you're interested in the full story about how this answer evolved and the feature came to be implemented, check out episode #32 of the GitMinutes podcast.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…