Caes
Caes

Reputation: 621

git reset --hard origin/master failing

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.

Upvotes: 49

Views: 99088

Answers (4)

Gavin
Gavin

Reputation: 151

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>

Upvotes: 5

Zoltan Rakottyai
Zoltan Rakottyai

Reputation: 1633

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>
    

Upvotes: 66

Erik Forsstr&#246;m
Erik Forsstr&#246;m

Reputation: 39

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

Upvotes: 3

Aleksandr Hovhannisyan
Aleksandr Hovhannisyan

Reputation: 1610

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.

Upvotes: 5

Related Questions