Git:仅当目录没有跟踪文件时才忽略目录
我有一个 git 存储库,如果我尚未在目录中添加文件,我希望 git status 忽略所有目录(以及下面的所有内容)。将 /*
添加到 .gitignore
几乎就可以了。我可以创建新目录,将文件放入其中,并且 git status 将完全被忽略。
然后我创建一个新目录并在其中放置一个文件“目录/文件”。然后我git add directory
、git commit
,并在该目录中创建一个新文件,例如“directory/newfile”。问题是 git status 不会向我显示新文件。
我尝试将 !/*/*
添加到 .gitignore
但据我所知它没有做任何事情。如何获取 git status 来显示包含跟踪(添加)文件的目录中的新文件,同时忽略没有跟踪文件的目录?
I have a git repository, and I want git status
to ignore all directories (and everything below) if I haven't added files in the directory yet. Adding /*
to .gitignore
almost does the trick. I can create new directories, put files in them, and git status
will be completely oblivious.
So then I create a new directory and put a file there, "directory/file". Then I git add directory
, git commit
, and create a new file inside this directory, say, "directory/newfile". The problem is that git status
won't show me the new file.
I tried adding !/*/*
to .gitignore
but it didn't do anything as far as I could tell. How can I get git status
to show new files in directories with file that are tracked (added), while ignoring directories without tracked files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
其他答案谈论 git 不跟踪目录,这是正确的,但没有回答问题。
首先,
/*
不仅会忽略目录及其内容,还会忽略存储库根目录中的文件。它忽略一切。我不确定这就是你想要的。如果没有,请尝试更改它/*/
。其次,
git add directory
不适用于忽略模式。git add
会失败,表示路径被忽略,您必须使用-f
。是的,目录(及其内容)只能使用 git add -f directory 来添加。现在,您已经添加并提交了目录,当您将新文件放入其中时, git status 将不会向您显示这一点,因为它当然会被忽略。 Git 不会进入目录来查找未跟踪的文件。但是,当然,对已跟踪文件的修改将出现在 git status 中。此外,
git add directory/newfile
将失败,并显示与使用-f
相同的消息。因此,每次添加目录时,您还必须显式地取消忽略该特定目录。所以你必须将
!/directory
添加到你的 .gitignore 中。The other answers talk about git not tracking directories, which is correct, but do not answer the question.
First of all,
/*
not only ignores directories and their content, but also files in the root of your repo. It ignores everything. I am not sure this is what you want. Try changing it/*/
if not.Second,
git add directory
would not have worked with the ignore pattern.git add
would have failed saying the path is ignored and you have to use-f
. And yes, the directory ( and its contents ) can be added with agit add -f directory
only.Now, that you have added and committed the directory, when you put a new file in it, git status will not show you that because, of course, it is being ignored. Git will not step into the directory to look for untracked files. But, of course, modifications to already tracked files will come in git status. Also,
git add directory/newfile
will fail with the same message about using-f
.So every time you add a directory, you also have to explicitly unignore that particular directory. So you will have to add something like
!/directory
to your .gitignore.Git 本身不存储目录。文件路径是节点数据的一个属性,没有文件就没有节点。如果“目录”为空,则
git add directory
在任何情况下都不会产生任何效果。git status
会将新创建的文件显示为未跟踪,除非您忽略它。尝试 git add directory/newfilename ,我不确定 .gitignore 是否会阻止显式添加,但我认为不会。如果是这样,您必须更改 .gitignore 以允许该目录(网上有很多有关语法的信息)。Git does not store directories themselves. The file path is an attribute of the node data, if there's no file, there's no node.
git add directory
should have no effect in any case, if 'directory' is empty.git status
would show the newly created file as untracked, except you have it ignored. Trygit add directory/newfilename
, I'm not sure if .gitignore blocks explicit adds, but I don't think it does. If it does, you'll have to change .gitignore to allow that directory (theres a lot of info on the syntax online).Git 无法跟踪目录。它不会存储空目录,这就是为什么您经常在想要拥有“空”文件夹的项目中找到名为“.keep”的文件。
Git can't track directories. It won't store empty directories, that's why you often find files named ".keep" in projects which want to have "empty"-folders.