0

I've seen a few topics relating to this topic but none exactly mirrored my situation. In most of those people pushed the wrong branch overwriting another or just needed to be able to restore the remote repo's commits from their local repo.

In my case, I have an old repo that I must have deleted locally. I still had the remote repo on GitHub and it maintained all my commits.

Somewhere along the way I must have cloned the repo onto my system but never reinitialized the local repo with git (ie didn't have a .git file nor did my terminal show git status). I believe the original local repo was on another machine before I formatted the hard drive. I continued to work on the program locally.

I ran:

  1. git init

  2. git add remote https://github.com/...

  3. git push origin master

    It gave me a message saying my local repo did not have some of the changes that were made on the remote repo. (Keep in mind that the local repo does not contain my commit history, only the remote GitHub repo does (did)).

    At the time I didn't think I needed the additions from GitHub and that the local repo was the most up-to-date, so I ran:

  4. git push -f origin master

    It went through just fine and I thought all was well. Until I logged into my GitHub. To my horror, I read the fateful words "1 commit".

So is there a way to restore the historical commits from that repo? I can't find them anywhere.

Christian
  • 4,902
  • 4
  • 24
  • 42
  • 1
    Welcome to SO! `git push -f` is always a good idea when the consequences are unclear :) Check out `git reflog`, see also https://stackoverflow.com/questions/3973994/how-can-i-recover-from-an-erronous-git-push-f-origin-master – Christian Nov 10 '22 at 23:09
  • See [this answer](https://stackoverflow.com/a/74364701/86072) – LeGEC Nov 11 '22 at 03:36
  • Note that in the multiple-answers link, there's some discussion about using the GitHub API to find the right hash ID to restore, but you do have to scroll through the local-repository-only answers to get there. – torek Nov 15 '22 at 07:05

0 Answers0