如何从 .gitignore 中的文件夹中删除文件?
How to remove files from a folder in .gitignore?
我似乎遇到了一个问题,文件夹“Resources/”被添加到我们开发分支的 .gitignore
,而另一个开发人员错误地将文件提交到功能分支(开发之外)中的这个文件夹.当功能分支合并到开发分支时,它添加了文件,然后停止跟踪文件夹,现在认为“/Resources”和其中的文件不存在,因为它们不应该存在。
不过,正如我从克隆分支并切换到开发中看到的那样,我们现在遇到的问题是资源/下载中的文件,因为它们是在 git 中添加的,但没有被删除.
我最初的想法是从 .gitingore
中删除“/Resources”,然后删除文件。但是,如果我尝试从 .gitignore
中删除“/Resources”文件夹,git 认为它们是新文件夹并尝试添加文件夹和已添加的文件。
编辑:我尝试使用 git filter-branch
,正如 other post 提到的那样,但我收到一条错误消息,提示“严重:修订错误 'rm'”
git filter-branch --force --index-filter 'git rm -r --cached --ignore-unmatch Resources/' --prune-empty --tag-name-filter cat -- --all
有没有办法从目录中删除被忽略的文件?
您可以添加确认 Resource/
文件夹删除的提交,同时忽略它:
git rm -r --cached Resources/
git commit -m "Delete Resources content, now ignored"
git push
If we delete the files, there are no tracked changes in git.
对,就是Resources/
加上.gitignore
的意义。
Currently, every time someone clones the repository, they have to manually delete the files
他们不会,因为 Resources/
不再跟踪(并且可以在本地重新创建)
如果文件夹已经被删除,那么开发人员就不必手动删除任何东西:本地文件将被忽略。
如果您需要从所有 提交中删除资源:
- 安装
git filter-repo
(python-based工具)
- 使用“Filtering based on paths (see also
--filename-callback
)”
When the feature branch was merged into the development branch, it added the files, then stopped tracking the folder and now thinks that "/Resources" and the files within it do not exist, as they should not.
.gitignore
用于检查要跟踪的新文件,无论您在该文件中写入什么内容,您手动提交的文件都将继续被跟踪。所以上面这句话没有任何意义,我相信你知道,因为稍后你会写:
The issue we're having now though, as I see from cloning the branch and switching into development, is that the files in Resources/ download, since they're added in git, but not removed.
正确,这些文件已经是存储库的一部分。
Is there a way to remove the files from a directory which are being ignored?
是的,git rm -r --cached
将执行文件删除,然后您可以使用 git commit
提交并使用 git push
推送。一旦文件从存储库中删除,.gitignore
将再次接管并且下次不会自动暂存文件(但仍可以手动暂存)。
我似乎遇到了一个问题,文件夹“Resources/”被添加到我们开发分支的 .gitignore
,而另一个开发人员错误地将文件提交到功能分支(开发之外)中的这个文件夹.当功能分支合并到开发分支时,它添加了文件,然后停止跟踪文件夹,现在认为“/Resources”和其中的文件不存在,因为它们不应该存在。
不过,正如我从克隆分支并切换到开发中看到的那样,我们现在遇到的问题是资源/下载中的文件,因为它们是在 git 中添加的,但没有被删除.
我最初的想法是从 .gitingore
中删除“/Resources”,然后删除文件。但是,如果我尝试从 .gitignore
中删除“/Resources”文件夹,git 认为它们是新文件夹并尝试添加文件夹和已添加的文件。
编辑:我尝试使用 git filter-branch
,正如 other post 提到的那样,但我收到一条错误消息,提示“严重:修订错误 'rm'”
git filter-branch --force --index-filter 'git rm -r --cached --ignore-unmatch Resources/' --prune-empty --tag-name-filter cat -- --all
有没有办法从目录中删除被忽略的文件?
您可以添加确认 Resource/
文件夹删除的提交,同时忽略它:
git rm -r --cached Resources/
git commit -m "Delete Resources content, now ignored"
git push
If we delete the files, there are no tracked changes in git.
对,就是Resources/
加上.gitignore
的意义。
Currently, every time someone clones the repository, they have to manually delete the files
他们不会,因为 Resources/
不再跟踪(并且可以在本地重新创建)
如果文件夹已经被删除,那么开发人员就不必手动删除任何东西:本地文件将被忽略。
如果您需要从所有 提交中删除资源:
- 安装
git filter-repo
(python-based工具) - 使用“Filtering based on paths (see also
--filename-callback
)”
When the feature branch was merged into the development branch, it added the files, then stopped tracking the folder and now thinks that "/Resources" and the files within it do not exist, as they should not.
.gitignore
用于检查要跟踪的新文件,无论您在该文件中写入什么内容,您手动提交的文件都将继续被跟踪。所以上面这句话没有任何意义,我相信你知道,因为稍后你会写:
The issue we're having now though, as I see from cloning the branch and switching into development, is that the files in Resources/ download, since they're added in git, but not removed.
正确,这些文件已经是存储库的一部分。
Is there a way to remove the files from a directory which are being ignored?
是的,git rm -r --cached
将执行文件删除,然后您可以使用 git commit
提交并使用 git push
推送。一旦文件从存储库中删除,.gitignore
将再次接管并且下次不会自动暂存文件(但仍可以手动暂存)。