{"id":4294,"date":"2026-04-22T18:54:42","date_gmt":"2026-04-22T16:54:42","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=4294"},"modified":"2026-04-22T19:16:17","modified_gmt":"2026-04-22T17:16:17","slug":"git-how-to-fix-your-last-commit-without-panic","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2026\/04\/22\/git-how-to-fix-your-last-commit-without-panic\/","title":{"rendered":"Git: How to Fix Your Last Commit Without Panic"},"content":{"rendered":"\n<p>We&#8217;ve all been there. You just made a commit, hit Enter, and then\u2026 <em>&#8220;Wait, I forgot a file!&#8221;<\/em> or <em>&#8220;That message is completely wrong!&#8221;<\/em><\/p>\n\n\n\n<p>Don&#8217;t worry. Git has you covered. In this article, we&#8217;ll explore two powerful (but sometimes scary) tools:<\/p>\n\n\n\n<ul><li><code>git commit --amend --no-edit<\/code>&nbsp;?&nbsp;<strong>Fix your last commit without changing its message<\/strong><\/li><li><code>git push --force-with-lease<\/code>&nbsp;?&nbsp;<strong>Safely push a rewritten commit to the server<\/strong><\/li><\/ul>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3><code>git commit --amend --no-edit<\/code><\/h3>\n\n\n\n<p>This command lets you <strong>modify your last commit<\/strong> by adding forgotten files or fixing small mistakes \u2014 <strong>without changing the commit message<\/strong>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><em>Think of it like editing the last page of your notebook before handing it in, without rewriting the title.<\/em><\/p><\/blockquote>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\">Real-life Example<\/span><\/h5>\n\n\n\n<p>Imagine you&#8217;re working on a website. You commit your changes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git add homepage.html\ngit commit -m \"Add homepage layout\"<\/code><\/pre>\n\n\n\n<p>Then you realize: <em>&#8220;Oh no, I forgot to include the CSS file!&#8221;<\/em><\/p>\n\n\n\n<p>Without <code>--amend<\/code>, you&#8217;d have to create a new commit just for one forgotten file. Messy! Instead:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Step 1: Stage the forgotten file\ngit add style.css\n\n# Step 2: Add it to the last commit, keeping the same message\ngit commit --amend --no-edit<\/code><\/pre>\n\n\n\n<p><strong><span class=\"has-inline-color has-vivid-green-cyan-color\">Result:<\/span><\/strong> Your last commit now includes <strong>both<\/strong><code>homepage.html<\/code><strong>and<\/strong><code>style.css<\/code>, still named <em>&#8220;Add homepage layout&#8221;<\/em>. Clean and tidy!<\/p>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\">&nbsp;Another Example: Fixing a Typo in a File<\/span><\/h5>\n\n\n\n<p>You committed a file with a small typo in the code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git commit -m \"Fix navigation bar bug\"\n# Oops! You left a typo in navbar.js<\/code><\/pre>\n\n\n\n<p>Fix the typo, then:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git add navbar.js\ngit commit --amend --no-edit<\/code><\/pre>\n\n\n\n<p><img loading=\"lazy\" width=\"35\" height=\"32\" class=\"wp-image-4064\" style=\"width: 35px;\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2025\/10\/image-2.png\" alt=\"\">The commit is updated with the fix. No extra &#8220;oops&#8221; commit in your history. Your history stays <strong>clean and professional<\/strong>.<\/p>\n\n\n\n<h4><img loading=\"lazy\" width=\"33\" height=\"36\" class=\"wp-image-4297\" style=\"width: 33px;\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2026\/04\/image.png\" alt=\"\"> Important Warning<\/h4>\n\n\n\n<p>When you use <code>--amend<\/code>, Git <strong>rewrites<\/strong> the commit. It&#8217;s not editing it in place \u2014 it creates a <strong>brand new commit<\/strong> with a new internal ID, and discards the old one.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Before amend:  ... -&gt; &#91;abc1234] \"Add homepage layout\"\nAfter amend:   ... -&gt; &#91;xyz9999] \"Add homepage layout\"  &lt;- new ID!<\/code><\/pre>\n\n\n\n<p>This is totally fine <strong>if you haven&#8217;t shared the commit yet<\/strong> (i.e., not pushed to a remote server).<br>But if you <strong>already pushed it<\/strong>, you&#8217;ll need a special push command. More on that below! <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4><img loading=\"lazy\" width=\"22\" height=\"20\" class=\"wp-image-4301\" style=\"width: 22px;\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2026\/04\/compare-icon.png\" alt=\"\"> Amend Variants \u2014 Quick Comparison<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>Message<\/th><th>What changes<\/th><\/tr><\/thead><tbody><tr><td><code>git commit --amend -m \"New message\"<\/code><\/td><td> Changed<\/td><td>Message + staged files added<\/td><\/tr><tr><td><code>git commit --amend --no-edit<\/code><\/td><td> Unchanged<\/td><td>Only staged files added<\/td><\/tr><tr><td><code>git commit --amend<\/code><\/td><td> Editor opens<\/td><td>You decide<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3><code>git push --force-with-lease<\/code><\/h3>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\">The Problem: Why Can&#8217;t I Just Push Normally?<\/span><\/h5>\n\n\n\n<p>After using <code>--amend<\/code>, if you try a regular <code>git push<\/code>, Git will refuse:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git push\n# <img loading=\"lazy\" width=\"28\" height=\"26\" class=\"wp-image-4300\" style=\"width: 28px;\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2026\/04\/cancel.png\" alt=\"\"> Error: Updates were rejected because the tip of your current branch is behind its remote counterpart.<\/code><\/pre>\n\n\n\n<p>Why? Because Git sees that your local history <strong>diverges<\/strong> from what&#8217;s on the server. The server has the old commit (<code>abc1234<\/code>), but you now have a new one (<code>xyz9999<\/code>).<\/p>\n\n\n\n<p>You need to tell Git: <em>&#8220;I know what I&#8217;m doing, please overwrite the remote with my version.&#8221;<\/em><br>That&#8217;s what <strong>force push<\/strong> is for.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h5>The 3 Force Push Options Explained<\/h5>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\"> Option 1:&nbsp;<code>git push --force<\/code>&nbsp;(or&nbsp;<code>-f<\/code>)<\/span><\/h5>\n\n\n\n<p>The nuclear option. It <strong>overwrites everything<\/strong> on the server, no questions asked.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git push --force<\/code><\/pre>\n\n\n\n<p><strong>The danger:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Server:     A -&gt; B -&gt; C   &lt;- your colleague just pushed C\nYou local:  A -&gt; B -&gt; D   &lt;- your amended commit\n\ngit push --force -&gt; C is GONE forever. <em>Your colleague loses their work.<\/em><\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><em> <strong>Never use <code>--force<\/code> on a shared branch.<\/strong> It can permanently destroy your teammates&#8217; work.<\/em><\/p><\/blockquote>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\">Option 2:&nbsp;<code>git push --force-with-lease<\/code>&nbsp;(recommended)<\/span><\/h5>\n\n\n\n<p>The <strong>safe<\/strong> force push. Before overwriting, it checks:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><em>&#8220;Has anyone else pushed to this branch since I last synced?&#8221;<\/em><\/p><\/blockquote>\n\n\n\n<p>If yes -&gt; <img loading=\"lazy\" width=\"28\" height=\"26\" class=\"wp-image-4300\" style=\"width: 28px;\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2026\/04\/cancel.png\" alt=\"\"> Push is <strong>refused<\/strong>. You&#8217;re protected.<br>If no -&gt; <img loading=\"lazy\" width=\"37\" height=\"31\" class=\"wp-image-4066\" style=\"width: 37px;\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2025\/10\/image-4.png\" alt=\"\"> Push goes through safely.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git push --force-with-lease<\/code><\/pre>\n\n\n\n<p><strong>Visual example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Scenario A \u2014 Safe:\nServer:    A -&gt; B        &lt;- nobody pushed after you\nYou local: A -&gt; B -&gt; D   &lt;- your amended commit\nResult: <img loading=\"lazy\" width=\"35\" height=\"32\" class=\"wp-image-4064\" style=\"width: 35px;\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2025\/10\/image-2.png\" alt=\"\"> Push accepted!\n\nScenario B \u2014 Protected:\nServer:    A -&gt; B -&gt; C   &lt;- colleague pushed C\nYou local: A -&gt; B -&gt; D   &lt;- your amended commit\nResult: <img loading=\"lazy\" width=\"28\" height=\"26\" class=\"wp-image-4300\" style=\"width: 28px;\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2026\/04\/cancel.png\" alt=\"\"> Push refused! Go check what C is first.\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\">Option 3:&nbsp;<code>git push --force-with-lease --force-if-includes<\/code>&nbsp;(maximum safety)<\/span><\/h5>\n\n\n\n<p>This is the <strong>most secure<\/strong> option. It adds an extra check on top of <code>--force-with-lease<\/code>:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><em>&#8220;Not only must the remote not have changed, but I must have actually integrated those remote commits into my local history.&#8221;<\/em><\/p><\/blockquote>\n\n\n\n<p>It closes a subtle loophole: if you ran <code>git fetch<\/code> recently, <code>--force-with-lease<\/code> alone might still allow an accidental overwrite. <code>--force-if-includes<\/code> prevents that.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git push --force-with-lease --force-if-includes<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Summary Table: Which Force Push to Use?<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>Safety<\/th><th>When to use<\/th><\/tr><\/thead><tbody><tr><td><code>git push --force<\/code><\/td><td><img loading=\"lazy\" width=\"28\" height=\"26\" class=\"wp-image-4300\" style=\"width: 28px;\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2026\/04\/cancel.png\" alt=\"\">Dangerous<\/td><td>Almost never<\/td><\/tr><tr><td><code>git push --force-with-lease<\/code><\/td><td><img loading=\"lazy\" width=\"37\" height=\"31\" class=\"wp-image-4066\" style=\"width: 37px;\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2025\/10\/image-4.png\" alt=\"\"> Safe<\/td><td>After amend\/rebase on your own branch<\/td><\/tr><tr><td><code>git push --force-with-lease --force-if-includes<\/code><\/td><td><img loading=\"lazy\" width=\"37\" height=\"31\" class=\"wp-image-4066\" style=\"width: 37px;\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2025\/10\/image-4.png\" alt=\"\"><img loading=\"lazy\" width=\"37\" height=\"31\" class=\"wp-image-4066\" style=\"width: 37px;\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2025\/10\/image-4.png\" alt=\"\"> Safest<\/td><td>When you want maximum protection<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Key Takeaways<\/h4>\n\n\n\n<ul><li><strong><code>git commit --amend --no-edit<\/code><\/strong>&nbsp;? Add forgotten files to your last commit, keeping the same message<\/li><li><strong><code>--amend<\/code>&nbsp;rewrites history<\/strong>&nbsp;? The commit gets a new ID<\/li><li><strong>Never use&nbsp;<code>git push --force<\/code><\/strong>&nbsp;on shared branches ? You risk destroying teammates&#8217; work<\/li><li><strong><code>git push --force-with-lease<\/code><\/strong>&nbsp;? The safe way to push a rewritten commit<\/li><li><strong>Add&nbsp;<code>--force-if-includes<\/code><\/strong>&nbsp;for maximum safety<\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;ve all been there. You just made a commit, hit Enter, and then\u2026 &#8220;Wait, I forgot a file!&#8221; or &#8220;That message is completely wrong!&#8221; Don&#8217;t worry. Git has you covered. In this article, we&#8217;ll explore two powerful (but sometimes scary) tools: git commit &#8211;amend &#8211;no-edit&nbsp;?&nbsp;Fix your last commit without changing its message git push &#8211;force-with-lease&nbsp;?&nbsp;Safely [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":807,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[60],"tags":[61,1139,1051],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/4294"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=4294"}],"version-history":[{"count":6,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/4294\/revisions"}],"predecessor-version":[{"id":4307,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/4294\/revisions\/4307"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/807"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=4294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=4294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=4294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}