Last active
April 17, 2026 19:07
-
-
Save hieblmedia/9318457 to your computer and use it in GitHub Desktop.
Gitignore - Exclude all except specific subdirectory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # If all files excluded and you will include only specific sub-directories | |
| # the parent path must matched before. | |
| # | |
| /** | |
| !/.gitignore | |
| ############################### | |
| # Un-ignore the affected subdirectory | |
| !/libraries/ | |
| # Ignore subdirectory and all including directories and files to match pattern as valid for the next pattern | |
| /libraries/** | |
| # This pattern only works with the two previous patterns | |
| # (i remember it was working alone on older git versions) | |
| !/libraries/myLibrary/ | |
| ############################### | |
| # Another example | |
| !/templates/ | |
| /templates/** | |
| !/templates/myTemplate/ |
Thanks for the solution!
In my case git add * must be used after configuration of the gitignore file. Somehow git add . does not work.
Awesome, thanks!
In case it helps anyone else, this is what I needed for my case.
- Need to ignore everything at the root, including files and directories
- Except certain directories
- And certain subdirectories of those directories
- But ignore DS_Store in those directories
It didn't work to use ** or * to ignore everything and then unignore. it also didn't work to unignore subdirectories with their full relative path.
# ignore directories
*/
# ignore files at the root
/*
#----
# allow these
!.gitignore
!dir1
!dir2
!dir3
# .obsidian is subdir of the above dir1, dir2, dir3. but must be referenced like this
!.obsidian
# plugins is subdir of dir1/.obsidian, dir2/.obsidian, dir3/.obsidian
!plugins
# these are subdirs of .obsidian/plugins
!obsidian-vimrc-support
!file-diff
# but ignore these
*.DS_StoreAfter getting it right, all I needed was to run git status -u to check what it picked up.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!
I wanted to share my example, maybe it would help someone.
My
.gitignore:After
git add .: