我有文件夹应用程序/,我添加到.gitignore。在应用程序/文件夹中是文件夹application/language/gr。我如何包含这个文件夹?
我试过了
application/
!application/language/gr/
我有文件夹应用程序/,我添加到.gitignore。在应用程序/文件夹中是文件夹application/language/gr。我如何包含这个文件夹?
我试过了
application/
!application/language/gr/
当前回答
我在这里发现了一个类似的情况,在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/*/*
其他回答
这招对我很管用:
**/.idea/**
!**/.idea/copyright/
!.idea/copyright/profiles_settings.xml
!.idea/copyright/Copyright.xml
这是另一个遍历目录结构以获得所需内容的示例。注意:我没有排除Library/,而是排除Library/**/*
# .gitignore file
Library/**/*
!Library/Application Support/
!Library/Application Support/Sublime Text 3/
!Library/Application Support/Sublime Text 3/Packages/
!Library/Application Support/Sublime Text 3/Packages/User/
!Library/Application Support/Sublime Text 3/Packages/User/*macro
!Library/Application Support/Sublime Text 3/Packages/User/*snippet
!Library/Application Support/Sublime Text 3/Packages/User/*settings
!Library/Application Support/Sublime Text 3/Packages/User/*keymap
!Library/Application Support/Sublime Text 3/Packages/User/*theme
!Library/Application Support/Sublime Text 3/Packages/User/**/
!Library/Application Support/Sublime Text 3/Packages/User/**/*macro
!Library/Application Support/Sublime Text 3/Packages/User/**/*snippet
!Library/Application Support/Sublime Text 3/Packages/User/**/*settings
!Library/Application Support/Sublime Text 3/Packages/User/**/*keymap
!Library/Application Support/Sublime Text 3/Packages/User/**/*theme
> git添加库
> git 状态
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: Library/Application Support/Sublime Text 3/Packages/User/Default (OSX).sublime-keymap
new file: Library/Application Support/Sublime Text 3/Packages/User/ElixirSublime.sublime-settings
new file: Library/Application Support/Sublime Text 3/Packages/User/Package Control.sublime-settings
new file: Library/Application Support/Sublime Text 3/Packages/User/Preferences.sublime-settings
new file: Library/Application Support/Sublime Text 3/Packages/User/RESTer.sublime-settings
new file: Library/Application Support/Sublime Text 3/Packages/User/SublimeLinter/Monokai (SL).tmTheme
new file: Library/Application Support/Sublime Text 3/Packages/User/TextPastryHistory.sublime-settings
new file: Library/Application Support/Sublime Text 3/Packages/User/ZenTabs.sublime-settings
new file: Library/Application Support/Sublime Text 3/Packages/User/adrian-comment.sublime-macro
new file: Library/Application Support/Sublime Text 3/Packages/User/json-pretty-generate.sublime-snippet
new file: Library/Application Support/Sublime Text 3/Packages/User/raise-exception.sublime-snippet
new file: Library/Application Support/Sublime Text 3/Packages/User/trailing_spaces.sublime-settings
对于Git 1.9/2.0(2014年第一季度),Karsten Blees (kblees)的提交59856de澄清了这种情况:
Gitignore.txt:阐明排除目录的递归性质
可选前缀“!”,否定模式;任何被前一个模式排除的匹配文件将被重新包含。 如果排除了文件的父目录,则无法重新包含该文件。(*) (*:除非git 2.8+满足某些条件,见下文) 出于性能原因,Git没有列出排除的目录,因此所包含文件上的任何模式都没有影响,无论它们定义在哪里。 对于以文字“!”开头的模式,在第一个“!”前面加一个反斜杠(“\”),例如,“\!important!.txt”。 示例排除除特定目录foo/bar以外的所有内容(注意/* -没有斜杠,通配符也会排除foo/bar内的所有内容):
--------------------------------------------------------------
$ cat .gitignore
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar
--------------------------------------------------------------
在你的情况下:
application/*
!application/**/
application/language/*
!application/language/**/
!application/language/gr/**
必须先将文件夹列入白名单,然后才能将给定文件夹中的文件列入白名单。
2016年2月/ 3月更新:
注意,在git 2.9.x/2.10(2016年年中?)中,如果重新包含的路径中没有通配符,则该文件的父目录被排除在外,则可能重新包含该文件。
nguyforgen Thái ngeconc Duy (pclouds)正在尝试添加这个特性:
git v2.7.0提交506d8f1, git v2.8.0-rc0提交76b620d Commit 5e57f9c git v2.8.0-rc0在提交5cee3493 git 2.8.0时被恢复(!)。
因此,在git 2.9+中,这实际上是可以工作的,但最终被恢复了:
application/
!application/language/gr/
我发现只有这个可行。
**/node_modules/*
!**/node_modules/keep-dir
@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