273

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?

Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
Michael Goerz
  • 4,300
  • 3
  • 23
  • 31
  • See also [.gitignore exclude folder but include specific subfolder](http://stackoverflow.com/questions/5533050/gitignore-exclude-folder-but-include-specific-subfolder) – 1615903 Mar 30 '17 at 13:51

13 Answers13

334

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.

Tyler Laing
  • 3,533
  • 1
  • 14
  • 7
  • 31
    For some reason this doesn't work consistently for me. I have the two lines `/stuff/` and `!/stuff/specific/`, but it still ignores /stuff/specific/ – LB-- Nov 18 '13 at 00:00
  • 1
    @LB-- I had a similar issue, and `git check-ignore -v` told me it was because of `~/.gitignore` – gurney alex Aug 25 '14 at 12:32
  • @gurneyalex I found out that it fixes itself if I delete the local repo and re-clone it, it must be a bug. – LB-- Aug 25 '14 at 18:44
  • 4
    Is the /*/ necessary? – Alex Baker Apr 29 '15 at 20:25
  • 3
    This didn't work for me. I had to add `!/bin/` to my root `.gitignore` file, but then additionally I had to add a `/bin/.gitignore` file with `!*`... Making only one of those changes didn't work. It only worked after combining both changes. – Felipe Brahm Mar 18 '16 at 06:06
  • 7
    @LB and others, try `/stuff/*` and `!/stuff/specific/`. The difference is `/stuff/` tells it to ignore the stuff directory itself, `/stuff/*`says ignore all directories in the stuff directory, then don't ignore this specific directory. That made the difference for me. – reads0520 Feb 12 '19 at 16:28
  • To make sure it works, after updating your .gitignore as the above, run `git init`, `git add .`. – bugo99iot May 05 '20 at 11:08
  • @reads0520's comment worked for me. – Nesha25 Apr 22 '22 at 22:41
142

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
Andrii Tishchenko
  • 1,842
  • 2
  • 13
  • 15
  • 3
    This worked wonders for unignoring a laravel vendor package I had modified! Thank you, you saved me lots of headaches – Maria Vilaró May 04 '17 at 07:48
79

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
Sooth
  • 2,834
  • 23
  • 26
  • 11
    +1 for the principle of including a directory, then, excluding it's contents, then, including the desired sub-directory, etc. I ended up using this answer on my projects. – John Jesus Sep 06 '13 at 15:54
  • Fast forward a few years and it kind of feels off that you still have to be this verbose. Specifying the final destination should be enough. – Mig82 Nov 08 '21 at 19:34
45

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.

Meetai.com
  • 6,622
  • 3
  • 31
  • 38
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • 2
    Didn't work for me. I have to have a .gitignore like this to make it work: `# Ignore everything * # But not these files... !/wp-content/ !/wp-content/plugins/ !/wp-content/plugins/my_plugin/ !/wp-content/plugins/my_plugin/*` No block code in comments? – xlttj Feb 28 '12 at 08:26
  • 1
    [Tyler's answer](http://stackoverflow.com/a/8594971/165673) has better single file solution – Yarin Jul 17 '12 at 12:02
27

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.

Community
  • 1
  • 1
solstice333
  • 3,399
  • 1
  • 31
  • 28
  • 1
    Thank you a million, this worked for me! Note to others that the order of the lines here is important, you can't have !/foo/bar before /foo/* for example. – Iamsodarncool Jan 24 '19 at 01:08
16

Try following in latest GIT version.

*
!*/
!/path/to/your/dir/**
Kirby
  • 2,847
  • 2
  • 32
  • 42
12

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.

Jake
  • 2,106
  • 1
  • 24
  • 23
9

This .gitignore works for me:

*/
/*
!bin/
Lesmana
  • 25,663
  • 9
  • 82
  • 87
Trianam
  • 17,313
  • 2
  • 15
  • 2
8

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),
greenoldman
  • 16,895
  • 26
  • 119
  • 185
6

try this obscure git bug.

!bin**/**

Hope it helps :)

Supporting docs git bug sample

PS: try it first before commenting.

david
  • 2,900
  • 5
  • 28
  • 48
4

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.

Henry
  • 7,721
  • 2
  • 38
  • 38
0

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

Senzo
  • 11
  • 1
0

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/
Aelius
  • 1,029
  • 11
  • 22