45

Trying to discard changes on server, 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
    
  2. 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.

Loosing my mind. Would really appreciate any help.

Community
  • 1
  • 1
Caes
  • 581
  • 1
  • 4
  • 6
  • 2
    This is very peculiar. But since you're trying to reset to the remote, then you can simply start over and clone the repository anew before you lose your mind for the moment. However, I hope you figure out why because I'd be afraid it might happen again. – Jeff Puckett Jul 23 '16 at 00:27
  • Things to check: is `GIT_INDEX_FILE` set in the environment? If so, do you have permission to create / write-on that file? If not, find the actual index file (usually `.git/index` but depends on `add-worktree` too now) and see if you have permission to create / write-on that file. – torek Jul 23 '16 at 01:09
  • hmm, GIT_INDEX_FILE is not set, permissions 644 for .git/index :\ – Caes Jul 23 '16 at 02:15
  • I guess the next thing I would try is strace (or similar) to see what is really failing. – torek Jul 23 '16 at 07:45
  • 2
    Turns out I did not have write permission on folder containing a drupal config file settings.php. Don't know how I missed that, or how it got changed, but there it is... after modifying permissions `reset --hard origin/master` worked fine. :/ Thanks for suggestions. – Caes Jul 26 '16 at 22:53
  • make sure to answer your own question and accept it so this can be closed down – DarkMukke Jan 26 '18 at 15:28

4 Answers4

59

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>
    
tripleee
  • 175,061
  • 34
  • 275
  • 318
ZR87
  • 1,463
  • 16
  • 27
  • 1
    Thank you a lot, it work for me in 2020, could you please explain how does it work? – Chau Giang Jun 23 '20 at 02:44
  • 3
    Before `git reset` ; I had to do `rm .git/index` – alper Nov 04 '20 at 11:33
  • I had to run `rm .git/index` before `rm .git/index` me too, and that command added files to the stage. But you can unstage the change and you will only see the file which is different between origin and local – Yehouda Feb 24 '22 at 12:05
4

Just wanted to note that this ended up being the source of my problem:

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.

If you're running anything on local, like Jekyll, React, etc. that has a lock on the files in your project, then killing that process with Ctrl+C/Cmd+C will do the trick.

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
4

I had the same error message after renaming a branch and then stashing - there was some dangling ref. to one of my files.

error: unable to unlink old 'my_filename.ext': Invalid argument
fatal: Could not reset index file to revision 'HEAD'

I was able to unlink the old file with:

git restore <my_filename.ext>
Gavin
  • 131
  • 5
2

I had the same problem and realized that I had run out of disk space...