我提交并将一些目录推送到github。之后,我修改了.gitignore文件,添加了一个应该忽略的目录。一切都很好,但(现在被忽略的)目录仍在github上。
如何从github和存储库历史记录中删除该目录?
我提交并将一些目录推送到github。之后,我修改了.gitignore文件,添加了一个应该忽略的目录。一切都很好,但(现在被忽略的)目录仍在github上。
如何从github和存储库历史记录中删除该目录?
当前回答
此方法应用标准的.gitignore行为,不需要手动指定需要忽略的文件。无法再使用--exclude-from=.gitignore:/-以下是更新的方法:一般建议:从一个干净的repo开始-所有内容都已提交,工作目录或索引中没有挂起的内容,并进行备份!
#commit up-to-date .gitignore (if not already existing)
#this command must be run on each branch
git add .gitignore
git commit -m "Create .gitignore"
#apply standard git ignore behavior only to current index, not working directory (--cached)
#if this command returns nothing, ensure /.git/info/exclude AND/OR .gitignore exist
#this command must be run on each branch
git ls-files -z --ignored --exclude-standard | xargs -0 git rm --cached
#optionally add anything to the index that was previously ignored but now shouldn't be:
git add *
#commit again
#optionally use the --amend flag to merge this commit with the previous one instead of creating 2 commits.
git commit -m "re-applied modified .gitignore"
#other devs who pull after this commit is pushed will see the newly-.gitignored files DELETED
如果您还需要从分支的提交历史记录中清除新忽略的文件,或者如果您不希望从将来的拉取中删除新忽略的文档,请参阅此答案。
其他回答
第一种解决方案:
git rm --cached `git ls-files -i -c --exclude-from=.gitignore`
git commit -m 'Removed all files that are in the .gitignore'
git push origin main
这将删除git中的所有文件/文件夹,从而避免您必须手动选择每个文件/文件夹
第二种解决方案:它从本地git存储库中删除文件。git rm-r--缓存。git添加。gitcommit-m'删除了.gitignore中的所有文件'git推送原点主
--cached标志从git存储库中删除文件,但不从文件系统中删除。
根据我的回答:如何从git存储库中删除目录?
要仅从git存储库中删除文件夹/目录,而不是从本地删除,请尝试3个简单步骤。
删除目录的步骤
git rm -r --cached FolderName
git commit -m "Removed folder from repository"
git push origin master
在下次提交时忽略该文件夹的步骤
要在下次提交时忽略该文件夹,请在根目录中创建一个名为.gitignore的文件并将文件夹名称放入其中。您可以放入任意数量的文件夹
.gitignore文件将如下所示
/FolderName
对于windows用户来说,这对我来说非常有用
git ls-files -i -c --exclude-from=.gitignore | %{git rm --cached $_}
git add .
git commit -m "Remove files from .gitignore"
注意:此解决方案仅适用于Github桌面GUI。
使用Github桌面GUI非常简单。
暂时将文件夹移动到其他位置(从项目文件夹中移出)。编辑.gitignore文件并删除文件夹条目,该条目将在github页面上删除主存储库。提交并同步项目文件夹。将文件夹重新移动到项目文件夹中重新编辑.gitignore文件。
这就是全部。
此方法应用标准的.gitignore行为,不需要手动指定需要忽略的文件。无法再使用--exclude-from=.gitignore:/-以下是更新的方法:一般建议:从一个干净的repo开始-所有内容都已提交,工作目录或索引中没有挂起的内容,并进行备份!
#commit up-to-date .gitignore (if not already existing)
#this command must be run on each branch
git add .gitignore
git commit -m "Create .gitignore"
#apply standard git ignore behavior only to current index, not working directory (--cached)
#if this command returns nothing, ensure /.git/info/exclude AND/OR .gitignore exist
#this command must be run on each branch
git ls-files -z --ignored --exclude-standard | xargs -0 git rm --cached
#optionally add anything to the index that was previously ignored but now shouldn't be:
git add *
#commit again
#optionally use the --amend flag to merge this commit with the previous one instead of creating 2 commits.
git commit -m "re-applied modified .gitignore"
#other devs who pull after this commit is pushed will see the newly-.gitignored files DELETED
如果您还需要从分支的提交历史记录中清除新忽略的文件,或者如果您不希望从将来的拉取中删除新忽略的文档,请参阅此答案。