git says I have unstaged changes but I can not see them

24.5k views Asked by At

I am executing this command on the second file I want to remove (the first was JetBrains' .idea folder files which are now in .gitignore), but I am not able to:

git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch gf5/console/basic.php' --prune-empty --tag-name-filter cat -- --all
Cannot rewrite branches: You have unstaged changes.

So how do I know what "unstaged" files git is referring to when it does not list them, and how do I fix this?

PS the issue is not stash(es), I do not have any and have not done any stashing on this repo ever.

NOTE: I did a git status and receive this:

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   gf5/console/basic.php

Changes not staged for commit:
   (use "git add <file>..." to update what will be committed)
   (use "git checkout -- <file>..." to discard changes in working directory)

//(and a whole long list of files)
3

There are 3 answers

1
Oliver Williams On BEST ANSWER

AHA!! I figured out what is going on. Read the last few sentences for a little insight.

The answer was to first do:

git status

This showed modified files in RED, which I presume means they are somehow "deleted" in git's mind. I then did:

git add --all

From there I committed all the staged files using SourceTree (sorry I didn't stay with the command line all the way). I went ahead and simultaneously pushed changes up to the server but that would be optional.

The above command then worked successfully.

When I tried to run the above command on a different file, I had the same problem. So I again ran git status and it turns out that the file that I just removed from history is still there and so you need to do this:

git rm /previous_file_you_just_removed_from_history.php

Also to clarify, the filter-branch command will remove the history of that file from all commits, but it will NOT remove the changes that you made to that file. I.e. that removed file is still the sum of all changes you made to it up to that point. I hope this helps clear this concept up for someone else.

0
ArkTekniK On

I had this same issue whereby all of my files were showing as modified right after a fresh clone and discovered the cause to be (in my case) that Tortoise Git was automatically modifying the line endings of every file on checkout and commit.

To prevent this behaviour, disable the AutoCrLf setting under the repository settings window (Git -> Config Source [Local] -> AutoCrlf) as described in this question.

1
Rodrigo Cruz On

In case more files needed to be excluded:

I used that command but then I noticed there were more files to include. Then, I tried to add them one by one but got a message saying it can not overwrite the backup file.

So this one seems to work for me:

$ git filter-branch --tree-filter 'rm -rf fullpath_file1 fullpath_file2 ... fullpath_file_n' -f HEAD