问题描述
我之前忽略了一个文件夹,但现在我只想忽略它的内容:如何从我的 .gitignore 文件中删除/修改一些条目,以便 git 再次跟踪它们?我尝试更新 gitignore 文件并添加带有 git add -f <folder-name>
的文件夹,但没有奏效。
感谢您的帮助
解决方法
如果要跟踪空目录,仅忽略其内容是什么意思?
有一种方法可以使目录保持(几乎)为空(在存储库中)是在该目录中创建一个包含以下四行的 .gitignore 文件:
# Ignore everything in this directory
*
# Except this file
!.gitignore
,
假设您在添加到 .gitignore 文件的根文件夹下有一个名为 target
的文件夹。文件夹结构类似于以下:
-project root
|- src
|-- your src files here
|- target
|- file1
|- file2
|-.gitignore
我假设您想撤消 git ignore 并选择性地忽略您之前添加到忽略列表的目录中的文件。假设您只想将上述目录结构中的文件 1 添加到 git ignore。在这种情况下,编辑 .gitignore 文件以删除 /target/ 文件夹模式并添加一个新的 .gitignore 文件。 gitignore 在目标文件夹中,以 file1 作为内容。它应该只自动选取 file2 进行跟踪。
-project root
|- src
|-- your src files here
|- target
|- file1
|- file2
|- .gitignore - add file1 here
|-.gitignore - remove /target/ from here
,
git 不跟踪文件夹,忽略文件夹或忽略其内容对 git 来说是一回事。
也就是说,从 .gitignore 中删除文件夹条目即可开始跟踪该文件夹中的内容。