提出最多的答案的解决方案是不正确的,因此很容易证明。
首先忽略上载/ *中的所有内容:
mkdir -p uploads/rubbish/stuff/KEEP_ME
touch uploads/a uploads/rubbish/a uploads/rubbish/stuff/a uploads/rubbish/stuff/KEEP_ME/a
echo '/uploads/*' >> .gitignore
git init
git add .
git commit -m "Initial commit"
现在,如上所述,忽略忽略的内容的父目录:
echo 'uploads/rubbish/stuff/KEEP_ME/' >> .gitignore
echo 'uploads/rubbish/stuff/KEEP_ME/*' >> .gitignore
git status -u
不显示未跟踪的文件。
为了使其正常工作,您需要忽略uploads/
树下的所有文件(uploads/**/*
,而不仅仅是顶层文件uploads/*
),然后添加要保留的树的所有父目录
echo '/uploads/**/*' > .gitignore
echo '!/uploads/rubbish/' >> .gitignore
echo '!/uploads/rubbish/stuff' >> .gitignore
echo '!/uploads/rubbish/stuff/KEEP_ME' >> .gitignore
echo '!/uploads/rubbish/stuff/KEEP_ME/*' >> .gitignore
git status -u
这使:
On branch master
...
Untracked files:
(use "git add <file>..." to include in what will be committed)
uploads/rubbish/stuff/KEEP_ME/a
如果我们uploads/*
在.gitignore
上面使用过,那么所有中间文件也将包括在内,因此例如uploads/rubbish/a
将显示在上面的status命令中。