Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in DevOps and Agile by (19.4k points)

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?

1 Answer

0 votes
by (27.5k points)

You have to keep in mind that the rules in your .gitignore file only apply to untracked files. Since in your repository the files under that directory were already committed, you need to unstage them first, then 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

Note that you won't be able to delete the file from your history without rewriting the history of your repository. 

Keep in mind that you should not do this if: 

  • Anyone else is working with your repository
  • You're using it from multiple computers

But if you still want to do that, you can use git filter-branch to rewrite the history. 

Here is a guide to that here. 

https://help.github.com/en/articles/removing-sensitive-data-from-a-repository

Browse Categories

...