2

I accidentally pushed the wrong commit, which was the first commit, to the main branch of the forked repository.

Since that forked repository has a lot of directories, I always downloaded only directories that I would work on at the time on my local machine. Then, I created or modified files.

Before I pushed the first commit(the wrong one), I always had uploaded those new files or modified files on the GitHub repository using the GitHub web UI. I'd never committed to Git at that moment.

Once I uploaded those files, I deleted them from my local machine.

Then, I committed a file to Git and pushed it to GitHub(used the following command git push -f)...now there is only one file in the repository.

I wanted to undo this and get back the forked repository with all files I uploaded. (I did undo the commit with git update-ref -d HEAD command, but there's no change on GitHub of course.)

I hope this explains my situation well enough, and please help me out.

(I thought I needed to ask the GitHub Support team and did, but still can't solve it yet...)

Kaho
  • 63
  • 8

1 Answers1

1

You need to use the GitHub poor man's reflog and query the GitHub Events API in order to get the most recent "previous" commit.

curl -u <username> https://api.github.com/repos/:owner/:repo/events

(replace :owner and :repo by your GitHub login and repository name)

From there, you can create a branch (on GitHub side) based on that commit:

curl -u <github-username> -X POST -d '{"ref":"refs/heads/<new-branch-name>", "sha":"<sha-from-step-1>"}' https://api.github.com/repos/:owner/:repo/git/refs

You can the fetch from origin, and locally reset your main branch to said "rescue" branch

git switch -C main origin/<new-branch-name>
git push -f

Note: you might need to add a personal access token when using curl

curl -i -u username:$token https://api.github.com/...

See Creating a personal access token.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you so much for the answer! I could get SHA, but after the following command `curl -u -X POST -d '{"ref":"refs/heads/", "sha":""}' https://api.github.com/repos/:owner/:repo/git/refs ` I got this one: `{ "message": "Not Found", "documentation_url": "https://docs.github.com/rest/reference/git#create-a-reference" }` does this mean it's impossible to retrieve the previous one anymore? – Kaho Mar 03 '21 at 21:12
  • @Kaho Did you replace the various parameters between `<...>`, like ``? And `;owner/:repo` as well? – VonC Mar 03 '21 at 21:24
  • Yes, I changed ``, ``, ``, and `:owner`, `:repo` to mine. is this because I force-pushed? I contacted the GitHub support team and they said they cannot revert the repository since they don't keep snapshots of the repository. I'm wondering if there are some changes... – Kaho Mar 03 '21 at 21:37
  • 1
    @Kaho No, the force push does not matter, unless you have done it more than 90 days ago. It should work. Check https://docs.github.com/en/rest/reference/git#create-a-reference and try to replace `` with a PAT (personal access token) and add (as in the documentation example) `-H "Accept: application/vnd.github.v3+json"`. – VonC Mar 03 '21 at 21:54
  • I did it! I made a mistake with the password...I'd kept putting the password for my computer or GitHub. When putting a PAT, it worked fine. You are a lifesaver, or saviour rather! – Kaho Mar 03 '21 at 22:57