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

This comment has been minimized.

Copy link

@Steampunkery Steampunkery commented Feb 11, 2018

You sir are a god

@sandboxgod

This comment has been minimized.

Copy link

@sandboxgod sandboxgod commented Jul 12, 2018

Yo, thanks!

@abaines

This comment has been minimized.

Copy link

@abaines abaines commented Aug 3, 2018

Thanks! This is awesome!

@avrahamappel

This comment has been minimized.

Copy link

@avrahamappel avrahamappel commented Aug 23, 2018

Thanks!

@FabianUx

This comment has been minimized.

Copy link

@FabianUx FabianUx commented Aug 23, 2018

Thanks

I removed one star for it to work

!/templates/
/templates/*
!/templates/myTemplate/
@CodeConnects

This comment has been minimized.

Copy link

@CodeConnects CodeConnects commented Nov 17, 2018

+1 to FabianUx

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

@tonyerskine

This comment has been minimized.

Copy link

@tonyerskine tonyerskine commented May 30, 2019

Thanks!

@dawidjaja

This comment has been minimized.

Copy link

@dawidjaja dawidjaja commented Jun 4, 2019

+1

@chip-edw

This comment has been minimized.

Copy link

@chip-edw chip-edw commented Jul 14, 2019

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

@ilmoralito

This comment has been minimized.

Copy link

@ilmoralito ilmoralito commented Aug 2, 2019

thanks for sharing

@kalekachali

This comment has been minimized.

Copy link

@kalekachali kalekachali commented Oct 13, 2019

Thank you sir. You is a genius!

@felixpenrose

This comment has been minimized.

Copy link

@felixpenrose felixpenrose commented Jun 16, 2020

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

This comment has been minimized.

Copy link

@martin-braun martin-braun commented Oct 9, 2020

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
@joaolisboa

This comment has been minimized.

Copy link

@joaolisboa joaolisboa commented Mar 13, 2021

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!

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