Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

You sir are a god

@sandboxgod
Copy link

Yo, thanks!

@abaines
Copy link

abaines commented Aug 3, 2018

Thanks! This is awesome!

@avrahamappel
Copy link

Thanks!

@FabianUx
Copy link

Thanks

I removed one star for it to work

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

@CodeConnects
Copy link

+1 to FabianUx

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

@tonyerskine
Copy link

Thanks!

@dawidjaja
Copy link

+1

@chip-edw
Copy link

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

@ilmoralito
Copy link

thanks for sharing

@kalekachali
Copy link

Thank you sir. You is a genius!

@felixpenrose
Copy link

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. 👍

@martin-braun
Copy link

martin-braun commented Oct 9, 2020

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

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

@joaolisboa
Copy link

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!

@martin-braun
Copy link

martin-braun commented Jun 11, 2021

And here is how to exclude only one folder in the root:

/**
!/.gitignore
!/dir
!/dir/**/*

@anisriva
Copy link

anisriva commented Oct 7, 2021

@hieblmedia

doesnt work in my case
image

@martin-braun
Copy link

martin-braun commented Oct 8, 2021

@anisriva have you read the comments above?

@anisriva
Copy link

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

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

+1 to FabianUx

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

Thanks bro!

@lucaslgr
Copy link

Thankssss !!!! helped me a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment