{"id":50808512,"url":"https://github.com/lpmi-13/reflog-power","last_synced_at":"2026-06-13T03:05:10.125Z","repository":{"id":45660710,"uuid":"170971131","full_name":"lpmi-13/reflog-power","owner":"lpmi-13","description":"a git micromaterial to practice using the reflog to bring a deleted local branch back to life","archived":false,"fork":false,"pushed_at":"2021-12-29T23:14:37.000Z","size":6,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-04-09T22:13:51.509Z","etag":null,"topics":["git","micromaterials","software"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lpmi-13.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}},"created_at":"2019-02-16T06:30:21.000Z","updated_at":"2024-03-25T13:24:51.000Z","dependencies_parsed_at":"2022-09-17T08:10:56.901Z","dependency_job_id":null,"html_url":"https://github.com/lpmi-13/reflog-power","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/lpmi-13/reflog-power","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lpmi-13%2Freflog-power","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lpmi-13%2Freflog-power/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lpmi-13%2Freflog-power/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lpmi-13%2Freflog-power/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lpmi-13","download_url":"https://codeload.github.com/lpmi-13/reflog-power/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lpmi-13%2Freflog-power/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34270418,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-13T02:00:06.617Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["git","micromaterials","software"],"created_at":"2026-06-13T03:04:59.620Z","updated_at":"2026-06-13T03:05:10.118Z","avatar_url":"https://github.com/lpmi-13.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Git Reflog Micromaterial\n\n[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/lpmi-13/reflog-power)\n\n## Task - delete a local branch and bring it back to life\n\n[read the reflog documentation here](https://git-scm.com/docs/git-reflog)\n\nThe basic idea is to practice *accidentally* deleting one of our\nlocal branches (which I've done a few times), and then finding\nit again in the reflog. If you have a version pushed to one of\nyour remotes, then you wouldn't need to do this. This is in the\ncase where you didn't push anything yet, and have deleted the\nbranch that only existed locally.\n\nScary, right! I used to think so, but don't worry...the reflog\ncan help us.\n\nOne of the best parts of git is how you can recover from almost\nanything, so don't be afraid to lose your work, you can almost\nalways get it back.\n\nWe're going to practice doing that now.\n\n\n## step-by-step\n\nTo start, let's check out a new branch and add a simple file:\n\n`git checkout -b new_script`\n(or whatever you want to call the branch)\n\n`touch script.js`\n\nYou can add some content in it if you want, but that's not\nnecessary to practice our reflog magic.\n\nLet's commit it so it sticks around.\n(the reflog can only show committed work, so if we never\ncommitted, we wouldn't be able to do this, but also we\nwouldn't be able to leave the branch without committing,\nso if we have a branch, then we have commits on it)\n\n```\ngit add script.js\ngit commit -m 'add a new script'\n```\n\nNow you will have one new file on your new branch. Let's go back\nto our `main` branch and take stock of things.\n\n`git checkout main`\n\nIf we want to remind ourselves of what happened in the other branch\nwe can simply type:\n\n`git log new_script`\n\nand our log should show us the new commit in the other branch.\n\n...now for the fun part!\n\nImagine that we're cleaning up some of our old and unused branches,\nand so we run a simple command to delete this branch locally:\n\n`git branch -D new_script`\n\nthis will result in something that looks like\n\n```\nDeleted branch new_script (was 51bc649)\n```\n\nSo now we've deleted it, and can go have some tea or something...\n\n---\n\n...but wait!!!\n\nOh my goodness...you had some fantastic file updates in there that\nyou just found out you actually need (note: this happened to\nme earlier today)! Disaster, right?!?!\n\nNot so fast...\n\nWe can use the reflog to bring this branch and any of its files\nback to life!\n\nFirst, go ahead and access the reflog:\n\n`git reflog`\n\nYou should see something like this:\n```\n9cdc824 (HEAD -\u003e main) HEAD@{0}: checkout: moving from new_script to main\n51bc648 HEAD@{1}: commit: add a new script\n9cdc824 (HEAD -\u003e main) HEAD@{2}: checkout: moving from main to new_script\n```\n(the hashes at the beginning will be different, and you might have slightly\ndifferent numbers in `HEAD@{#}`, but the important part is that you have\nreferences to where your HEAD has been).\n\nSo this is basically a list of the steps you've been going through in your\nlocal copy of the repo. You can see where you cloned down the repo, where\nyou switched to a new branch, and where you added a new commit.\n\nThe reflog doesn't record where we delete branches, but that's okay, since\nwhat we want it for here is to checkout a previous state (commit).\n\nThe easy thing to do is look for the commit with the commit message that\nhad our new file. Here that is:\n\n`51bc648 HEAD@{1}: commit: add a new script`\n\nSo to complete the magic, we can simply checkout that particular commit\n(it still lives in git even if the branch is no longer listed when we\nrun a command like `git branch`)\n\n`git checkout HEAD@{1}`\n\nyou can also just use the short hash:\n\n`git checkout 51bc648`\n\n...but I personally like the `HEAD@{#}` syntax, since it feels more intuitive to me.\n\nEssentially, the # there is the number of \"transitions\" or \"moves\" or \"state changes\"\nago.\n\nAfter checking out the commit, our terminal should look something like:\n```\nNote: checking out 'HEAD@{1}'.\n\nYou are in 'detached HEAD' state. You can look around, make experimental\nchanges and commit them, and you can discard any commits you make in this\nstate without impacting any branches by performing another checkout.\n\nIf you want to create a new branch to retain commits you create, you may\ndo so (now or later) by using -b with the checkout command again. Example:\n\n  git checkout -b \u003cnew-branch-name\u003e\n\nHEAD is now at 51bc648 add a new script\n```\n\n...don't worry too much about the `detached HEAD` state, we won't be here\nlong.\n\nSo now that we've checked out that commit, we should see something like\nthis when we run `git status`:\n\n```\nHEAD detached at 51bc648\nnothing to commit, working tree clean\n```\n\nwe can confirm that we have our old file back by running `ls`.\n(or however you like to inspect the files in the directory)\n\nYou can add stuff to the file now if you want, but for our exercise,\nwe don't need to.\n\nWe need to checkout an actual branch now to make these files\nstick around, so let's do that with:\n\n`git checkout -b new_script_back_to_life`\n\nNow return to the `main` branch:\n\n`git checkout main`\n\nThere are a few ways to get stuff from that other branch into your `main` branch,\nbut since we only need one file, let's just do it the easy way:\n\n`git checkout new_script_back_to_life new_script.js`  \n\nthis syntax means something like\n\u003cpre\u003e\n(git checkout     FROM_BRANCH        FILE_NAME)\n\u003c/pre\u003e\n\nAnd now, if you run `git status`, you should see the new file in the output:\n\n```\nOn branch main\nChanges to be committed:\n  (use \"git reset HEAD \u003cfile\u003e...\" to unstage)\n\n        new file:   new_script.js\n\n```\n\nAwesome! We brought it back! Now just add it, commit it, and you can continue\ndoing whatever other great things you were doing in this project.\n\nThat's it! We're done!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flpmi-13%2Freflog-power","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flpmi-13%2Freflog-power","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flpmi-13%2Freflog-power/lists"}