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

我试过了

application/
!application/language/gr/

当前回答

最简单也可能是最好的方法是手动添加文件(通常这优先于.gitignore风格的规则):

git add /path/to/module

如果文件已经被忽略,则可能需要-f。您甚至可能希望使用-N意图添加标志,以建议您将添加它们,但不是立即添加。我经常对我还没有准备好的新文件这样做。


这是一个答案的副本张贴在什么很容易是一个重复的QA。我在这里重新发布是为了增加能见度——我发现不搞乱gitignore规则更容易。

其他回答

gitignore -指定有意忽略的未跟踪文件。

示例排除除特定目录foo/bar以外的所有内容(注意/* -没有斜杠,通配符也会排除foo/bar内的所有内容):

$ cat .gitignore
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar

WordPress的另一个例子:

!/wp-content
wp-content/*
!/wp-content/plugins
wp-content/plugins/*
!wp-content/plugins/my-awesome-plugin

更多信息在这里:https://git-scm.com/docs/gitignore

这是另一个遍历目录结构以获得所需内容的示例。注意:我没有排除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

最简单也可能是最好的方法是手动添加文件(通常这优先于.gitignore风格的规则):

git add /path/to/module

如果文件已经被忽略,则可能需要-f。您甚至可能希望使用-N意图添加标志,以建议您将添加它们,但不是立即添加。我经常对我还没有准备好的新文件这样做。


这是一个答案的副本张贴在什么很容易是一个重复的QA。我在这里重新发布是为了增加能见度——我发现不搞乱gitignore规则更容易。

我在这里发现了一个类似的情况,在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/*/*

在WordPress中,这帮助了我:

wp-admin/
wp-includes/
/wp-content/*
!wp-content/plugins/
/wp-content/plugins/*
!/wp-content/plugins/plugin-name/
!/wp-content/plugins/plugin-name/*.*
!/wp-content/plugins/plugin-name/**