Unignore subdirectories of ignored directories in Git

无人久伴 提交于 2019-12-17 15:35:57

问题


Let's say I have ignored a directory, but I want to unignore specific subdirectories therein. So I have the setup:

/uploads/
/uploads/rubbish/
/uploads/rubbish/stuff/KEEP_ME/
/uploads/foo/
/uploads/foo/bar/lose/

And I want to ignore everything but the KEEP_ME directory. I would hope the ignore would look something like:

/uploads/*
!/uploads/rubbish/stuff/KEEP_ME/

But that's not working, and neither are several permutations on the same theme.

One that does work is

/uploads/**/**/**/
!/uploads/rubbish/stuff/KEEP_ME/

But that feels a little restrictive and verbose?


回答1:


According to pattern format section of the gitignore documentation:

An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined. Put a backslash ("\") in front of the first "!" for patterns that begin with a literal "!", for example, "!important!.txt".

Therefore the previously-excluded parent directory /uploads/rubbish/stuff/keep/ pattern must be exclusively negated before negating its content:

#ignore everything within /uploads/ 
/uploads/*

#include everything within /uploads/rubbish/stuff/keep
!/uploads/rubbish/stuff/keep/  
!/uploads/rubbish/stuff/keep/*

To include subdirectories inside /uploads/rubbish/stuff/keep/ add the third line:

!/uploads/rubbish/stuff/keep/**/*



回答2:


Even if you add something to .gitignore, you can force git to add it to the index

git add --force uploads/rubbish/stuff/KEEP_ME/

However, "KEEP_ME" seems to be a directory and git usually doesnt like empty folder, so you should can add a "placeholder"-holder file instead, if the folder is empty

git add --force uploads/rubbish/stuff/KEEP_ME/.keep_me



回答3:


Was trying to figure out how to include a specific folder when excluding all folders with the generic exclusion

**/build

if you add the /* to the end of your generic exclude you continue to exclude all build files **/build/*

Then you add another line to correct for the path that you want to be included to look like

!**/folder/build/* 

leaving us a gitignore that reads

**/build/* 
!**/folder/build/* 



回答4:


Buo-Ren Lin and John's answers are quite helpful, but I needed to combine both.

When wanting to exclude other sub-folders as well as files within uploads, I found it necessary to explicitly specify arbitrary directories before the given folder both while excluding the folder and while including the sub-folder

**/uploads/*
!**/uploads/rubbish/
!**/uploads/rubbish/*

I also found it necessary to explicitly re-include both the sub-folder and its contents to show both items within the folder and sub-folders.



来源:https://stackoverflow.com/questions/5261959/unignore-subdirectories-of-ignored-directories-in-git

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