{"id":25300139,"url":"https://github.com/deftio/git-recover-from-bad-hard-reset","last_synced_at":"2026-01-20T11:11:03.268Z","repository":{"id":148290209,"uuid":"588448730","full_name":"deftio/git-recover-from-bad-hard-reset","owner":"deftio","description":"How I unwound an accidental git reset that went to far and couldn't be recovered using normal git checkout / cherry-pick procedures. Your mileage may vary.","archived":false,"fork":false,"pushed_at":"2023-01-13T06:24:42.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-06T23:41:21.379Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/deftio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-13T06:23:15.000Z","updated_at":"2023-01-13T06:23:15.000Z","dependencies_parsed_at":"2023-06-04T05:56:44.682Z","dependency_job_id":null,"html_url":"https://github.com/deftio/git-recover-from-bad-hard-reset","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/deftio/git-recover-from-bad-hard-reset","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deftio%2Fgit-recover-from-bad-hard-reset","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deftio%2Fgit-recover-from-bad-hard-reset/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deftio%2Fgit-recover-from-bad-hard-reset/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deftio%2Fgit-recover-from-bad-hard-reset/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/deftio","download_url":"https://codeload.github.com/deftio/git-recover-from-bad-hard-reset/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deftio%2Fgit-recover-from-bad-hard-reset/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28602175,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-20T10:46:13.255Z","status":"ssl_error","status_checked_at":"2026-01-20T10:42:51.865Z","response_time":117,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-13T05:38:31.878Z","updated_at":"2026-01-20T11:11:03.263Z","avatar_url":"https://github.com/deftio.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Recovering from git reset --hard (and its friends)\n\nSo I screwed up and accidentally hit a \n\n```sh\ngit reset --hard\n```\n\nwhen I should have been paying attention more and lost all the files in my initial commit.  \n\n~~ admonishment etc ~~\n\nBut here is how I unwound that (not easy but doable if git hasn't garbage collected your commits yet):\n\n## First we need to find lost commits:\n\n```sh\ngit fsck --lost-found\n```\n\nwhich shows stuff like this\n\n```text\nnotice: HEAD points to an unborn branch (359d54df943b1769eb18093f41c3c3c2f4438f54)\nnotice: No default references\nmissing tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\ndangling tree 359d54df943b1769eb18093f41c3c3c2f4438f54\n```\n\nNormally you may be able to restore some of these lost files with\n\n```sh\ngit checkout 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n```\n\nbut at for me returned this:\n\n```\nfatal: Cannot switch branch to a non-commit '4b825dc642cb6eb9a060e54bf8d69288fbee4904'\n```\n\nor \n\n```sh\ngit reflog\n```\nbut that returned this:\n\n```\nfatal: your current branch '359d54df943b1769eb18093f41c3c3c2f4438f54' does not have any commits yet\n```\n\nand of course this doesn't work either:\n\n```sh\ngit checkout 34053495some-long-hash2340293034334\n```\n\nand this didn't work either for the same reason... \n\n```sh\ngit cherry-pick git checkout 359d54df943b1769eb18093f41c3c3c2f4438f54\n```\n\n\nso you may have dangling trees or commits which can't be restored via the normal means...\n\nAlas all *may* not be lost (yet)..\n\n\n## The amazing power of git cat-file...\n\nSO back to the output of \n```sh\ngit fsck --lost-found\n```\n\nIt turns out we can take that dangling tree and run this:\n\n```sh\ngit cat-file -p 359d54df943b1769eb18093f41c3c3c2f4438f54\n```\n\nwhich shows the contents of that dangling tree (that git checkout, etc can't crack open)  giving this:\n\n```\n18093f41c3c3c2f4438f54\n100644 blob 936d605963eb73d0021c58965c5e7a3c3752ce03    README.md\n040000 tree bfa5598456b567cebde5a6a5917b7ab4a5d24f5d    config\n040000 tree db92d1e1ffb392836e19d2923cec45ca4b228126    mosquitto\n040000 tree 3eed7d61bde8c636fdac0ea2072638c787882f64    node_modules\n100644 blob 12277d965af7e05539bc1e361939d1a1fa680995    package-lock.json\n100644 blob 0ca4858f81be9a782a65cb31f6dd1d4be1cbf52f    package.json\n040000 tree 768502931d4ae5a641717441531bc1dcf2067a85    src\n```\n\nand then again to descend in to the src directory...\n\n```sh\ngit cat-file -p 768502931d4ae5a641717441531bc1dcf2067a85\n```\n\nwhich showed all the files in the /src directory like this:\n\n```\n717441531bc1dcf2067a85\n100644 blob 6139a8a8cfee274645c44940761238d7fcc4cb61    mqtt-cli.js\n100644 blob 40b47fee9de8ff291dd380db7e2960bce1f9e266    mqtt-hive.js\n100644 blob 05b90da77155109af8a3686792fcabacb95bcf2d    mqtt-local-pub.js\n100644 blob 607bd7f8d3a7d2302f0bc73b04675f1c6230cb8b    mqtt-local-sub.js\n100644 blob 2a58b0ec63c0203ecea66b2e1b129cdd3789734d    mqtt_publisher.py\n100644 blob be2afb5ea21d77f61a1e43c43811275d8c06cf37    mqtt_subscribe.py\n```\n\nwhich finally could be restored like this..\n\n\n\n```sh\ngit cat-file -p 607bd7f8d3a7d2302f0bc73b04675f1c6230cb8b \u003e  mqtt-local-sub.js\n```\nyup just like that you can rescue some lost files out of the file system abyss.\n\nThere are some scripts around to automate this but atleast you can navigate through the hashes of the old commit history and have some luck.\n\nI can't say this will undo bad git resets but atleaset there is a chance...!\n\nGood luck!\n\n## Some excellent references:\n\n* [Yashints](https://yashints.dev/about) : \n[How I recovered a day's worth of work with git](https://yashints.dev/blog/2020/04/05/git-logs)\n\n* [Stack Overflow: recover-dangling-blobs-in-git](https://stackoverflow.com/questions/9560184/recover-dangling-blobs-in-git)\n\n* [Rescue your ever lost commits](https://github.com/pendashteh/git-recover-index)\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeftio%2Fgit-recover-from-bad-hard-reset","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeftio%2Fgit-recover-from-bad-hard-reset","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeftio%2Fgit-recover-from-bad-hard-reset/lists"}