Some time ago I set up my .gitignore
file to not track a folder my_folder
with:
my_folder/
Now I want to track only a given file inside said folder, named my_file.md
. After making .gitignore
look like this:
my_folder/ !my_folder/my_file.md
and checking:
git status
the file does not appear as a change to be committed.
What am I doing wrong?
Add
I tried changing my .gitignore
file to:
my_folder/* !my_folder/my_file.md
as advised but the file is still not showing up as a change to commit after a git status
. Do I need to reset something?
Add 2
Attempting to add the file with git add my_folder/my_file.md
returns:
The following paths are ignored by one of your .gitignore files: my_folder/my_file.md Use -f if you really want to add them. fatal: no files added
The command git check-ignore -v my_folder/my_file.md
gives:
.gitignore:1:my_folder/* my_folder/my_file.md
In order to start tracking previously ignored files or patterns: First, you need to change your working directory to the repository by typing cd <path> . Second, you need to specify the files or patterns to track them by typing git add -f <file name>. extension for a specific file or git add -f *.
Yes, you can track the . gitignore file, but you do not have to. The main reason of having this file into repository is to have everyone working on the project, ignoring same files and folders. Also see this: Should you commit .
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.
A .gitignore file is a plain text file that contains a list of all the specified files and folders from the project that Git should ignore and not track. Inside .gitignore , you can tell Git to ignore only a single file or a single folder by mentioning the name or pattern of that specific file or folder.
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:
So here, with git 2.8+, this would work:
/my_folder !my_folder/my_file.md
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