Git - Unignore a subfolder
Today I found somethings new to unignore a subfolder in git.
I have a git folder with this structure:
├── Personal
└── Work
├── project1
│ ├── docs
│ │ └── doc.md
│ └── test.md
└── project2
└── test.md
and now, I want to unignore Work/project1/docs/
only. (every files and other subfolders in Work
still be ignore). So, what should I do?
In .gitignore
file, I tried some rules like that:
# 1
Work/
!Work/project1/docs/
# 2
Work/*
!Work/project1/docs/*
...
or swap those lines. But it doesn’t work.
After searching, I found 2 interesting things:
- You have to “unignore” every parent directory of anything that you want to “unignore”: Ignore everything in this directory, but not some certain subdirectory.
Work/*
!Work/project1/
Work/project1/*
!Work/project1/docs/
The trailing
/*
is significant.If you write
Work/
in gitignore, git will understand that you want to:- Ignore
Work
folder itself - Ignore everything in
Work
folder.
=> If you write
unignore
rule below (eg:!Work/project1
), git will do nothing, cause it never looks at Work folder, so it can not apply unignore rule to any subfolders- Ignore
If you write
Work/*
in gitignore, git will understand that you want to:- Keep folder
Work
- Ignore everything in
Work
folder.
=> Git can apply unignore rule to subfolders cause I can look at
Work
- parent dir.- Keep folder