Skip to content

Instantly share code, notes, and snippets.

@hieblmedia
Last active April 17, 2026 19:07
Show Gist options
  • Select an option

  • Save hieblmedia/9318457 to your computer and use it in GitHub Desktop.

Select an option

Save hieblmedia/9318457 to your computer and use it in GitHub Desktop.
Gitignore - Exclude all except specific subdirectory
#
# 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/
@Steampunkery
Copy link
Copy Markdown

You sir are a god

@sandboxcoder
Copy link
Copy Markdown

Yo, thanks!

@abaines
Copy link
Copy Markdown

abaines commented Aug 3, 2018

Thanks! This is awesome!

@avrahamappel
Copy link
Copy Markdown

Thanks!

@FabianUx
Copy link
Copy Markdown

Thanks

I removed one star for it to work

!/templates/
/templates/*
!/templates/myTemplate/

@CodeConnects
Copy link
Copy Markdown

+1 to FabianUx

Did not work for me with two asterisks, but is perfect with only one. Thanks!

@tonyerskine
Copy link
Copy Markdown

Thanks!

@dawidjaja
Copy link
Copy Markdown

+1

@chip-edw
Copy link
Copy Markdown

Thanks. This worked for me.
After hours of reading confusing posts, I was very happy to find this.

@ilmoralito
Copy link
Copy Markdown

thanks for sharing

@kalekachali
Copy link
Copy Markdown

Thank you sir. You is a genius!

@felixpenrose
Copy link
Copy Markdown

Thanks. This worked for me.
After hours of reading confusing posts, I was very happy to find this.

Ditto - most posts were confusing and missed key information regarding negation, finally this helped me realise where I was going wrong. 👍

@joaolisboa
Copy link
Copy Markdown

If the affected folder is not in the root level of the .gitignore, the solutions above didn't work, however this does:

! templates/
**/templates/*
!**/templates/myTemplate

This is exactly what I needed and worked for me. Thanks!

@anisriva
Copy link
Copy Markdown

anisriva commented Oct 7, 2021

@hieblmedia

doesnt work in my case
image

@martin-braun
Copy link
Copy Markdown

martin-braun commented Oct 8, 2021

@anisriva have you read the comments above?

@anisriva
Copy link
Copy Markdown

anisriva commented Oct 8, 2021

@

@anisriva have you read the comments above?

@martin-braun yes i did, and i specifically tried yours as well it seem to only work for the immediate child directory.

@TommyQC
Copy link
Copy Markdown

TommyQC commented Apr 19, 2022

Thanks

I removed one star for it to work

!/templates/
/templates/*
!/templates/myTemplate/

Worked for me Thanks!

@leandrosoares6
Copy link
Copy Markdown

+1 to FabianUx

Did not work for me with two asterisks, but is perfect with only one. Thanks!

Thanks bro!

@lucaslgr
Copy link
Copy Markdown

Thankssss !!!! helped me a lot!

@5c077yP
Copy link
Copy Markdown

5c077yP commented Sep 6, 2023

@

@anisriva have you read the comments above?

@martin-braun yes i did, and i specifically tried yours as well it seem to only work for the immediate child directory.

For me worked to re-include every individual parent again, meaning

/**
!/mnt
!/mnt/cassandra
!/mnt/cassandra/analytics-seed
!/mnt/cassandra/analytics-seed/spark
!/mnt/cassandra/analytics-seed/spark/jupyter
...

@taljacob2
Copy link
Copy Markdown

Thank you!
I wanted to share my example, maybe it would help someone.

My .gitignore:

# Disable all directories.
*/
!.gitignore

# Enable `Jenkins Service/init.groovy.d`.
!Jenkins Service/
Jenkins Service/*
!Jenkins Service/init.groovy.d/

# Enable `Scripts`.
!Scripts/

After git add .:

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   .gitignore
        new file:   Jenkins Service/init.groovy.d/BuildRerunner.groovy
        new file:   Jenkins Service/init.groovy.d/rerun-last-prod-build.groovy
        new file:   Jenkins Service/init.groovy.d/rerun-zombie-builds.groovy
        new file:   Scripts/jenkins-delete-cache.bat

@Wanying-Zhu
Copy link
Copy Markdown

Thanks for the solution!
In my case git add * must be used after configuration of the gitignore file. Somehow git add . does not work.

@irwineguiluz
Copy link
Copy Markdown

Awesome, thanks!

@rhoboat
Copy link
Copy Markdown

rhoboat commented Apr 17, 2026

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_Store

After 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