{"id":181,"date":"2018-08-23T12:14:09","date_gmt":"2018-08-22T23:14:09","guid":{"rendered":"http:\/\/blog.wiseowls.co.nz\/?p=181"},"modified":"2026-03-08T00:49:15","modified_gmt":"2026-03-07T11:49:15","slug":"oops-i-did-it-again-git-disaster-recovery","status":"publish","type":"post","link":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/08\/23\/oops-i-did-it-again-git-disaster-recovery\/","title":{"rendered":"Oops I did it again: git disaster recovery"},"content":{"rendered":"<h3 class=\"wp-block-heading\">Houston, we have a problem<\/h3>\n<p>So, you&#8217;ve accidentally pushed your commit before realising it breaks everything? Or maybe the merge you&#8217;ve been working on for the last 2 hours has turned out to have gone south in the most inexplicable and unimaginable way? Okay, no worries, we&#8217;re all use git nowadays, right?<br \/>When I end up in situation like this (and I find myself there quite more often than I&#8217;d like to admit), this is the decision tree I follow to get back up and running:<br \/><img decoding=\"async\" class=\"drawit-img aligncenter wp-image-207\" src=\"https:\/\/blog.wiseowls.co.nz\/wp-content\/uploads\/2018\/08\/drawit-diagram-1.svg\" alt=\"Decision tree flowchart for git disaster recovery scenarios\" title=\"drawit diagram\"><br \/>I am assuming you&#8217;ve got your working copy checked out and you have backed it up prior to running any commands pasted from the internet.<\/p>\n<h2 class=\"wp-block-heading\">To rewrite or not to rewrite<\/h2>\n<p>Generally speaking you should avoid rewriting history if you could. Changing commit chain would affect other developers that might have been relying on some of the commits in their feature branches. This would mean more clean up for everyone in the team and more potential stuff-ups. Sometimes, however, there&#8217;s just no choice but to rewrite. For example when someone checks in their private keys or large binary files that just do not belong in the repo.<\/p>\n<h2 class=\"wp-block-heading\"><a name=\"revert-commit\"><\/a>Revert last commit<\/h2>\n<p>This does not roll back. It in fact creates a new commit, that ends up negating the previous one.<\/p>\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">git revert HEAD\ngit push<\/code><\/pre>\n<h2 class=\"wp-block-heading\"><a name=\"reset\"><\/a>Reset master to previous commit<\/h2>\n<p>This is by far the most common scenario. Note the <b>^<\/b> symbol at the end. It tells git to get parent of that commit instead. Very handy.<\/p>\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">git reset HEAD^ --hard\ngit push origin -f<\/code><\/pre>\n<h2 class=\"wp-block-heading\"><a name=\"rebase\"><\/a>Rebase interactively<\/h2>\n<p>It is likely that <b>vi<\/b> is the default text editor the shipped with your git installation. So before you go down this rabbit hole &#8211; make sure you understand the basics of <a href=\"http:\/\/www.ks.uiuc.edu\/Training\/Tutorials\/Reference\/virefcard.pdf\">text editing with vi<\/a>.<\/p>\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">git log --pretty=oneline\ngit rebase -i ^<\/code><\/pre>\n<p>you will be presented with a file with commit chain one per line along with action codes git should run over each commit. Don&#8217;t worry, quick command reference is just down the bottom. Once you have saved and exited &#8211; git will run commands sequentially producing the desired state in the end.<\/p>\n<h2 class=\"wp-block-heading\"><a name=\"cherrypick\"><\/a>Go cherry picking<\/h2>\n<p>Enjoy your cherry picking responsibly. Cherry picking is generally frowned upon in the community as it essentially creates a duplicate commit with another SHA. But our justification here would be we&#8217;re ultimately going to drop the old branch so there&#8217;d be no double-ups in the end. My strategy here is to branch off a last known good state, cherry pick all good commits over to new branch. Finally swap branches around and kill the old one. This is not the end of story however as all other developers would need to check out the new branch and potentially rebase their unmerged changes. Once again &#8211; use with care.<\/p>\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">git log --pretty=oneline\ngit branch fixing_master ^\ngit push<\/code><\/pre>\n<h2 class=\"wp-block-heading\"><a name=\"restore-file\"><\/a>Restore deleted file<\/h2>\n<p>This is pretty straightforward. First we find the last commit to have touched the file in question (deleting it). And then we check out a copy from parent commit (which would be the last version available at the time):<\/p>\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$file='\/path\/to\/file'\ngit checkout $(git rev-list -n 1 HEAD -- \"$file\")^ -- \"$file\"<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Houston, we have a problem So, you&#8217;ve accidentally pushed your commit before realising it breaks everything? Or maybe the merge you&#8217;ve been working on for the last 2 hours has turned out to have gone south in the most inexplicable and unimaginable way? Okay, no worries, we&#8217;re all use git nowadays, right?When I end up &hellip; <a href=\"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/08\/23\/oops-i-did-it-again-git-disaster-recovery\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Oops I did it again: git disaster recovery&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[11],"tags":[14],"class_list":["post-181","post","type-post","status-publish","format-standard","hentry","category-dev","tag-git"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Oops I did it again: git disaster recovery - Timur and associates<\/title>\n<meta name=\"description\" content=\"Accidentally pushed something terrible? A decision tree for git disaster recovery: revert, reset, rebase, or cherry pick.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/08\/23\/oops-i-did-it-again-git-disaster-recovery\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Oops I did it again: git disaster recovery - Timur and associates\" \/>\n<meta property=\"og:description\" content=\"Accidentally pushed something terrible? A decision tree for git disaster recovery: revert, reset, rebase, or cherry pick.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/08\/23\/oops-i-did-it-again-git-disaster-recovery\/\" \/>\n<meta property=\"og:site_name\" content=\"Timur and associates\" \/>\n<meta property=\"article:published_time\" content=\"2018-08-22T23:14:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-07T11:49:15+00:00\" \/>\n<meta name=\"author\" content=\"timur\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@TimurKh\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/08\\\/23\\\/oops-i-did-it-again-git-disaster-recovery\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/08\\\/23\\\/oops-i-did-it-again-git-disaster-recovery\\\/\"},\"author\":{\"name\":\"timur\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/#\\\/schema\\\/person\\\/34d0ed30d573b5bc317ea990bd2e0c59\"},\"headline\":\"Oops I did it again: git disaster recovery\",\"datePublished\":\"2018-08-22T23:14:09+00:00\",\"dateModified\":\"2026-03-07T11:49:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/08\\\/23\\\/oops-i-did-it-again-git-disaster-recovery\\\/\"},\"wordCount\":502,\"image\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/08\\\/23\\\/oops-i-did-it-again-git-disaster-recovery\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/wp-content\\\/uploads\\\/2018\\\/08\\\/drawit-diagram-1.svg\",\"keywords\":[\"git\"],\"articleSection\":[\"Development\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/08\\\/23\\\/oops-i-did-it-again-git-disaster-recovery\\\/\",\"url\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/08\\\/23\\\/oops-i-did-it-again-git-disaster-recovery\\\/\",\"name\":\"Oops I did it again: git disaster recovery - Timur and associates\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/08\\\/23\\\/oops-i-did-it-again-git-disaster-recovery\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/08\\\/23\\\/oops-i-did-it-again-git-disaster-recovery\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/wp-content\\\/uploads\\\/2018\\\/08\\\/drawit-diagram-1.svg\",\"datePublished\":\"2018-08-22T23:14:09+00:00\",\"dateModified\":\"2026-03-07T11:49:15+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/#\\\/schema\\\/person\\\/34d0ed30d573b5bc317ea990bd2e0c59\"},\"description\":\"Accidentally pushed something terrible? A decision tree for git disaster recovery: revert, reset, rebase, or cherry pick.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/08\\\/23\\\/oops-i-did-it-again-git-disaster-recovery\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/08\\\/23\\\/oops-i-did-it-again-git-disaster-recovery\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/08\\\/23\\\/oops-i-did-it-again-git-disaster-recovery\\\/#primaryimage\",\"url\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/wp-content\\\/uploads\\\/2018\\\/08\\\/drawit-diagram-1.svg\",\"contentUrl\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/wp-content\\\/uploads\\\/2018\\\/08\\\/drawit-diagram-1.svg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2018\\\/08\\\/23\\\/oops-i-did-it-again-git-disaster-recovery\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Oops I did it again: git disaster recovery\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/#website\",\"url\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/\",\"name\":\"Timur and associates\",\"description\":\"Notes of an IT contractor\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/#\\\/schema\\\/person\\\/34d0ed30d573b5bc317ea990bd2e0c59\",\"name\":\"timur\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg\",\"caption\":\"timur\"},\"sameAs\":[\"https:\\\/\\\/x.com\\\/TimurKh\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Oops I did it again: git disaster recovery - Timur and associates","description":"Accidentally pushed something terrible? A decision tree for git disaster recovery: revert, reset, rebase, or cherry pick.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/08\/23\/oops-i-did-it-again-git-disaster-recovery\/","og_locale":"en_US","og_type":"article","og_title":"Oops I did it again: git disaster recovery - Timur and associates","og_description":"Accidentally pushed something terrible? A decision tree for git disaster recovery: revert, reset, rebase, or cherry pick.","og_url":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/08\/23\/oops-i-did-it-again-git-disaster-recovery\/","og_site_name":"Timur and associates","article_published_time":"2018-08-22T23:14:09+00:00","article_modified_time":"2026-03-07T11:49:15+00:00","author":"timur","twitter_card":"summary_large_image","twitter_creator":"@TimurKh","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/08\/23\/oops-i-did-it-again-git-disaster-recovery\/#article","isPartOf":{"@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/08\/23\/oops-i-did-it-again-git-disaster-recovery\/"},"author":{"name":"timur","@id":"https:\/\/blog.wiseowls.co.nz\/#\/schema\/person\/34d0ed30d573b5bc317ea990bd2e0c59"},"headline":"Oops I did it again: git disaster recovery","datePublished":"2018-08-22T23:14:09+00:00","dateModified":"2026-03-07T11:49:15+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/08\/23\/oops-i-did-it-again-git-disaster-recovery\/"},"wordCount":502,"image":{"@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/08\/23\/oops-i-did-it-again-git-disaster-recovery\/#primaryimage"},"thumbnailUrl":"https:\/\/blog.wiseowls.co.nz\/wp-content\/uploads\/2018\/08\/drawit-diagram-1.svg","keywords":["git"],"articleSection":["Development"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/08\/23\/oops-i-did-it-again-git-disaster-recovery\/","url":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/08\/23\/oops-i-did-it-again-git-disaster-recovery\/","name":"Oops I did it again: git disaster recovery - Timur and associates","isPartOf":{"@id":"https:\/\/blog.wiseowls.co.nz\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/08\/23\/oops-i-did-it-again-git-disaster-recovery\/#primaryimage"},"image":{"@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/08\/23\/oops-i-did-it-again-git-disaster-recovery\/#primaryimage"},"thumbnailUrl":"https:\/\/blog.wiseowls.co.nz\/wp-content\/uploads\/2018\/08\/drawit-diagram-1.svg","datePublished":"2018-08-22T23:14:09+00:00","dateModified":"2026-03-07T11:49:15+00:00","author":{"@id":"https:\/\/blog.wiseowls.co.nz\/#\/schema\/person\/34d0ed30d573b5bc317ea990bd2e0c59"},"description":"Accidentally pushed something terrible? A decision tree for git disaster recovery: revert, reset, rebase, or cherry pick.","breadcrumb":{"@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/08\/23\/oops-i-did-it-again-git-disaster-recovery\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/08\/23\/oops-i-did-it-again-git-disaster-recovery\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/08\/23\/oops-i-did-it-again-git-disaster-recovery\/#primaryimage","url":"https:\/\/blog.wiseowls.co.nz\/wp-content\/uploads\/2018\/08\/drawit-diagram-1.svg","contentUrl":"https:\/\/blog.wiseowls.co.nz\/wp-content\/uploads\/2018\/08\/drawit-diagram-1.svg"},{"@type":"BreadcrumbList","@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2018\/08\/23\/oops-i-did-it-again-git-disaster-recovery\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.wiseowls.co.nz\/"},{"@type":"ListItem","position":2,"name":"Oops I did it again: git disaster recovery"}]},{"@type":"WebSite","@id":"https:\/\/blog.wiseowls.co.nz\/#website","url":"https:\/\/blog.wiseowls.co.nz\/","name":"Timur and associates","description":"Notes of an IT contractor","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.wiseowls.co.nz\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.wiseowls.co.nz\/#\/schema\/person\/34d0ed30d573b5bc317ea990bd2e0c59","name":"timur","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg","caption":"timur"},"sameAs":["https:\/\/x.com\/TimurKh"]}]}},"_links":{"self":[{"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/posts\/181","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/comments?post=181"}],"version-history":[{"count":24,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/posts\/181\/revisions"}],"predecessor-version":[{"id":1363,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/posts\/181\/revisions\/1363"}],"wp:attachment":[{"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/media?parent=181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/categories?post=181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/tags?post=181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}