将它们添加到.gitignore后,从远程存储库中删除目录 [英] Remove directory from remote repository after adding them to .gitignore

查看:184
本文介绍了将它们添加到.gitignore后,从远程存储库中删除目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我承诺并推送了一些目录给github。之后,我改变了 .gitignore 文件,添加了一个应该忽略的目录。一切正常,但(现在忽略)目录留在github上。



如何从github和版本库历史中删除该目录?

解决方案

.gitignore 文件中的规则仅适用于未跟踪的文件。由于该目录下的文件已经在您的存储库中提交,因此您必须取消它们,创建提交并将其提交给GitHub:

  git rm -r --cached some-directory 
git commit -m'删除现在被忽略的目录some-directory'
git push origin master

如果没有重写存储库的历史记录,则无法从历史记录中删除文件 - 如果其他人正在处理您的存储库,或者您从多台计算机使用它。如果你仍然想这样做,你可以使用 git filter-branch 来重写历史 -

另外,请注意 git rm -r --cached some-directory 会是这样的:

  rm'some -b目录/ 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关于存储库的反馈;这些文件仍然在工作目录中。


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.

How do I delete that directory from github and the repository history?

解决方案

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

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.

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'

The rm is feedback from git about the repository; the files are still in the working directory.

这篇关于将它们添加到.gitignore后,从远程存储库中删除目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆