简体   繁体   中英

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

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?

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.

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

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

Note: This is an exclusion file (ie .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

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.

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 .

Try following in latest GIT version.

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

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.

This .gitignore works for me:

*/
/*
!bin/

try this obscure git bug.

!bin**/**

Hope it helps :)

Supporting docs git bug sample

PS: try it first before commenting.

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.

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

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),

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM