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

我试过了

application/
!application/language/gr/

当前回答

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

其他回答

这是另一个遍历目录结构以获得所需内容的示例。注意:我没有排除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添加应该被排除的文件和文件夹,有效:

Git添加-f。

我经常在CLI中使用这个解决方案,而不是配置我的.gitignore,我创建了一个单独的.include文件,在其中定义我想包含的(子)目录,尽管目录直接或递归被.gitignore忽略。

因此,我额外使用

git add `cat .include`

在登台期间,提交之前。

对于OP,我建议使用.include,其中包含这些行:

<parent_folder_path>/application/language/gr/*

注意:使用cat不允许使用别名(在.include内)来指定$HOME(或任何其他特定目录)。这是因为行homedir/app1/* 当使用上述命令传递给git add时,显示为git add 'homedir/app1/*',并且将字符括在单引号(")中保留了引号内每个字符的文字值,从而防止别名(如homedir)发挥作用(参见Bash单引号)。

下面是我在这里的repo中使用的.include文件的示例。

/home/abhirup/token.txt
/home/abhirup/.include
/home/abhirup/.vim/*
/home/abhirup/.viminfo
/home/abhirup/.bashrc
/home/abhirup/.vimrc
/home/abhirup/.condarc

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

git add /path/to/module

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


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

如果您排除了application/,那么它下面的所有内容将始终被排除(即使稍后的某些负排除模式(“unignore”)可能匹配application/下的某些内容)。

要做你想做的事情,你必须“取消忽略”你想“取消忽略”的所有父目录。通常情况下,您会成对编写针对这种情况的规则:忽略目录中的所有内容,但不包括某些子目录。

# you can skip this first one if it is not already excluded by prior patterns
!application/

application/*
!application/language/

application/language/*
!application/language/gr/

请注意 后面的/*是重要的:

模式dir/排除了名为dir的目录和(隐式地)该目录下的所有内容。 使用dir/, Git永远不会查看dir下的任何内容,因此永远不会对dir下的任何内容应用任何“不排除”模式。 模式dir/*没有说明dir本身;它只是排除了dir下的所有东西。 使用dir/*, Git将直接处理dir的内容,让其他模式有机会“取消排除”一些内容(!dir/sub/)。