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

删除回忆录丶 提交于 2019-12-17 00:46:33

问题


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?


回答1:


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.




回答2:


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



回答3:


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



回答4:


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 directory that are in subdirectories that aren't called bin. This may or may not matter in your situation.




回答5:


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.




回答6:


Try following in latest GIT version.

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



回答7:


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.




回答8:


This .gitignore works for me:

*/
/*
!bin/



回答9:


try this obscure git bug.

!bin**/**

Hope it helps :)

Supporting docs git bug sample

PS: try it first before commenting.




回答10:


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.



来源:https://stackoverflow.com/questions/1248570/how-do-i-tell-git-to-ignore-everything-except-a-subdirectory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!