如何从Git存储库中删除包含文件的单个目录?


当前回答

在删除任何内容之前,第一个git命令需要知道您是谁

初始化git-config user.name“某人”git-config user.email“someone@someplace.com"git rm-rgitcommit-m“删除目录”git推送原始主机

其他回答

您可以使用Attlasian源树(Windows)(https://www.atlassian.com/software/sourcetree/overview). 只需从树中选择文件并按下顶部的“删除”按钮。文件将从本地存储库和本地git数据库中删除。然后提交,然后推送。

我之前已经提交了该文件夹,并希望删除历史记录中的目录。

我执行了以下操作:

将文件夹添加到.gitignore:

echo Folder_Name/ >> .gitignore

从所有提交中删除:

git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch Folder_Name/' --prune-empty --tag-name-filter cat -- --all

从旧提交中删除refs:

git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d

确保完全删除所有旧引用

rm -Rf .git/logs .git/refs/original

执行垃圾收集

git gc --prune=all --aggressive

将更改推送到在线存储库:

git push

你到此为止。

但您可以通过以下操作将所有更改推送到所有分支:但是要小心这个命令!

git push origin --all --force
git push origin --tags --force

之后,该文件夹从git中删除,但没有从本地磁盘中删除。

要仅从git存储库而不是本地删除文件夹/目录,请尝试3个简单命令。


删除目录的步骤

git rm -r --cached FolderName
git commit -m "Removed folder from repository"
git push origin master

在下次提交时忽略该文件夹的步骤

要在下次提交时忽略该文件夹,请在根文件夹(初始化git的主项目目录)中创建一个名为.gitignore的文件并将该文件夹名称放入其中。您可以忽略任意数量的文件/文件夹

.gitignore文件将如下所示

/FolderName

我的一位同事建议BFG Repo Cleaner,我认为它很强大。它不仅可以删除不需要的数据,还可以清除存储库中的任何相关提交信息。

我通常使用gitadd-all从远程存储库中删除文件/文件夹

rm -r folder_name
git add --all
git commit -m "some comment"
git push origin master

master可以被存储库的任何其他分支替换。