How do I tell Git to ignore everything except a subdirectory?

GitGitignore

Git Problem Overview


I want to ignore all files in my repository except those that occur in the bin subdirectory. I tried adding the following to my .gitignore:

*
!bin/*

This does not have the desired effect, however: I created a new file inside of bin/, but doing git status still shows nothing to commit (working directory clean).

Any suggestions?

Git Solutions


Solution 1 - Git

This ignores root files & root directories, then un-ignores the root bin directory:

/*
/*/
!/bin/

This way you get all of the bin directory, including subdirectories and their files.

Solution 2 - Git

Here how to ignore everything exept one directory "MY_SUPER_DUPER_TEMPLATE_directory" in some directory

Structure: /bitrix/templates/MY_SUPER_DUPER_TEMPLATE_directory

/*
!/bitrix
/bitrix/*
!/bitrix/templates
/bitrix/templates/*
!/bitrix/templates/MY_SUPER_DUPER_TEMPLATE_directory
*.DS_Store
*.gitignore

Solution 3 - Git

You must exclude everything on the way to the destinations, but you must include the destinations:

Note: This is an exclusion file (i.e. .gitignore) so the logic is inverted. Ignore all, except the tsp directory, ignore all in tsp directory, except the src directory...

/*
!/tsp
/tsp/*
!/tsp/src
/tsp/src/*
!/tsp/src/*.h
!/tsp/src/*.cpp
!/tsp/src/data.txt
!/.gitignore

Solution 4 - Git

The only issue you have is that the bin directory itself is not matched by the bin/* pattern so git isn't even look in the bin directory.

There are two solutions that spring to mind.

.gitignore :

*
!/bin/
!bin/*

or

.gitignore :

*
!/bin/

bin/.gitignore :

!*

I prefer the second solution as the first solution won't stop ignoring files in the bin directories that are in subdirectories that aren't called bin. This may or may not matter in your situation.

Solution 5 - Git

From the official git doc, one of the examples says:

Example to exclude everything except a specific directory foo/bar (note the /* - without the slash, the wildcard would also exclude everything within foo/bar):

$ cat .gitignore
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar

For an explanation about the leading slash: When to use leading slash in gitignore.

Solution 6 - Git

Try following in latest GIT version.

*
!*/
!/path/to/your/dir/**

Solution 7 - Git

I think a better way would be to anchor each pattern to the top git directory by starting the pattern with a slash:

/*
!/public_html
!/.gitignore

Instead of ignoring all files it will only ignore top level files, not the ones in the directory you dont want to ignore.

Solution 8 - Git

This .gitignore works for me:

*/
/*
!bin/

Solution 9 - Git

try this obscure git bug.

!bin**/**

Hope it helps :)

Supporting docs git bug sample

PS: try it first before commenting.

Solution 10 - Git

I had to do this recently:

*
!sub-dir/
!sub-dir/*

I'm not sure why I need two lines for to exclude one folder, but this is what worked for me and macos.

Solution 11 - Git

Late to the party, but none of the answers worked for me "out of the box". This one does:

* 
!bin
!bin/**
  • the first line ignores everything
  • the second line includes back "bin" directory itself
  • the last line includes back entire content of the "bin" directory no matter how nested it is (note double asterisk),

Solution 12 - Git

Here is my solution. In my scenario I had a folder with subfolders as follow:

  • database (main folder)
    • conf (subfolder)
    • drivers (subfolder)
    • jars (subfolder)
    • sql (subfolder)

And from the database folder I wanted to push the sql folder only so my .gitignore file was as follow:

database/*
!database/sql/

Where the first line simply say ignore all subfolder(s) of the database folder and the second line meaning since you are ignoring all subfolder(s) of the database folder exclude(don't ignore) the sql subfolder

Solution 13 - Git

To me it turns out I should not ignore the parent folder but files and folders in it using /*

-- **/node_modules

++ **/node_modules/*

then

++ !node_modules/@my-package/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichael GoerzView Question on Stackoverflow
Solution 1 - GitTyler LaingView Answer on Stackoverflow
Solution 2 - GitAndrii TishchenkoView Answer on Stackoverflow
Solution 3 - GitnikcView Answer on Stackoverflow
Solution 4 - GitCB BaileyView Answer on Stackoverflow
Solution 5 - Gitsolstice333View Answer on Stackoverflow
Solution 6 - GitKirbyView Answer on Stackoverflow
Solution 7 - GitJakeView Answer on Stackoverflow
Solution 8 - GitTrianamView Answer on Stackoverflow
Solution 9 - GitdavidView Answer on Stackoverflow
Solution 10 - GitHenryView Answer on Stackoverflow
Solution 11 - GitgreenoldmanView Answer on Stackoverflow
Solution 12 - GitSenzoView Answer on Stackoverflow
Solution 13 - GitMazwi ThwalaView Answer on Stackoverflow