{"id":988,"date":"2016-08-23T16:45:22","date_gmt":"2016-08-23T14:45:22","guid":{"rendered":"http:\/\/blog.thecodecampus.de\/?p=988"},"modified":"2024-06-12T15:27:04","modified_gmt":"2024-06-12T13:27:04","slug":"git-change-files-previous-commit","status":"publish","type":"post","link":"https:\/\/www.thecodecampus.de\/blog\/git-change-files-previous-commit\/","title":{"rendered":"Undo and edit your last commits with git"},"content":{"rendered":"<p>Sometimes one might get into the unfortunate need to change a git commit. In this post I will show some options to revert or change commits which are not yet pushed to an remote branch.<!--more--><\/p>\n<h4>Undo the last commit<\/h4>\n<p>It&#8217;s quite easy to undo the last git commit using:<\/p>\n<p><code>git reset HEAD~<\/code><\/p>\n<p>This will remove the very last commit from your local working tree. Your latest changes will still be there and you can just edit them to your needs. When done use<br \/>\n<code>git add .<\/code><br \/>\nto add the files into your local working branch again and commit your fix.<\/p>\n<h4>Undo any commit<\/h4>\n<p>In case you need\u00a0to edit one specific commit, instead of the last one, git also enables us to do<\/p>\n<p>In this example I have two commits. I need to change the first one, while the last one should stay untouched.<\/p>\n<ol>\n<li>Pick your commits\u00a0id\u00a0from <code><code>git log<br \/>\n<\/code><\/code><\/p>\n<pre class=\"lang:default decode:true\">$ git log\r\ncommit 54092b52ac09c32b6712fd6f39e28680bf1507fa\r\nAuthor: Anonymous\r\nDate:   Tue Aug 23 16:14:18 2016 +0200\r\n\r\n    good commit\r\n\r\ncommit 5b5a753329764c9a51935b43961a6598a860f96d\r\nAuthor: Anonymous\r\nDate:   Tue Aug 23 16:13:41 2016 +0200\r\n\r\n    bad commit\r\n<\/pre>\n<p>In this\u00a0example the commit we want to edit has the id &#8216;5b5a753329764c9a51935b43961a6598a860f96d&#8217;<\/li>\n<li>Next do a git rebase: <code><code>git rebase --interactive \"COMMIT-ID\"^<br \/>\n<\/code><\/code><\/p>\n<pre class=\"lang:default decode:true \">pick 5b5a753 bad commit\r\npick 54092b5 good commit<\/pre>\n<p>This will open an text editor and will show your last commits.<\/li>\n<li>Search for your bad commit again and then replace the word pick in front with edit\n<pre class=\"lang:default decode:true \">edit 5b5a753 bad commit\r\npick 54092b5 good commit\r\n<\/pre>\n<p>If you are using nano as your editor you can close the editor with CTRL + X and then typing &#8216;y&#8217; to save it.<\/li>\n<li>Make your changes<\/li>\n<li>then add the files to the working tree again with <code>git add .<\/code><\/li>\n<li>re-add them to the commit with\u00a0<code>git commit --amend<\/code><\/li>\n<li>Eventually, use\u00a0<code>git rebase --continue to merge your changed commit with your other commit.<\/code><\/li>\n<\/ol>\n<p>Now Git will change all\u00a0subsequent commits and update the mistakes.<\/p>\n<p>If those\u00a0subsequent commits affect the corrected lines you might need to fix merge conflicts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes one might get into the unfortunate need to change a git commit. In this post I will show some options to revert or change commits which are not yet pushed to an remote branch.<\/p>\n","protected":false},"author":29,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92,79],"tags":[],"class_list":["post-988","post","type-post","status-publish","format-standard","hentry","category-others","category-tooling"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Undo and edit your last commits with git - Web Development Blog<\/title>\n<meta name=\"description\" content=\"Sometimes one might get into the unfortunate need to change a git commit. I will show some options to revert or edit commits in local working tree\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.thecodecampus.de\/blog\/git-change-files-previous-commit\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Undo and edit your last commits with git - Web Development Blog\" \/>\n<meta property=\"og:description\" content=\"Sometimes one might get into the unfortunate need to change a git commit. I will show some options to revert or edit commits in local working tree\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.thecodecampus.de\/blog\/git-change-files-previous-commit\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Development tips and tricks - theCodeCampus Blog\" \/>\n<meta property=\"article:published_time\" content=\"2016-08-23T14:45:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-12T13:27:04+00:00\" \/>\n<meta name=\"author\" content=\"theCodeCampus\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"theCodeCampus\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/git-change-files-previous-commit\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/git-change-files-previous-commit\\\/\"},\"author\":{\"name\":\"theCodeCampus\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#\\\/schema\\\/person\\\/276bbda2f8da73154f22fb652201cfbc\"},\"headline\":\"Undo and edit your last commits with git\",\"datePublished\":\"2016-08-23T14:45:22+00:00\",\"dateModified\":\"2024-06-12T13:27:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/git-change-files-previous-commit\\\/\"},\"wordCount\":276,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#organization\"},\"articleSection\":[\"Others\",\"Tooling\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/git-change-files-previous-commit\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/git-change-files-previous-commit\\\/\",\"url\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/git-change-files-previous-commit\\\/\",\"name\":\"Undo and edit your last commits with git - Web Development Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#website\"},\"datePublished\":\"2016-08-23T14:45:22+00:00\",\"dateModified\":\"2024-06-12T13:27:04+00:00\",\"description\":\"Sometimes one might get into the unfortunate need to change a git commit. I will show some options to revert or edit commits in local working tree\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/git-change-files-previous-commit\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/git-change-files-previous-commit\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/git-change-files-previous-commit\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Undo and edit your last commits with git\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/\",\"name\":\"Web Development tips and tricks - theCodeCampus Blog\",\"description\":\"Tips, tricks, and experiences about developing web and mobile applications with Angular, TypeScript, and Testing.\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#organization\",\"name\":\"theCodeCampus\",\"url\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/TCC-Logo-Bildmarke-quadratisch.jpg\",\"contentUrl\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/TCC-Logo-Bildmarke-quadratisch.jpg\",\"width\":156,\"height\":156,\"caption\":\"theCodeCampus\"},\"image\":{\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/#\\\/schema\\\/person\\\/276bbda2f8da73154f22fb652201cfbc\",\"name\":\"theCodeCampus\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/TCC-Logo-Bildmarke-quadratisch-96x96.jpg\",\"url\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/TCC-Logo-Bildmarke-quadratisch-96x96.jpg\",\"contentUrl\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/TCC-Logo-Bildmarke-quadratisch-96x96.jpg\",\"caption\":\"theCodeCampus\"},\"description\":\"Our knowledge is not simply gained through reading - it is trained, tested and constantly being expanded. Because first and foremost, we are all developers at W11K. The know-how that we acquire here as developers, consultants and information architects flows immediately into our training courses and articles for theCodeCampus.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/showcase\\\/thecodecampus\\\/\"],\"url\":\"https:\\\/\\\/www.thecodecampus.de\\\/blog\\\/author\\\/admin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Undo and edit your last commits with git - Web Development Blog","description":"Sometimes one might get into the unfortunate need to change a git commit. I will show some options to revert or edit commits in local working tree","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:\/\/www.thecodecampus.de\/blog\/git-change-files-previous-commit\/","og_locale":"en_US","og_type":"article","og_title":"Undo and edit your last commits with git - Web Development Blog","og_description":"Sometimes one might get into the unfortunate need to change a git commit. I will show some options to revert or edit commits in local working tree","og_url":"https:\/\/www.thecodecampus.de\/blog\/git-change-files-previous-commit\/","og_site_name":"Web Development tips and tricks - theCodeCampus Blog","article_published_time":"2016-08-23T14:45:22+00:00","article_modified_time":"2024-06-12T13:27:04+00:00","author":"theCodeCampus","twitter_card":"summary_large_image","twitter_misc":{"Written by":"theCodeCampus","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.thecodecampus.de\/blog\/git-change-files-previous-commit\/#article","isPartOf":{"@id":"https:\/\/www.thecodecampus.de\/blog\/git-change-files-previous-commit\/"},"author":{"name":"theCodeCampus","@id":"https:\/\/www.thecodecampus.de\/blog\/#\/schema\/person\/276bbda2f8da73154f22fb652201cfbc"},"headline":"Undo and edit your last commits with git","datePublished":"2016-08-23T14:45:22+00:00","dateModified":"2024-06-12T13:27:04+00:00","mainEntityOfPage":{"@id":"https:\/\/www.thecodecampus.de\/blog\/git-change-files-previous-commit\/"},"wordCount":276,"commentCount":0,"publisher":{"@id":"https:\/\/www.thecodecampus.de\/blog\/#organization"},"articleSection":["Others","Tooling"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.thecodecampus.de\/blog\/git-change-files-previous-commit\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.thecodecampus.de\/blog\/git-change-files-previous-commit\/","url":"https:\/\/www.thecodecampus.de\/blog\/git-change-files-previous-commit\/","name":"Undo and edit your last commits with git - Web Development Blog","isPartOf":{"@id":"https:\/\/www.thecodecampus.de\/blog\/#website"},"datePublished":"2016-08-23T14:45:22+00:00","dateModified":"2024-06-12T13:27:04+00:00","description":"Sometimes one might get into the unfortunate need to change a git commit. I will show some options to revert or edit commits in local working tree","breadcrumb":{"@id":"https:\/\/www.thecodecampus.de\/blog\/git-change-files-previous-commit\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.thecodecampus.de\/blog\/git-change-files-previous-commit\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.thecodecampus.de\/blog\/git-change-files-previous-commit\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.thecodecampus.de\/blog\/"},{"@type":"ListItem","position":2,"name":"Undo and edit your last commits with git"}]},{"@type":"WebSite","@id":"https:\/\/www.thecodecampus.de\/blog\/#website","url":"https:\/\/www.thecodecampus.de\/blog\/","name":"Web Development tips and tricks - theCodeCampus Blog","description":"Tips, tricks, and experiences about developing web and mobile applications with Angular, TypeScript, and Testing.","publisher":{"@id":"https:\/\/www.thecodecampus.de\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.thecodecampus.de\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.thecodecampus.de\/blog\/#organization","name":"theCodeCampus","url":"https:\/\/www.thecodecampus.de\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.thecodecampus.de\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.thecodecampus.de\/blog\/wp-content\/uploads\/2024\/01\/TCC-Logo-Bildmarke-quadratisch.jpg","contentUrl":"https:\/\/www.thecodecampus.de\/blog\/wp-content\/uploads\/2024\/01\/TCC-Logo-Bildmarke-quadratisch.jpg","width":156,"height":156,"caption":"theCodeCampus"},"image":{"@id":"https:\/\/www.thecodecampus.de\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.thecodecampus.de\/blog\/#\/schema\/person\/276bbda2f8da73154f22fb652201cfbc","name":"theCodeCampus","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.thecodecampus.de\/blog\/wp-content\/uploads\/2024\/01\/TCC-Logo-Bildmarke-quadratisch-96x96.jpg","url":"https:\/\/www.thecodecampus.de\/blog\/wp-content\/uploads\/2024\/01\/TCC-Logo-Bildmarke-quadratisch-96x96.jpg","contentUrl":"https:\/\/www.thecodecampus.de\/blog\/wp-content\/uploads\/2024\/01\/TCC-Logo-Bildmarke-quadratisch-96x96.jpg","caption":"theCodeCampus"},"description":"Our knowledge is not simply gained through reading - it is trained, tested and constantly being expanded. Because first and foremost, we are all developers at W11K. The know-how that we acquire here as developers, consultants and information architects flows immediately into our training courses and articles for theCodeCampus.","sameAs":["https:\/\/www.linkedin.com\/showcase\/thecodecampus\/"],"url":"https:\/\/www.thecodecampus.de\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/posts\/988","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/users\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/comments?post=988"}],"version-history":[{"count":5,"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/posts\/988\/revisions"}],"predecessor-version":[{"id":993,"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/posts\/988\/revisions\/993"}],"wp:attachment":[{"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/media?parent=988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/categories?post=988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thecodecampus.de\/blog\/wp-json\/wp\/v2\/tags?post=988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}