将目录添加到 .gitignore 后从远程存储库中删除目录 [英] Remove directory from remote repository after adding them to .gitignore
问题描述
我提交并推送了一些目录到 github.之后,我修改了 .gitignore
文件,添加了一个应该被忽略的目录.一切正常,但(现在被忽略)目录保留在 github 上.
I committed and pushed some directory to github. After that, I altered the .gitignore
file adding a directory that should be ignored. Everything works fine, but the (now ignored) directory stays on github.
如何从 github 和存储库历史记录中删除该目录?
How do I delete that directory from github and the repository history?
推荐答案
.gitignore
文件中的规则仅适用于未跟踪的文件.由于该目录下的文件已在您的存储库中提交,您必须取消暂存,创建提交并将其推送到 GitHub:
The rules in your .gitignore
file only apply to untracked files. Since the files under that directory were already committed in your repository, you have to unstage them, create a commit, and push that to GitHub:
git rm -r --cached some-directory
git commit -m 'Remove the now ignored directory "some-directory"'
git push origin master
您不能在不重写仓库历史记录的情况下从历史记录中删除文件 - 如果其他人正在使用您的仓库,或者您正在多台计算机上使用它,则不应这样做.如果你仍然想这样做,你可以使用 git filter-branch
来重写历史 - 这里有一个有用的指南.
You can't delete the file from your history without rewriting the history of your repository - you shouldn't do this if anyone else is working with your repository, or you're using it from multiple computers. If you still want to do that, you can use git filter-branch
to rewrite the history - there is a helpful guide to that here.
另外,请注意 git rm -r --cached some-directory
的输出将类似于:
Additionally, note the output from git rm -r --cached some-directory
will be something like:
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 对仓库的反馈;文件仍在工作目录中.
The rm
is feedback from git about the repository; the files are still in the working directory.
这篇关于将目录添加到 .gitignore 后从远程存储库中删除目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!