Andrew Latham
Andrew Latham

Reputation: 6132

Git won't delete a file and always thinks the working directory is dirty

I was trying to put a new Rails app on Heroku, and had deleted the default index file public/index.html. However, to my surprise, when I visited my app on Heroku, I was shown the default index page instead of what index that I wanted to see.

OK, that's a little strange, but I guess there could be some reason like Git not removing the file from the repository even though I deleted it. So I looked up on the internet how to force Git to remove files from the repository and came up with

git filter-branch --tree-filter 'rm public/index.html' HEAD

When I ran this, however, it said "Cannot rewrite branch(es) with dirty working directory". I was unsure why my working directory was dirty, since I had just committed, and when I ran git status it said

# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   deleted:    public/index.html

This was confusing to me because I had deleted public/index.html quite a while ago. Why should it be the only change left? Still, I ran git add . and git commit -m "..." and it said

# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   deleted:    public/index.html
#
no changes added to commit (use "git add" and/or "git commit -a")

I then, irritated, tried git add . and git status and saw that the change was simply not being added. git add public/index.html and git rm public/index.html didn't do anything for me either. Then I googled the problem, and came up with a StackOverflow post where someone suggested trying git stash and git stash apply to refresh. I did that, and things still weren't working.

What is the problem and how do I fix it?

Upvotes: 0

Views: 997

Answers (2)

AlexDev
AlexDev

Reputation: 4717

Regarding the git filter-branch, it would remove the file from all the commits in the repo, if that is what you intended. Just make commit or stash your changes before doing it. If you just want it deleted from now on than seansmash's answer is probably what you need.

Upvotes: 0

Sean
Sean

Reputation: 346

I think you have to commit the delete basically, so do a git commit -a -m '[comment]' and then it'll accept your delete

Upvotes: 3

Related Questions