23

When I attempt to use git rm --cached I receive the following error:

error: the following file has staged content different from both the file and the HEAD:

I know that I can circumvent this error with git rm --cached -f <filename>. But normally when I unstage files with git rm --cached I do not get this error.

My question is what does it mean that the file has different staged content from the HEAD.

krc
  • 483
  • 1
  • 3
  • 12
  • 4
    It means the file has changed, moved to stage state, and now its content is different then the HEAD version. – MaxZoom Jun 21 '16 at 19:52
  • 2
    This error also happens after adding a folder containing another git repository – galath Oct 12 '18 at 14:27

5 Answers5

16

Typically, you get this status for an item that is "staged and modified", that is: it was modified in the first place, then it was staged and then it was modified again.

This status must be handled with care, otherwise lead to a lot of misconception if you now run a commit, since only staged changes will be committed (yes, even in the same file context, only staged changes will be committed), and not staged changes will be kept in the non-staging area for a future commit (if staged).

galath
  • 5,717
  • 10
  • 29
  • 41
Luis
  • 2,833
  • 3
  • 27
  • 41
  • Okay this makes a lot of sense. I had added the file to git and edited it while it was staged. Thank you for the insight. – krc Jun 21 '16 at 19:57
  • 2
    For those wondering how you can `git rm --cached` without `-f`. You'll first have to stage the change made on top of the already staged changes. So `git add file`, which will set the staged content equal to the file content. You can then use `git rm --cached file` – 3limin4t0r Sep 07 '21 at 16:00
5

There are three places (for changes) worth to be distinguished:

  • in the file
  • changes in the file that you have already accepted to be part of the next commit, so called staged changes
  • changes in a file which were committed.

Only the first two matter, when you edit and commit files. Of the two (unstaged vs staged) git status shows you, what type of change you have.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
2

if you you wants to remove some file from commit and you are getting this. error: the following file has staged content different from both the file and the HEAD: your_file_name (use -f to force removal)

then please use -f after rm like this git rm -f --cached your_file_name

Hannan
  • 109
  • 9
0

In your terminal, run:

  1. Commit your change first.
  2. git rm --cached filename-to-untrack then what should be ignored will be ignored.(if that is a folder then add -r after the rm)
NeoZoom.lua
  • 2,269
  • 4
  • 30
  • 64
-1

I had your question, as well as the question of "how do I fix it". I couldn't figure out a way to fix it with SourceTree. (normally it gives more options than GitHub desktop.)

As Micha and Luis point out, the literal answer to your question is that you have three versions of the file, and yes they have to be handled with care, as they are all different. In other words, yes the staged file is different from HEAD (any staged file would be). The versions are:

  1. The original file in source control
  2. The file staged, newer than what's committed
  3. The file on disk, even newer than what's committed or in source control

The use case is, you staged the files, then you change it in your IDE say, then you went back to remove the commit to stage your actual changed file, and you got this error.

SourceTree wouldn't let me stage the new file, nor would it let me unstage the file, giving this error message. As we have some CI/CD pipelines setup, I did not want to commit the file; I wanted the newest file.

The fix for me was to use the git CLI. A simple

git add offendingfile.js

did the trick. Why SourceTree couldn't do it, I'm not sure.

J. Gwinner
  • 931
  • 10
  • 15