{"id":20056,"date":"2020-12-22T02:14:50","date_gmt":"2020-12-22T10:14:50","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=20056"},"modified":"2023-12-01T04:06:21","modified_gmt":"2023-12-01T12:06:21","slug":"git-undo-merge","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/git-undo-merge\/","title":{"rendered":"Git Undo Merge: A Guide"},"content":{"rendered":"\n<p><em>You can undo a Git merge using the git reset &#8211;merge command. This command changes all files that are different between your current repository and a particular commit. There is no &#8220;git undo merge&#8221; command but the git reset command works well to undo a merge.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Undo a Git Merge<\/h2>\n\n\n\n<p>Have you just merged two branches that you\u2019re not ready to merge? Not to worry, <a href=\"https:\/\/careerkarma.com\/blog\/what-is-git\/\">Git<\/a> has a solution for you. Developers merge branches all the time that should not be branched, so you\u2019re definitely not alone in experiencing this issue.\n\n<\/p>\n\n\n\n<p>In this tutorial, we\u2019re going to talk about git merges and how to undo a git merge. We\u2019ll walk through an example of two approaches you can use to undo a git merge. Let\u2019s begin!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Git Merges<\/h2>\n\n\n\n<p>Git relies heavily on branches. These are used to maintain separate lines of development inside a project. Branches allow you to work on multiple different versions of a repository at once. Merging combines <a href=\"https:\/\/careerkarma.com\/blog\/git-merge\">two branches into one<\/a>. <\/p>\n\n\n\n<p>You can merge a branch into another branch whenever you are ready. This means that you can develop a feature or a bug fix on a separate branch. Then, you can make it part of your main project later.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Git Undo Merge<\/h2>\n\n\n\n<p>To undo a git merge, you need to find the commit ID of your last commit. Then, you need to use the git reset command to reset your repository to its state in that commit. There is no &#8220;git revert merge&#8221; command.<\/p>\n\n\n\n<p>The steps to revert a merge, in order, are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>git log OR git reflog (to find the last commit ID)<\/li><li>git reset &#8211;merge (to revert to the commit you specify)<\/li><\/ul>\n\n\n\n<p>Say we have accidentally merged two branches that should not be merged. We can undo these changes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Undo Merge Git Example<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Find the Commit ID<\/h3>\n\n\n\n<p>To start, we need to find out the commit ID of the commit before our merge on our remote repository. You can do this using git reflog:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>git reflog<\/pre><\/div>\n\n\n\n<p>Let\u2019s run this command on a Git repository:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>ac7188c HEAD@{6}: commit: feat: Push example code for innerText and innerHTML tutorial\na9fdeb5 HEAD@{7}: commit (initial): feat: Merge dev-fix-7 into master<\/pre><\/div>\n\n\n\n<p>This command tells us that the last commit has the hash a9fdeb5.<\/p>\n\n\n\n<p>Alternatively, you can use the <a href=\"https:\/\/careerkarma.com\/blog\/git-log\/\">git log command<\/a>. But, the reflog command returns an output that is easier to read. This is why we opted to use reflog instead of the log command.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Revert to the Commit<\/h3>\n\n\n\n<p>We can use this hash to revert the merge commit with the <a href=\"https:\/\/careerkarma.com\/blog\/git-revert-commit\/\">git reset<\/a> \u2013merge command:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>git reset --merge a9fdeb5<\/pre><\/div>\n\n\n\n<p>This command resets our repository to the state it was at in the a9fdeb5 commit on the master branch.\n\n<\/p>\n\n\n\n<p>The \u2013merge flag resets an index and updates all the files that are different between the current state of your repository and the HEAD.\n\n<\/p>\n\n\n\n<p>This flag does not reset files with changes that have not been added to a commit. This makes it safer than using the oft-recommended git reset \u2013hard command.<\/p>\n\n\n\n<p>Once we have reset our repository, we can make the relevant changes to our code. Then, we can push them to a remote branch using the <a href=\"https:\/\/careerkarma.com\/blog\/git-push\/\">git push command<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A Word on the HEAD Shorthand<\/h2>\n\n\n\n<p>The Git HEAD keyword refers to the latest commit in your repository. You can use the <a href=\"https:\/\/careerkarma.com\/blog\/what-is-a-git-head\/\">Git HEAD shorthand<\/a> to undo a merge:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>git reset --merge HEAD~1<\/pre><\/div>\n\n\n\n<p>This command reverts our repository to the last commit. HEAD refers to the current state of your repository; HEAD~1 is the last commit in your repository.<\/p>\n\n\n\n<p>This command reverts our repository to the last commit. HEAD refers to the current state of your repository; HEAD~1 is the last commit in your repository.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The git reset command undoes a merge. The \u2013merge flag resets a repository but keeps files to which changes have been made that have not been added to your repository.<\/p>\n\n\n\n<p>Are you interested in learning more about Git? Check out our <a href=\"https:\/\/careerkarma.com\/blog\/what-is-git\/\">How to Learn Git guide<\/a>. In this guide, you&#8217;ll find advice on how to learn Git and a list of top learning resources.<\/p>\n","protected":false},"excerpt":{"rendered":"You can undo a Git merge using the git reset --merge command. This command changes all files that are different between your current repository and a particular commit. There is no \"git undo merge\" command but the git reset command works well to undo a merge. How to Undo a Git Merge Have you just&hellip;","protected":false},"author":240,"featured_media":20057,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[17286],"tags":[],"class_list":{"0":"post-20056","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-git"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Coding","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Git Undo Merge: A Step-By-Step Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"The git reset --merge command allows you to undo a merge. On Career Karma, learn how to perform a Git undo merge operation.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/git-undo-merge\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Git Undo Merge: A Guide\" \/>\n<meta property=\"og:description\" content=\"The git reset --merge command allows you to undo a merge. On Career Karma, learn how to perform a Git undo merge operation.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/git-undo-merge\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-22T10:14:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T12:06:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/joshua-aragon-BMnhuwFYr7w-unsplash.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"680\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\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:\/\/careerkarma.com\/blog\/git-undo-merge\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/git-undo-merge\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Git Undo Merge: A Guide\",\"datePublished\":\"2020-12-22T10:14:50+00:00\",\"dateModified\":\"2023-12-01T12:06:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/git-undo-merge\/\"},\"wordCount\":666,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/git-undo-merge\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/joshua-aragon-BMnhuwFYr7w-unsplash.jpg\",\"articleSection\":[\"Git\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/git-undo-merge\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/git-undo-merge\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/git-undo-merge\/\",\"name\":\"Git Undo Merge: A Step-By-Step Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/git-undo-merge\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/git-undo-merge\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/joshua-aragon-BMnhuwFYr7w-unsplash.jpg\",\"datePublished\":\"2020-12-22T10:14:50+00:00\",\"dateModified\":\"2023-12-01T12:06:21+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"The git reset --merge command allows you to undo a merge. On Career Karma, learn how to perform a Git undo merge operation.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/git-undo-merge\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/git-undo-merge\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/git-undo-merge\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/joshua-aragon-BMnhuwFYr7w-unsplash.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/joshua-aragon-BMnhuwFYr7w-unsplash.jpg\",\"width\":1020,\"height\":680},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/git-undo-merge\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Coding\",\"item\":\"https:\/\/careerkarma.com\/blog\/code\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Git Undo Merge: A Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Git Undo Merge: A Step-By-Step Guide | Career Karma","description":"The git reset --merge command allows you to undo a merge. On Career Karma, learn how to perform a Git undo merge operation.","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:\/\/careerkarma.com\/blog\/git-undo-merge\/","og_locale":"en_US","og_type":"article","og_title":"Git Undo Merge: A Guide","og_description":"The git reset --merge command allows you to undo a merge. On Career Karma, learn how to perform a Git undo merge operation.","og_url":"https:\/\/careerkarma.com\/blog\/git-undo-merge\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-12-22T10:14:50+00:00","article_modified_time":"2023-12-01T12:06:21+00:00","og_image":[{"width":1020,"height":680,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/joshua-aragon-BMnhuwFYr7w-unsplash.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/git-undo-merge\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/git-undo-merge\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Git Undo Merge: A Guide","datePublished":"2020-12-22T10:14:50+00:00","dateModified":"2023-12-01T12:06:21+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/git-undo-merge\/"},"wordCount":666,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/git-undo-merge\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/joshua-aragon-BMnhuwFYr7w-unsplash.jpg","articleSection":["Git"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/git-undo-merge\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/git-undo-merge\/","url":"https:\/\/careerkarma.com\/blog\/git-undo-merge\/","name":"Git Undo Merge: A Step-By-Step Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/git-undo-merge\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/git-undo-merge\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/joshua-aragon-BMnhuwFYr7w-unsplash.jpg","datePublished":"2020-12-22T10:14:50+00:00","dateModified":"2023-12-01T12:06:21+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"The git reset --merge command allows you to undo a merge. On Career Karma, learn how to perform a Git undo merge operation.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/git-undo-merge\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/git-undo-merge\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/git-undo-merge\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/joshua-aragon-BMnhuwFYr7w-unsplash.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/joshua-aragon-BMnhuwFYr7w-unsplash.jpg","width":1020,"height":680},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/git-undo-merge\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Coding","item":"https:\/\/careerkarma.com\/blog\/code\/"},{"@type":"ListItem","position":3,"name":"Git Undo Merge: A Guide"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/20056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=20056"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/20056\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/20057"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=20056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=20056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=20056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}