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

13

276

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?

Axenic answered 8/8, 2009 at 11:4 Comment(1)
See also .gitignore exclude folder but include specific subfolderAplanatic
D
340

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.

Distrain answered 21/12, 2011 at 19:4 Comment(10)
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/Polytrophic
@Polytrophic I had a similar issue, and git check-ignore -v told me it was because of ~/.gitignoreOverman
@gurneyalex I found out that it fixes itself if I delete the local repo and re-clone it, it must be a bug.Polytrophic
Is the /*/ necessary?Housework
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.Markmarkdown
@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.Whalen
To make sure it works, after updating your .gitignore as the above, run git init, git add ..Wanids
@reads0520's comment worked for me.Bayer
This is, hands down, THE correct answer.Alburnum
@Whalen I would upvote 1000x if possible. This needs to be its own Q&A. It is too common of a problem.Marriott
C
143

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
Cwmbran answered 13/6, 2012 at 15:43 Comment(1)
This worked wonders for unignoring a laravel vendor package I had modified! Thank you, you saved me lots of headachesPankey
I
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
Indictment answered 20/1, 2013 at 7:27 Comment(2)
+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.Fortunetelling
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.Petrel
O
47

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.

Organize answered 8/8, 2009 at 11:21 Comment(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?Tridentine
Tyler's answer has better single file solutionYonah
T
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.

Terchie answered 18/7, 2016 at 9:53 Comment(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.Kella
T
17

Try following in latest GIT version.

*
!*/
!/path/to/your/dir/**
Tabard answered 23/9, 2015 at 12:11 Comment(2)
it should actually be !/path/to/your/dir/** for the last lineMcglone
@faham, yep, just added it.Tabard
W
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.

Whitherward answered 5/11, 2010 at 21:18 Comment(0)
K
11

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),
Keithakeithley answered 24/6, 2021 at 7:38 Comment(0)
F
9

This .gitignore works for me:

*/
/*
!bin/
Frodi answered 7/4, 2011 at 13:50 Comment(0)
R
6

try this obscure git bug.

!bin**/**

Hope it helps :)

Supporting docs git bug sample

PS: try it first before commenting.

Rochester answered 9/2, 2016 at 3:16 Comment(0)
R
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.

Reproduction answered 25/9, 2019 at 23:35 Comment(0)
I
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

Irreproachable answered 26/8, 2020 at 12:25 Comment(0)
S
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/
Spadix answered 15/11, 2021 at 19:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.