Jean
Jean

Reputation: 2625

Git : mistakenly undid a commit

I wanted to checked in a bug fix to Git, but I had already started working on another feature. I checked in the files for the bug fix correctly (didn't check in the files I changed for the feature, which is not yet complete). Then, when I built the version in Git separately, I realized I had checked in one of the feature-related changes too, by mistake. So, I first thought

Let me commit all the files. Although the feature is not complete, the code is compiling successfully and working.

So, I started checking in a few files related to the new feature coding. Then, I thought that I could've just changed that one line in the file I had committed by mistake initially and re-committed it. So, I wanted to undo the commit. And I did

git reset HEAD~1
git reset HEAD~1

(twice). Then, the issues began. My Eclipse started showing some errors. So, I wanted to undo the reset. So I did this:

git reflog

repeatedly & gave (referred Undoing git reset?)

git reset HEAD@{N}

for every N that I saw in reflog which I wanted back. Now, when I try to push to git with

git push -u origin master

I get the following message

! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes

when I try

git status

I see

Your branch and 'origin/master' have diverged,
and have 5 and 5 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

I'm so confused! I just want back the copy of code in my local machine which has the changes I did before I started all this commit/uncommit. Any help would be appreciated.

Upvotes: 1

Views: 1481

Answers (1)

Simon Boudrias
Simon Boudrias

Reputation: 44589

In the reflog you also have the commit hash id.

Use this hash when resetting (and --hard it):

git reset --hard <HASH>

Also, as your correct commits are on your remote, you can do it this way:

git reset --hard origin/master

Upvotes: 2

Related Questions