Initially, my intention was to remove some long ago commit from one of my GitHub repositories using the following command from one of the answers to this question: Undoing a 'git push'
git push -f origin commit:branch
Apparently, I was distracted and seem to have forgotten that this way I'll "undo" all the commits up to the one referenced. Also, usually working with GitHub Desktop to commit/push/pull in my private repositories only, I probably have not understood how Git works on a command line (with all this "complex" stuff possible). I realized I might have messed up big time when GitHub Desktop suggested to push (?) 63 commits to the remote repository. Yeah, I pretty much panicked and deleted my local copy - only to pull the repository again and realize that the changes affected the remote repository.
I found another answer to the question Is there anyway to undo git push -f? sounding promising at first but since I might have messed up a second time by panicking, I obviously cannot scroll my terminal to determine the relevant commit to be restored and git reflog
also does not seem to be of help since this repository is freshly cloned, as far as I understand.
>git reflog
7a7a940 (HEAD -> main, origin/main, origin/HEAD) HEAD@{0}: reset: moving to ORIG_HEAD
7a7a940 (HEAD -> main, origin/main, origin/HEAD) HEAD@{1}: rebase (finish): returning to refs/heads/main
7a7a940 (HEAD -> main, origin/main, origin/HEAD) HEAD@{2}: rebase (start): checkout HEAD~7
7a7a940 (HEAD -> main, origin/main, origin/HEAD) HEAD@{3}: rebase (finish): returning to refs/heads/main
7a7a940 (HEAD -> main, origin/main, origin/HEAD) HEAD@{4}: rebase (start): checkout main
7a7a940 (HEAD -> main, origin/main, origin/HEAD) HEAD@{5}: clone: from https://github.com/dimfalk/rainfallr.git
However, since it's feeling like I'm pretty much making it worse every time I reach for a blade of grass without any real idea what I'm doing, I decided to get some help.
Is there any chance I can restore these "lost" 63 commits from over the past 2 years or are they inevitably gone?
Edit:
I just noticed all the commits seem still to be listed when inspecting GitHub branch activity. Is this helpful?
Update README.md (Force push)
dimfalk force pushed - 75c0ac..7a7a940 - 1 hour ago
[...]
You can navigate to the commit in GitHub, e.g. https://github.com/dimfalk/your-repo/tree/75c0cac and then enter a new branch name in the branch dropdown in the top left. If the branch does not exist yet, GitHub will offer to create it.
You can then fetch this branch to have it locally and then either merge or reset your current branch with/to it to restore your repository to your desired state. Once the history has been restored, you can then force push again to sync the remote repository.
Details on how to create the branch with GitHub's UI are covered in creating a branch from Git commit ID within Github web UI.
PS. Deleting rarely helps with restoring stuff. If you mess up, create a backup.