.gitignore排除包含文件但包含子目录的文件夹

时间:2011-09-02 14:27:59

标签: git gitignore

我有这样的文件夹:

/foo/one.txt
/foo/bar/two.txt
/foo/other/three.txt

我想要排除文件夹/ foo /中的所有内容,除了子文件夹/ foo / bar /。我该怎么做?

有了这个.gitignore,我设法排除了“其他”子文件夹,但仍然包含了文件“one.txt”。

/foo/*
!/foo/bar/

2 个答案:

答案 0 :(得分:0)

就像Charles Bailey在评论部分所说,你的代码有效:

$ cat .gitignore 
/foo/*
!/foo/bar/

$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   .gitignore
#   foo/
nothing added to commit but untracked files present (use "git add" to track)
$ git add foo/ -n
add 'foo/bar/two.txt'
$ 

答案 1 :(得分:0)

.gitignore正在运行时,您可能会遇到需要从现在位于gitignore的索引中删除跟踪文件的常见问题,例如: why-git-doesnt-ignore-my-specified-file

也就是说,使用git rm <file>删除/确保它不在索引中。