{"id":3873,"date":"2022-08-20T21:23:49","date_gmt":"2022-08-20T15:53:49","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/"},"modified":"2022-08-20T21:23:49","modified_gmt":"2022-08-20T15:53:49","slug":"solved-how-do-i-revert-a-git-repository-to-a-previous-commit","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/","title":{"rendered":"(Solved) How do I revert a Git repository to a previous commit?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-4114122\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"4114122\" data-parentid=\"4114095\" data-score=\"11569\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>This depends a lot on what you mean by &#8220;revert&#8221;.<\/p>\n<h2>Temporarily switch to a different commit<\/h2>\n<p>If you want to temporarily go back to it, fool around, then come back to where you are, all you have to do is check out the desired commit:<\/p>\n<pre class=\"lang-sh prettyprint-override\"><code># This will detach your HEAD, that is, leave you with no branch checked out:\ngit checkout 0d1d7fc32\n<\/code><\/pre>\n<p>Or if you want to make commits while you&#8217;re there, go ahead and make a new branch while you&#8217;re at it:<\/p>\n<pre><code>git checkout -b old-state 0d1d7fc32\n<\/code><\/pre>\n<p>To go back to where you were, just check out the branch you were on again. (If you&#8217;ve made changes, as always when switching branches, you&#8217;ll have to deal with them as appropriate. You could reset to throw them away; you could stash, checkout, stash pop to take them with you; you could commit them to a branch there if you want a branch there.)<\/p>\n<h2>Hard delete unpublished commits<\/h2>\n<p>If, on the other hand, you want to really get rid of everything you&#8217;ve done since then, there are two possibilities. One, if you haven&#8217;t published any of these commits, simply reset:<\/p>\n<pre class=\"lang-sh prettyprint-override\"><code># This will destroy any local modifications.\n# Don't do it if you have uncommitted work you want to keep.\ngit reset --hard 0d1d7fc32\n\n# Alternatively, if there's work to keep:\ngit stash\ngit reset --hard 0d1d7fc32\ngit stash pop\n# This saves the modifications, then reapplies that patch after resetting.\n# You could get merge conflicts, if you've modified things which were\n# changed since the commit you reset to.\n<\/code><\/pre>\n<p>If you mess up, you&#8217;ve already thrown away your local changes, but you can at least get back to where you were before by resetting again.<\/p>\n<h2>Undo published commits with new commits<\/h2>\n<p>On the other hand, if you&#8217;ve published the work, you probably don&#8217;t want to reset the branch, since that&#8217;s effectively rewriting history. In that case, you could indeed revert the commits. With Git, revert has a very specific meaning: create a commit with the reverse patch to cancel it out. This way you don&#8217;t rewrite any history.<\/p>\n<pre class=\"lang-sh prettyprint-override\"><code># This will create three separate revert commits:\ngit revert a867b4af 25eee4ca 0766c053\n\n# It also takes ranges. This will revert the last two commits:\ngit revert HEAD~2..HEAD\n\n#Similarly, you can revert a range of commits using commit hashes (non inclusive of first hash):\ngit revert 0d1d7fc..a867b4a\n\n# Reverting a merge commit\ngit revert -m 1 &lt;merge_commit_sha&gt;\n\n# To get just one, you could use `rebase -i` to squash them afterwards\n# Or, you could do it manually (be sure to do this at top level of the repo)\n# get your index and work tree into the desired state, without changing HEAD:\ngit checkout 0d1d7fc32 .\n\n# Then commit. Be sure and write a good message describing what you just did\ngit commit\n<\/code><\/pre>\n<p>The <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/git-scm.com\/docs\/git-revert\"><code>git-revert<\/code> manpage<\/a> actually covers a lot of this in its description. Another useful link is <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/git-scm.com\/book\/en\/v2\/Git-Tools-Advanced-Merging#_undoing_merges\">this git-scm.com section discussing git-revert<\/a>.<\/p>\n<p>If you decide you didn&#8217;t want to revert after all, you can revert the revert (as described here) or reset back to before the revert (see the previous section).<\/p>\n<p>You may also find this answer helpful in this case:<br \/>How can I move HEAD back to a previous location? (Detached head) &amp; Undo commits<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">8<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How do I revert a Git repository to a previous commit? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] This depends a lot on what you mean by &#8220;revert&#8221;. Temporarily switch to a different commit If you want to temporarily go back to it, fool around, then come back to where you are, all you have to do is check out the desired commit: # This will detach your HEAD, that is, leave &#8230; <a title=\"(Solved) How do I revert a Git repository to a previous commit?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/\" aria-label=\"More on (Solved) How do I revert a Git repository to a previous commit?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[329,398,416,417],"class_list":["post-3873","post","type-post","status-publish","format-standard","hentry","category-solved","tag-git","tag-git-checkout","tag-git-reset","tag-git-revert"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>(Solved) How do I revert a Git repository to a previous commit? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"(Solved) How do I revert a Git repository to a previous commit? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] This depends a lot on what you mean by &#8220;revert&#8221;. Temporarily switch to a different commit If you want to temporarily go back to it, fool around, then come back to where you are, all you have to do is check out the desired commit: # This will detach your HEAD, that is, leave ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-20T15:53:49+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"(Solved) How do I revert a Git repository to a previous commit?\",\"datePublished\":\"2022-08-20T15:53:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/\"},\"wordCount\":369,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"git\",\"git-checkout\",\"git-reset\",\"git-revert\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/\",\"name\":\"(Solved) How do I revert a Git repository to a previous commit? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-08-20T15:53:49+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"(Solved) How do I revert a Git repository to a previous commit?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"(Solved) How do I revert a Git repository to a previous commit? - JassWeb","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:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/","og_locale":"en_US","og_type":"article","og_title":"(Solved) How do I revert a Git repository to a previous commit? - JassWeb","og_description":"[ad_1] This depends a lot on what you mean by &#8220;revert&#8221;. Temporarily switch to a different commit If you want to temporarily go back to it, fool around, then come back to where you are, all you have to do is check out the desired commit: # This will detach your HEAD, that is, leave ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/","og_site_name":"JassWeb","article_published_time":"2022-08-20T15:53:49+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"(Solved) How do I revert a Git repository to a previous commit?","datePublished":"2022-08-20T15:53:49+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/"},"wordCount":369,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["git","git-checkout","git-reset","git-revert"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/","url":"https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/","name":"(Solved) How do I revert a Git repository to a previous commit? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-20T15:53:49+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-do-i-revert-a-git-repository-to-a-previous-commit\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"(Solved) How do I revert a Git repository to a previous commit?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/3873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=3873"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/3873\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=3873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=3873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=3873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}