我有文件夹应用程序/,我添加到.gitignore。在应用程序/文件夹中是文件夹application/language/gr。我如何包含这个文件夹?

我试过了

application/
!application/language/gr/

当前回答

这招对我很管用:

**/.idea/**
!**/.idea/copyright/
!.idea/copyright/profiles_settings.xml
!.idea/copyright/Copyright.xml

其他回答

我在这里发现了一个类似的情况,在laravel中默认情况下,.gitignore忽略所有使用asterix的内容,然后覆盖公共目录。 (这也是与主答案@Chris Johnsen相同的解决方案,只是可能更简洁一点。)

*
!public
!.gitignore

如果您遇到OP场景,这是不够的。

如果你想提交一个特定的public子文件夹,例如,在你的public/products目录中,你想包含一个子文件夹深度的文件,例如,包含public/products/a/b.jpg,它们不会被正确检测到,即使你像这样特别地添加它们!/public/products, !public/products/*等。

解决方案是确保像这样为每个路径级别添加一个条目来覆盖它们。

*
!.gitignore
!public/
!public/*/
!public/products/
!public/products/*
!public/products/*/
!public/products/*/
!public/products/*/*

@Chris Johnsen的回答很好,但在Git的新版本(1.8.2或更高版本)中,有一个双星号模式,你可以利用它来获得更简洁的解决方案:

# assuming the root folder you want to ignore is 'application'
application/**/*

# the subfolder(s) you want to track:
!application/language/gr/

这样你就不必“取消忽略”你想要跟踪的子文件夹的父目录。


Git 2.17.0(不知道在这个版本之前有多早。可能回到1.8.2),使用**模式结合排除法对指向您的文件的每个子目录有效。例如:

# assuming the root folder you want to ignore is 'application'
application/**

# Explicitly track certain content nested in the 'application' folder:
!application/language/
!application/language/gr/
!application/language/gr/** # Example adding all files & folder in the 'gr' folder
!application/language/gr/SomeFile.txt # Example adding specific file in the 'gr' folder

我想跟踪jquery生产js文件,这是有效的:

node_modules/*
!node_modules/jquery
node_modules/jquery/*
!node_modules/jquery/dist/*

我想跟踪位于/etc/nagios/中的Nagios配置文件以及/usr/lib64/ Nagios /plugins/中的插件。为此,我在/中初始化了一个git repo,并使用了以下排除列表:

/*
!etc
etc/*
!etc/nagios
!usr
usr/*
!usr/lib64
usr/lib64/*
!usr/lib64/nagios
usr/lib64/nagios/*
!usr/lib64/nagios/plugins

Git像这样遍历列表:

/*             exclude everything under / ...
!etc           but include /etc back
etc/*             exclude everything under /etc/...
!etc/nagios       but include /etc/nagios back
!usr           but include /usr back
usr/*             exclude everything under /usr/...
                  and so on...

类似于这条评论,没有一个解决方案和模式适合我;强制git添加应该被排除的文件和文件夹,有效:

Git添加-f。