PureDevOps Community

Git: could not reset index file to revision 'head' ? git reset --hard origin/master failing

Trying to discard changes on my system, and make it exactly the same as origin/master:

git fetch --all
git reset --hard origin/master

I’ve done this before on same repo without problems, but this time it fails with the following:

fatal: Could not reset index file to revision 'origin/master'

Have tried the following:

  1. remove index and reset as suggested here:
rm .git/index
git reset
  1. also suggested here that some process could have a lock on .git\index. That killing process and then executing git reset could fix it, but not sure how to check if something has a lock on file remotely. Also seems that removing index file and reset would have had same effect.

Had the same issue, which quite crazy cause reset --hard should always work, but it seems the working tree had a lot of differences to my target branch.

I had no time to dig this down, but found this by accident:

  1. Run git gc to do some garbage collection. It will remove unnecessary files and optimize the local repository (more info about git gc can be found here).
  2. Simply reset and finally reset --hard to desired branch.
$ git gc

$ git reset

$ git reset --hard <target_branch>