.gitignore排除文件夹和文件,但包含子文件夹的内容。

4
我有以下这样的文件夹:
/foo/one.txt
/foo/bar/two.txt
/foo/other/three.txt

我希望在排除/foo/文件夹中的所有内容时保留子文件夹/foo/bar/。如何实现?
使用以下.gitignore文件,我已经成功排除了“其他”子文件夹,但仍然包括文件“one.txt”。
/foo/*
!/foo/bar/

1
你所做的对我来说按照你想要的方式起作用了。 - CB Bailey
2个回答

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'
$ 

0

也许你已经成功使用了 .gitignore,但你可能会遇到一个常见的问题,需要从索引中删除一个现在在 gitignore 中的跟踪文件,例如 why-git-doesnt-ignore-my-specified-file

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


谢谢。它在索引中。我太愚蠢了,竟然没有检查自己的示例 :) - exstral

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接