1

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 <omi.chaklader@gmail.com>
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 <omi.chaklader@gmail.com>
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.

Arefe
  • 11,321
  • 18
  • 114
  • 168
  • If you worked on that in IDEA you can probably recover your code from [Local History](https://www.jetbrains.com/help/idea/local-history.html), that's unrelated to version-control. – Tobias K. Feb 09 '19 at 11:37
  • On the VCS menu, point to Local History, and then click Show History. It shows nothing for me. – Arefe Feb 09 '19 at 11:51

1 Answers1

2

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:

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I was trying to get a fix quickly and I have done `git checkout b6e96685f6c0d4e77ac39e45499fc0213808cdb5 ` and `git stash` onto them. Your command doesn't help now. Please, kindly tell me what to do. – Arefe Feb 09 '19 at 10:25
  • 1
    @Arefe Is b6e96685f the right commit? (the one you had before your first reset?). If yes: `git checkout master; git reset --hard b6e96685f` – VonC Feb 09 '19 at 10:26
  • I wasn't able to check out the master . I get the output `$ git checkout master error: The following untracked working tree files would be overwritten by checkout: .idea/vcs.xml .idea/workspace.xml Please move or remove them before you switch branches. Aborting` – Arefe Feb 09 '19 at 10:27
  • 1
    @Arefe Again, if you have the right commit, do a `git reset --hard`, `git clean -fdx` to reset everything, then checkout master, reset to the right commit, pull rebase. – VonC Feb 09 '19 at 10:28
  • Please, see the updated question. I find no change till now. I get very nervous at the moment. – Arefe Feb 09 '19 at 10:40
  • @Arefe Again: did you make a commit *before* trying your initial push? If yes, that commit, and its content, is not lost: check the output of `git reflog show`: in your case, try a `git reset --hard 1572b11db8f` to see if you get your old code back. (and then: pull --rebase, and push) – VonC Feb 09 '19 at 10:50
  • I did make a commit before the initial push. It's great to know. I provided the output for the `reflog` in the updated answer. – Arefe Feb 09 '19 at 10:54
  • @Arefe OK. If you are on the master branch, a `git reset --hard 1572b11db8f` should show you your previous code. – VonC Feb 09 '19 at 10:55
  • @Arefe Is there any other commit beside b6e9668 `git reflog show`? – VonC Feb 09 '19 at 10:57
  • The `git reset --hard 1572b11db8f` shows nothing and I'm in the master branch. – Arefe Feb 09 '19 at 10:57
  • I was not been able to perform the `pull --rebase` and get this, `$ git pull --rebase Updating 1572b11..b6e9668 error: The following untracked working tree files would be overwritten by merge: .idea/vcs.xml .idea/workspace.xml Please move or remove them before you merge. Aborting First, rewinding head to replay your work on top of it... error: The following untracked working tree files would be overwritten by checkout: .idea/vcs.xml .idea/workspace.xml Please move or remove them before you switch branches. Aborting could not detach HEAD` – Arefe Feb 09 '19 at 10:57
  • You should do the `pull --rebase` only *after* having recovered your code. Not before. Make sure `git status` shows the rebase is not still in progress. – VonC Feb 09 '19 at 10:59
  • I will be back in an hour – VonC Feb 09 '19 at 10:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/188140/discussion-between-vonc-and-arefe). – VonC Feb 09 '19 at 11:00