{"id":567468,"date":"2026-07-25T11:44:12","date_gmt":"2026-07-25T11:44:12","guid":{"rendered":"https:\/\/www.newsbeep.com\/ie\/567468\/"},"modified":"2026-07-25T11:44:12","modified_gmt":"2026-07-25T11:44:12","slug":"the-underused-git-safety-net-that-can-save-you-from-catastrophic-code-loss","status":"publish","type":"post","link":"https:\/\/www.newsbeep.com\/ie\/567468\/","title":{"rendered":"The underused Git safety net that can save you from catastrophic code loss"},"content":{"rendered":"<p>It\u2019s fair to say that git is one of the most complicated commands you have installed on your machine. With over 180 subcommands (in version 2.55), it\u2019s also a pretty good bet that there are one or two you\u2019ve never even heard of, let alone tried out.<\/p>\n<p>The reflog subcommand is one of these: use it, and you can recover from all sorts of otherwise catastrophic mistakes, but you\u2019re probably not using it. However, with a quick introduction and a small amount of practice, you\u2019ll be able to deploy this safety net and avert disaster with one command.<\/p>\n<p>                        git reflog: like git log, but for everything<\/p>\n<p>            This subcommand reveals a record of all previous local actions<\/p>\n<p>Git can sometimes give off the impression that it records everything you do, and\u2014for the most part\u2014that\u2019s pretty accurate. But the standard git log doesn\u2019t tell you the full story, so where do you go to find out more? Reflog.<\/p>\n<p>It\u2019s important to understand the differences between git log and git reflog because you can get very different output from the two. For a trivial, one-commit repository on your local machine, the two are basically the same:<\/p>\n<p>        <img width=\"825\" height=\"183\" loading=\"lazy\" decoding=\"async\" alt=\"The git reflog command showing a single action representing the initial commit.\" data-img-url=\"https:\/\/www.newsbeep.com\/ie\/wp-content\/uploads\/2026\/07\/git-reflog-new-repo.png\" src=\"https:\/\/www.newsbeep.com\/ie\/wp-content\/uploads\/2026\/07\/git-reflog-new-repo.png\" class=\"img-brightness-opt-out\"\/><\/p>\n<p> But with a much older repository, cloned from a remote, you\u2019ll see big differences:<\/p>\n<p>        <img width=\"825\" height=\"244\" loading=\"lazy\" decoding=\"async\" alt=\"The git reflog command on a freshly-cloned repository shows just a single entry, even when the repo has many thousands of commits.\" data-img-url=\"https:\/\/www.newsbeep.com\/ie\/wp-content\/uploads\/2026\/07\/git-reflog-curl-clone.png\" src=\"https:\/\/www.newsbeep.com\/ie\/wp-content\/uploads\/2026\/07\/git-reflog-curl-clone.png\" class=\"img-brightness-opt-out\"\/><\/p>\n<p> What\u2019s going on here is that git log is showing you a history of commits that are reachable from your current commit. Meanwhile, git reflog shows a log of reference actions in the local repository. Actions can be things like <a href=\"https:\/\/www.howtogeek.com\/869305\/git-commit\/\" target=\"_blank\" rel=\"nofollow noopener\">a commit<\/a>, <a href=\"https:\/\/www.howtogeek.com\/853521\/git-merge\/\" target=\"_blank\" rel=\"nofollow noopener\">a merge<\/a>, or <a href=\"https:\/\/www.howtogeek.com\/849210\/git-rebase\/\" target=\"_blank\" rel=\"nofollow noopener\">a rebase<\/a>. They also include some interactions with a remote, like <a href=\"https:\/\/www.howtogeek.com\/451360\/how-to-clone-a-github-repository\/\" target=\"_blank\" rel=\"nofollow noopener\">cloning<\/a> or pulling from upstream.<\/p>\n<p>The basic git reflog command is a shorthand for the default git reflog show subcommand. This is actually a synonym for a specific call to git log, namely:<\/p>\n<p>git log &#8211;walk-reflogs &#8211;abbrev-commit &#8211;pretty=oneline<\/p>\n<p>Here, &#8211;walk-reflogs shows reflog entries instead of commits, &#8211;pretty=oneline shows each entry on one line, and &#8211;abbrev-commit produces much shorter commit IDs (e.g., &#8220;771eb26&#8221; instead of &#8220;771eb26cc738ab21d9d4e3ec7e3b27c51fb0fa56&#8221;). But &#8220;reflog&#8221; is much easier to remember than all that, and there are several additional subcommands for maintenance, including list, delete, and expire.<\/p>\n<p>You can make commit hashes even shorter using &#8211;abbrev=n to specify the minimum length, which is 7 by default and can be as low as 4.<\/p>\n<p>                        An example: undoing an amend<\/p>\n<p>            How to recover from a potential catastrophe with the right detective tool<\/p>\n<p>Git\u2019s reflog command is so useful because it provides access to local actions that were never pushed to a server. These are fragile since there\u2019s no record of them, other than on your local machine. The reflog command doesn\u2019t do any recovery itself, but it enables it by revealing the SHA1 hash of a commit that might otherwise be irretrievable.<\/p>\n<p>Take a common example that can result in data loss: amending changes. Using git commit &#8211;amend, you can change the last commit, rewriting its message or altering the file changes it contains. For example, you might immediately spot a typo that needs correcting:<\/p>\n<p>git add .<br \/>\ngit commit -m &#8216;bug fix for #672&#8217;<br \/>\ngit commit &#8211;amend -m &#8216;bug fix for #673&#8217;<\/p>\n<p>This is a great feature, but you need to be aware of what\u2019s going on in the background. When you use &#8211;amend, git creates a new commit to replace the previous commit. If you start with something like this:<\/p>\n<p>        <img width=\"1650\" height=\"1032\" loading=\"lazy\" decoding=\"async\" alt=\"A diagram showing the HEAD reference pointing to a main branch which points to a commit with two ancestors.\" data-img-url=\"https:\/\/www.newsbeep.com\/ie\/wp-content\/uploads\/2026\/07\/git-three-commits.png\" src=\"https:\/\/www.newsbeep.com\/ie\/wp-content\/uploads\/2026\/07\/git-three-commits.png\" class=\"img-brightness-opt-out\"\/><\/p>\n<p> You\u2019ll end up with the following: a new commit in place of the old one, which itself is left unreachable:<\/p>\n<p>        <img width=\"1650\" height=\"1420\" loading=\"lazy\" decoding=\"async\" alt=\"A diagram showing the HEAD reference pointing to a main branch which points to a commit with two ancestors. Another, unreachable commit is present, which used to have the same two ancestors.\" data-img-url=\"https:\/\/www.newsbeep.com\/ie\/wp-content\/uploads\/2026\/07\/git-one-amend.png\" src=\"https:\/\/www.newsbeep.com\/ie\/wp-content\/uploads\/2026\/07\/git-one-amend.png\" class=\"img-brightness-opt-out\"\/><\/p>\n<p> In this situation, though, git reflog can help you locate that lost commit, in case you amended but later decided you didn&#8217;t mean to. To begin, run git reflog and identify the SHA1 hash of the lost commit:<\/p>\n<p>        <img width=\"825\" height=\"211\" loading=\"lazy\" decoding=\"async\" alt=\"The git reflog command showing 4 actions, one of which is an amended commit.\" data-img-url=\"https:\/\/www.newsbeep.com\/ie\/wp-content\/uploads\/2026\/07\/git-reflog-with-amend.png\" src=\"https:\/\/www.newsbeep.com\/ie\/wp-content\/uploads\/2026\/07\/git-reflog-with-amend.png\" class=\"img-brightness-opt-out\"\/><\/p>\n<p> With the SHA1\u2014117ed62\u2014in hand, you can now use git-checkout to revert the change:<\/p>\n<p>git checkout 117ed62<\/p>\n<p>Note that git log now reports the original three commits, without the amend:<\/p>\n<p>        <img width=\"825\" height=\"179\" loading=\"lazy\" decoding=\"async\" alt=\"The git log command showing a reverted commit.\" data-img-url=\"https:\/\/www.newsbeep.com\/ie\/wp-content\/uploads\/2026\/07\/git-checkout-after-amend.png\" src=\"https:\/\/www.newsbeep.com\/ie\/wp-content\/uploads\/2026\/07\/git-checkout-after-amend.png\" class=\"img-brightness-opt-out\"\/><\/p>\n<p> Meanwhile, git reflog produces even more information than before:<\/p>\n<p>        <img width=\"825\" height=\"225\" loading=\"lazy\" decoding=\"async\" alt=\"The git reflog command showing three commits, an amended commit, and a reverted commit.\" data-img-url=\"https:\/\/www.newsbeep.com\/ie\/wp-content\/uploads\/2026\/07\/git-reflog-after-amend-checkout.png\" src=\"https:\/\/www.newsbeep.com\/ie\/wp-content\/uploads\/2026\/07\/git-reflog-after-amend-checkout.png\" class=\"img-brightness-opt-out\"\/><\/p>\n<p>                        Using reflog identifiers<\/p>\n<p>            Most commands support alternatives to commit IDs<\/p>\n<p>Many git commands work with the SHA1 hash to identify a commit ID, like git log 3dde11f. However, git reflog prints relative IDs, like HEAD@{4}, alongside each action. These IDs let you identify a specific action with greater granularity than just a commit ID.<\/p>\n<p>You can use reflog IDs with many git commands, including diff, log, &#8230; When used with git log, they can reveal otherwise unreachable commits, much like reflog does. You can omit the prefix before the @ to default to the current branch.<\/p>\n<p>If you\u2019re looking for reflog IDs in a long log, try using git reflog &#8211;oneline &#8211;no-decorate. The &#8211;no-decorate option prevents reflog from labelling references like branches and tags, so IDs will align and be easier to read.<\/p>\n<p>            The reflog subcommand is your ultimate recovery tool<\/p>\n<p>OK, reflog doesn\u2019t directly recover anything for you, but it empowers you\u2014via other git tools and commands\u2014to do just that. With the SHA1 of any unreachable commits, you can recover from almost any git action, however destructive it may have seemed at the time.<\/p>\n","protected":false},"excerpt":{"rendered":"It\u2019s fair to say that git is one of the most complicated commands you have installed on your&hellip;\n","protected":false},"author":2,"featured_media":567469,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[61,60,82],"class_list":["post-567468","post","type-post","status-publish","format-standard","has-post-thumbnail","category-science","tag-ie","tag-ireland","tag-science"],"_links":{"self":[{"href":"https:\/\/www.newsbeep.com\/ie\/wp-json\/wp\/v2\/posts\/567468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.newsbeep.com\/ie\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.newsbeep.com\/ie\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.newsbeep.com\/ie\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.newsbeep.com\/ie\/wp-json\/wp\/v2\/comments?post=567468"}],"version-history":[{"count":0,"href":"https:\/\/www.newsbeep.com\/ie\/wp-json\/wp\/v2\/posts\/567468\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.newsbeep.com\/ie\/wp-json\/wp\/v2\/media\/567469"}],"wp:attachment":[{"href":"https:\/\/www.newsbeep.com\/ie\/wp-json\/wp\/v2\/media?parent=567468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.newsbeep.com\/ie\/wp-json\/wp\/v2\/categories?post=567468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.newsbeep.com\/ie\/wp-json\/wp\/v2\/tags?post=567468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}