Track file inside ignored directory
when I need track files like this, or only directory structures, I add something like this to my .gitignore file
resources/**/*.*
which means, "ignore all files with extension inside the resources directory including subdirectories".
Then I put an "empty" file inside those directories I want to keep tracked.
Note that only extensionless files are going to be tracked, and that's why this works.
with the option listed above, you can now add this:
!resources/my_needed_file.md
and that's another solution that not includes forced files, but is only an option if you are using files with extensions.
To add to ".gitignore
exclude folder but include specific subfolder", one good way to debug those .gitignore file is to use git check-ignore
(Git 1.8.4+):
git check-ignore -v my_folder/my_file.md
You would see it is still ignored because of the my_folder/
rule.
That is because it is not possible to re-include a file if a parent directory of that file is excluded.(*
)
(*
: unless certain conditions are met in git 2.?+, see below)
That is why ignoring the files within that folder (my_folder/*
, instead of the folder itself) allows you to exclude one.
Of course, you can force adding a file ignored (git add -f my_folder/my_file.md
), but that is not the point of this answer.
The point is to explain why adding !my_folder/my_file.md
in .gitignore
doesn't work with git 2.6 or less.
Note that with git 2.9.x/2.10 (mid 2016?), it might be possible to re-include a file if a parent directory of that file is excluded if there is no wildcard in the path re-included.
Nguyễn Thái Ngọc Duy (pclouds
) is trying to add this feature:
- commit 506d8f1 for git v2.7.0, reverted in commit 76b620d git v2.8.0-rc0
- commit 5e57f9c git v2.8.0-rc0,... reverted(!) in commit 5cee3493 git 2.8.0-rc4.
So here, with git 2.8+, this would work:
/my_folder
!my_folder/my_file.md
A file will not show up as "change to be committed" if it is not yet tracked (IOW was never added before). Simply add the file with git add
, and Git will track the file only, but still ignore the folder.
git add -f ignored_folder/my_file
The -f
flag is important, since git will refuse adding files from ignored paths.