简体   繁体   中英

How to download a specific git tree or commit after force push

I pushed the commit with -force without thinking about the consequences, but I do have all of the previous commits from an old Pull Request and I can still see the files and their commits. My question is, is it possible to download the repository even if all of the commits have been reset but there are still commits from an old Pull Request?

Say for example I would like to download the repository from this link https://github.com/organizaton/repo.github.io/commits/99fb22467f90a18be7e00aa1d51743bb4af20a43

Is it possible to download the repository files like that?

The current repository sees only 2 commits, but previously, there was a fair amount of them. I'm wondering if I can return them back?

Any kind of help is appreciated.

From inside your local repository you can fetch a github pull request like this:

git fetch origin pull/ID/head:newbranchname

eg with 1 in place of ID for pull request #1

After that you can happily switch branch with git checkout newbranchname and inspect your recovered treasure with git log

Source: https://help.github.com/articles/checking-out-pull-requests-locally/

Even if you pushed with force, the remote repository works the same way than your local one, and have received the same objects, which persist for a while.

What I would do is to perform a

git reflog <branch>

… onto the branch you've performed the push --force from, and try to retrieve the hash sum of the last commit in date before you do. If you can't find it from here (because you branch would probably have diverged from a long time before you push it), you can try to retrieve the remote reflog through the Github's event API :

https://api.github.com/repos/<your account>/<your repository>/events

Once you get this sum, create a tag pointing onto the commit's hash sum

git tag <tagname> <hash>

Then push the lineage onto the server

git push origin <tagname>

Since everything should still already be on server (only unreferenced), this should be immediate. You'll then be able to browse the old history without altering the current state of your production branches.

Docs about Github's API:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM