What should I put in my Git repository's .gitignore
to exclude all files and directories there except specific ones. For example if I have a large number of files and directories in a repo, and just want Git to track fileX
, .fileY
, and directoryZ
, but completely ignore everything else, what should be in my .gitignore
?
Asked
Active
Viewed 1,746 times
3

David Neiss
- 8,161
- 2
- 20
- 21

orome
- 45,163
- 57
- 202
- 418
-
@s-hunter: The answer below seems a lot better than any of those (if I understand it). How do we close this as a duplicate question, but highlight that answer? – orome Jul 30 '16 at 12:40
-
My intension is to link this question to the other one that's essential the same but worded differently. So that people can find this question and the other question using the words you used. Thus I commented it but didn't flag it. – s-hunter Jul 30 '16 at 13:07
-
@s-hunter: Sounds good. I'll accept this answer and mark this as solved by the duplicate so it can be closed. – orome Jul 30 '16 at 13:09
-
None of this old answer mention the rule I mention below. http://stackoverflow.com/a/19023985/6309 is more recent and more accurate for what you want to do. – VonC Jul 30 '16 at 13:17
1 Answers
5
You should ignore content; but exclude folders.
That allows you to exclude files.
/**
!/**/
!fileX
That is because:
It is not possible to re-include a file if a parent directory of that file is excluded.
Check the effect of a .gitignore with git checkignore -v
git check-ignore -v -- afile