-1

I was working on a project which was committed on GitHub. I made some changes and tried to commit it to the repo, But I got the error saying Your branch is ahead of 'origin/main' by 3 commits.

So I tried to go back to the branch origin/main and I did git reset --hard origin/master

Now I was on the main branch. So I pushed the code on Github. Code got pushed but it was not reflected in the repo.

And When I came back to the editor all the changes I made on Local machine were gone it is not there.

I need my changes made codes back on my local machine.

Please Help!

Ross
  • 11
  • 1
  • 3
  • How did you push the reset? – evolutionxbox Feb 17 '21 at 15:08
  • normally using `git push` – Ross Feb 17 '21 at 15:09
  • 2
    Hang on. Why did you reset in the first place? It's expected that when work is done locally the local branch becomes "ahead" of the origin branch. – evolutionxbox Feb 17 '21 at 15:10
  • It was not getting pushed. That's why I went back to main branch – Ross Feb 17 '21 at 15:14
  • Why though? Didn't you want to keep the changes? – evolutionxbox Feb 17 '21 at 15:15
  • _"It was not getting pushed"_ --- Why? What was the error? – evolutionxbox Feb 17 '21 at 15:15
  • 1
    `Your branch is ahead of 'origin/main' by 3 commits.` this is not an error , but rather informing you that your local branch is modified and the next step here was to do a `git push` to push your changes to remote. – Dev-vruper Feb 17 '21 at 15:18
  • I am a new bee. Since I was getting the error and the code was not getting pushed so I searched StackOverflow and it said I have to go back to the main branch in order to push so I came back to the main branch. and pushed but after the code gets pushed all the changed made were gone. – Ross Feb 17 '21 at 15:19
  • but I did `git push` and it was not pushing my code so I thought I have to go back to main branch – Ross Feb 17 '21 at 15:22
  • try to run `git reflog` and see if you see the last commit which you want in the list . you can then run `git reset --soft of the commit where you want your HEAD. – Dev-vruper Feb 17 '21 at 15:27
  • 1
    Does this answer your question? [How can I undo git reset --hard HEAD~1?](https://stackoverflow.com/questions/5473/how-can-i-undo-git-reset-hard-head1) – Despina Kastani Feb 17 '21 at 15:35

1 Answers1

0

But I got the error saying Your branch is ahead of 'origin/main' by 3 commits.

This is not an error. It's just information: your Git is telling you that, as far as it knows, you have three commits that they don't.

So I tried to go back to the branch origin/main and I did git reset --hard origin/master

That tells your Git: Please discard my three commits, and any unsaved work too. It will have done so.

The good news is that commits are considered highly valuable—almost semi-sacred—in Git, and discarded ones can usually be retrieved for a minimum of at least 30 days. Unsaved work is not recoverable this way though.

The answers in the linked question (How can I undo git reset --hard HEAD~1?) will let you get those three commits back. For unsaved (un-committed) work that you want back, you will need some other non-Git method to recover that. If there is no such work you are in good shape.

torek
  • 448,244
  • 59
  • 642
  • 775