.gitignore无法忽略除文件夹之外的所有文件

时间:2014-12-03 18:36:47

标签: git

基本上,我有以下文件夹结构:

repo 
     README.md
     testing_folder
           readingme.md
     resources_folder

在我的.gitignore file中,我想忽略除当前目录中的文件夹之外的所有文件,这就是我所拥有的:

*
!.gitignore
!*/

当我git status时,这就是我得到的:

On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   .gitignore

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   .gitignore
    modified:   README.md

问题是:它没有显示testing_folderresources_folderuntracked。当我尝试git add testing/readingme.md时,我收到以下错误消息:

The following paths are ignored by one of your .gitignore files:
testing/readingme.md
Use -f if you really want to add them.
fatal: no files added

我现在开始变得非常困惑,因为我已经阅读了很多其他相关帖子。他们似乎工作,但我的工作不起作用。

我尝试在.gitignore文件中包含并排除!*/语句,但文件夹仍未显示为untracked

1 个答案:

答案 0 :(得分:2)

我重新创建了您在帖子中显示的目录结构,以及我的.gitignore包含的内容:

/*
!*/                                                                         
!.gitignore

我注意到当第一行是*而不是/*时,我遇到了和你一样的问题。我想,添加/会给git足够的上下文来做你想做的事情。使用/*我的git status返回:

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   .gitignore

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    testing_folder/

这实际上只会跟踪父目录中不存在的文件。 resources_folder没有显示的原因是因为git没有跟踪空目录。 There are some ways to trick git into tracking empty folders,但它不是默认行为。