我提交并将一些目录推送到github。之后,我修改了.gitignore文件,添加了一个应该忽略的目录。一切都很好,但(现在被忽略的)目录仍在github上。
如何从github和存储库历史记录中删除该目录?
我提交并将一些目录推送到github。之后,我修改了.gitignore文件,添加了一个应该忽略的目录。一切都很好,但(现在被忽略的)目录仍在github上。
如何从github和存储库历史记录中删除该目录?
当前回答
如果您在windows机器上使用Blundell所描述的技术(删除所有内容,然后添加所有内容),则可能会遇到所有文件都被修改的问题,因为行结尾发生了变化。要避免这种情况,请使用以下命令:
git rm -r --cached .
git config --global core.autocrlf false
git add --renormalize .
git add .
这将从索引中删除所有文件,将git配置为不更改行尾,重新规范行尾,并暂存除.gitignore中指定的文件之外的所有刚刚删除的文件
然后git提交
参考:如何在不更改空格的情况下从.gitignore中列出的存储库中删除文件
其他回答
布伦德尔的第一个回答对我来说并不奏效,但它给我指明了正确的方向。我也这样做过:
> for i in `git ls-files -i --exclude-from=.gitignore`; do git rm --cached $i; done
> git commit -m 'Removed all files that are in the .gitignore'
> git push origin master
我建议您先运行以下语句检查要删除的文件:
git ls-files -i --exclude-from=.gitignore
我在visual studio中使用了一个默认的.gitignore文件,我注意到它正在删除项目中的所有日志和bin文件夹,这不是我的本意。
根据我的回答:如何从git存储库中删除目录?
要仅从git存储库中删除文件夹/目录,而不是从本地删除,请尝试3个简单步骤。
删除目录的步骤
git rm -r --cached FolderName
git commit -m "Removed folder from repository"
git push origin master
在下次提交时忽略该文件夹的步骤
要在下次提交时忽略该文件夹,请在根目录中创建一个名为.gitignore的文件并将文件夹名称放入其中。您可以放入任意数量的文件夹
.gitignore文件将如下所示
/FolderName
.gitignore文件中的规则仅适用于未跟踪的文件。由于该目录下的文件已经提交到您的存储库中,因此您必须将其解压缩,创建提交,并将其推送到GitHub:
git rm -r --cached some-directory
git commit -m 'Remove the now ignored directory "some-directory"'
git push origin master
在不重写存储库历史记录的情况下,您无法从历史记录中删除文件-如果其他人正在使用您的存储库,或者您正在多台计算机上使用它,则不应这样做。如果您仍然想这样做,可以使用gitfilter分支重写历史记录-这里有一个有用的指南。
此外,请注意git-rm-cached-somedirectory的输出类似于:
rm 'some-directory/product/cache/1/small_image/130x130/small_image.jpg'
rm 'some-directory/product/cache/1/small_image/135x/small_image.jpg'
rm 'some-directory/.htaccess'
rm 'some-directory/logo.jpg'
rm是git对存储库的反馈;这些文件仍在工作目录中。
此方法应用标准的.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
如果您还需要从分支的提交历史记录中清除新忽略的文件,或者如果您不希望从将来的拉取中删除新忽略的文档,请参阅此答案。
Blundell的答案应该是有效的,但由于某种奇怪的原因,它与我无关。我必须首先将第一个命令输出的文件名导入一个文件,然后循环该文件并逐个删除该文件。
git ls-files -i --exclude-from=.gitignore > to_remove.txt
while read line; do `git rm -r --cached "$line"`; done < to_remove.txt
rm to_remove.txt
git commit -m 'Removed all files that are in the .gitignore'
git push origin master