Arefe
Arefe

Reputation: 12451

Github Retrieve the code after I make a mistake

I just make a mistake at the time of working in a Java project. The code was in the IntelliJ and I make a repo in the Github with the intention to push there. I initially tried to push and I get information that the branch was behind. I think its due to the reason I put the LICENSE and the .gitignore in the repo at the time of creation.

I perform the commands then,

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

The IDEA has node code and all gone.

enter image description here

The log info is here:

$ git log 
commit b6e96685f6c0d4e77ac39e45499fc0213808cdb5 (HEAD -> master, origin/master)
Author: Chaklader <[email protected]>
Date:   Sat Feb 9 11:04:31 2019 +0100

    updated

commit 1572b11db8f42f5444df312f5f86c5791befb22c

How do I get back to the previous commit now? I have done nothing afterward.

Update:

I did little mess with check out the branches and stash them. Afterward, I perform the following operations as suggested.

$ git reset --hard
$ git clean -fdx

$ git checkout master # make sure you are on the right branch first
$ git reset --hard HEAD@{1}

$ git pull --rebase

I find no changes and the IDE look like the following,

enter image description here

The log is here:

$ git log

$ git log
commit b6e96685f6c0d4e77ac39e45499fc0213808cdb5 (HEAD -> master, origin/master)
Author: Chaklader <[email protected]>
Date:   Sat Feb 9 11:04:31 2019 +0100

    updated

commit 1572b11db8f42f5444df312f5f86c5791befb22c

I'm in the master branch now:

$ git branch 
* master

When I did stash, I get the info,

$ git stash
Saved working directory and index state WIP on master: b6e9668 updated

The reflog command provides the info,

    $ git reflog show
1572b11 (HEAD -> master) HEAD@{0}: reset: moving to 1572b11db8f
1572b11 (HEAD -> master) HEAD@{1}: reset: moving to 1572b11db8f
b6e9668 (origin/master) HEAD@{2}: reset: moving to HEAD
b6e9668 (origin/master) HEAD@{3}: reset: moving to HEAD
b6e9668 (origin/master) HEAD@{4}: reset: moving to HEAD@{1}
b6e9668 (origin/master) HEAD@{5}: reset: moving to HEAD@{1}
b6e9668 (origin/master) HEAD@{6}: checkout: moving from master to master
:

The command $ git reset --hard 1572b11db8f return almost no code page,

enter image description here

Is there a possibility that I lost the code forever? Please, kindly help me.

Upvotes: 1

Views: 360

Answers (1)

VonC
VonC

Reputation: 1328332

IF you had done a commit before your reset --hard, try now:

git checkout master # make sure you are on the right branch first
git reset --hard HEAD@{1}

Check git reflog as in here to determine the right reference to reset to (git reflog show).

Then

git pull --rebase

Finally

git push

If git relog does not show any relevant commit, then you need to use some file-saving feature of your environment:

Upvotes: 2

Related Questions