Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git reset --hard origin/master failing

Tags:

git

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.

like image 948
Caes Avatar asked Jul 23 '16 00:07

Caes


People also ask

What does git reset -- hard origin master do?

git reset --hard origin/master works only as a full wipe out if you are in a local branch. If you are in the master branch instead, and if you have made changes, you can only drop all of the files that you made or changed. You cannot drop the folders that you added.

How do I undo git reset hard origin master?

If you committed it, nothing is lost. If you have the reference of the commit, you can just git reset --hard <sha> to that precise commit. In case you don't you can always use git reflog to retrieve the sha before performing the hard reset.

Is git reset destructive?

There are two kinds of "destructive" here -- commands that are destructive to your git history and commands that discard changes in your working copy. Commands that discard work tree changes: git reset.


2 Answers

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> 
like image 56
ZR87 Avatar answered Sep 28 '22 04:09

ZR87


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.

like image 36
AleksandrH Avatar answered Sep 28 '22 05:09

AleksandrH