Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove directory from remote repository after adding them to .gitignore

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?

like image 858
janw Avatar asked Oct 28 '11 09:10

janw


People also ask

How do I remove a folder from my remote repository?

Use rm -r switch with the git command to remove directory recursively. After removing the directory you need to commit changes to the local git repository. Then push the changes to remove the directory from the remote git repository.

Will Gitignore remove files from repo?

gitignore" is very helpful, it tells me what files are excluded by . ignore. I was happy to find these commands, BUT it does not really remove the files from repository.


1 Answers

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.

like image 185
Mark Longair Avatar answered Oct 14 '22 01:10

Mark Longair