{"id":2737,"date":"2026-04-10T02:12:42","date_gmt":"2026-04-09T20:42:42","guid":{"rendered":"https:\/\/mukundasoftware.com\/blog\/?p=2737"},"modified":"2026-04-10T03:13:38","modified_gmt":"2026-04-09T21:43:38","slug":"git-undo-last-commit-locally","status":"publish","type":"post","link":"https:\/\/mukundasoftware.com\/blog\/software\/how-to\/git-undo-last-commit-locally.html","title":{"rendered":"How to Undo the Last Git Commit Locally (Without Losing Your Work)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">We&#8217;ve all been there. You type <code>git commit -m \"Fix typo\"<\/code> and then immediately realize you committed the wrong file, forgot to add something, or the commit message is absolute garbage. Panic sets in. Is your work gone forever?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Relax.<\/strong> Git is like a time machine. Almost every mistake is reversible \u2014 you just need the right command.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This guide will show you exactly how to undo your last local commit safely, whether you want to keep your changes or scrap them completely.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<div style=\"height:75px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u26a1 <strong>The 30-Second Fix<\/strong> (Just Tell Me the Command)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you just want to fix the problem right now, here are the two commands you need.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2705 SAFE Option: Undo the commit but keep all your file changes<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git reset --soft HEAD~1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Note for PowerShell users: If you get an error, wrap the commit reference in quotes like this: <code>git reset --soft \"HEAD~1\"<\/code>.<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u274c DANGER Option: Undo the commit AND delete all the changes you just made<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git reset --hard HEAD~1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it. In 90% of cases, the first command is exactly what you need. You can now re-commit with a better message or add the missing files.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<div style=\"height:75px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 <strong>What Actually Happened?<\/strong> (<em>Understanding the Concept<\/em>)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Think of Git commits as a line of checkpoints in a video game. Every time you commit, you save your progress.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>HEAD<\/code> is a pointer that tells you where you currently are in the timeline.<\/li>\n\n\n\n<li><code>HEAD~1<\/code> means &#8220;one step behind where I am now&#8221; (the last commit).<\/li>\n\n\n\n<li><code>git reset<\/code> moves the <code>HEAD<\/code> pointer backward.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">When you move the pointer back, the commit disappears from the timeline. But what happens to the changes you made? That depends on the flag you use (<code>--soft<\/code>, <code>--mixed<\/code>, <code>--hard<\/code>).<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<div style=\"height:75px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udfaf Detailed Scenarios: Which Command Should You Use?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Don&#8217;t just copy-paste blindly. Here\u2019s exactly when to use which option based on your real-life situation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. &#8220;I Want to Undo the Commit, But Keep My Files Ready to Commit Again&#8221;<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Scenario:<\/strong> You wrote a bad message like &#8220;asdf&#8221; or you just want to add one more small change to the last batch.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Command:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git reset --soft HEAD~1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Result:<\/strong> The commit is gone. Your changes are still <strong>staged<\/strong> (green in <code>git status<\/code>). You can immediately type <code>git commit -m \"New improved message\"<\/code> to fix it.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. &#8220;I Want to Undo the Commit and Unstage the Files to Keep Working&#8221;<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Scenario:<\/strong> You committed a bunch of files but you want to break them into two separate commits.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Command:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git reset HEAD~1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><em>(Note: <code>--mixed<\/code> is the default behavior, so you can leave it out)<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Result:<\/strong> The commit is gone. Your changes are kept, but they are <strong>unstaged<\/strong> (red in <code>git status<\/code>). You can now selectively add files with <code>git add<\/code> and create multiple new commits.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. &#8220;I Messed Up Badly. I Want to Delete the Commit AND the Changes Permanently&#8221;<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Scenario:<\/strong> You accidentally committed a giant log file or a password. You want it gone from your computer history forever.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Command:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git reset --hard HEAD~1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u26a0\ufe0f Warning:<\/strong> This is destructive. The changes are <strong>gone forever<\/strong> from your local working directory. Only use this if you are 100% sure you don&#8217;t need the code.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. &#8220;I Want to Undo the Last 3 Commits&#8221;<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Scenario:<\/strong> You&#8217;ve been on a coding spree and realized the last few commits are a mess. You want to start over from 3 commits ago but keep all the code changes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Command:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git reset --soft HEAD~3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Result:<\/strong> The last 3 commits are squashed into uncommitted changes. You can now make one single, beautiful commit.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<div style=\"height:75px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udea8 <strong>The Critical Distinction<\/strong>: <em>Before Push vs. After Push<\/em><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This is the most important rule in Git collaboration.<\/p>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\">\u2705 <strong>I HAVE NOT PUSHED (Local Only)<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Action:<\/strong> Use <code>git reset<\/code>.<\/li>\n\n\n\n<li><strong>Why:<\/strong> You are rewriting history that only exists on your computer. It&#8217;s safe and private.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\">\u274c <strong>I HAVE ALREADY PUSHED (Remote\/GitHub)<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Action:<\/strong> <strong>DO NOT USE <code>git reset<\/code>.<\/strong> Use <code>git revert<\/code>.<\/li>\n\n\n\n<li><strong>Why:<\/strong> If you reset a commit that your teammates have already pulled, you will cause a &#8220;merge conflict nightmare&#8221; and possibly delete their work.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Command for pushed commits:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git revert HEAD<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This creates a <em>new<\/em> commit that undoes the changes of the last commit. It&#8217;s safe for everyone because you are adding to history, not deleting it.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<div style=\"height:75px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u2694\ufe0f <strong>Git Reset vs. Git Revert<\/strong>: The Cheat Sheet<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-center\" data-align=\"center\">Command<\/th><th class=\"has-text-align-center\" data-align=\"center\">What it does<\/th><th class=\"has-text-align-center\" data-align=\"center\">When to use<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\"><strong>git reset<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\">Erases history (like going back in time).<\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>Local commits only.<\/strong> You haven&#8217;t shared them yet.<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"><strong>git revert<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\">Creates a &#8220;negative&#8221; commit to undo changes.<\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>Shared\/Pushed commits.<\/strong> Everyone else can see the fix.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Golden Rule:<\/strong> <em>If it&#8217;s on GitHub\/GitLab\/Bitbucket and someone else might have seen it, you MUST revert, not reset.<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<div style=\"height:75px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\ude2d <strong>Common Panic Situations<\/strong> (<em>And Their Fixes<\/em>)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">&#8220;Help! I accidentally committed a huge video file.&#8221;<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udc49 <strong>Solution:<\/strong> <code>git reset --soft HEAD~1<\/code> to uncommit. Then <strong>add that file to <code>.gitignore<\/code><\/strong> before committing again.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">&#8220;Oh no. I committed my API secret key!&#8221;<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udc49 <strong>Solution:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>First, change the key<\/strong> in your service dashboard immediately (it is now compromised).<\/li>\n\n\n\n<li>If you haven&#8217;t pushed, use <code>git reset --hard HEAD~1<\/code> to scrub the commit.<\/li>\n\n\n\n<li><strong>Crucial:<\/strong> Double-check your actual folder to ensure the secret file (e.g., <code>.env<\/code>) isn&#8217;t still sitting there uncommitted on your hard drive. Delete it or remove the line from your file system before you accidentally commit it again.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">&#8220;The commit is fine, but the message is &#8216;WIP&#8217; and I forgot to change it.&#8221;<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udc49 <strong>Solution:<\/strong> You don&#8217;t even need to reset! Just use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git commit --amend -m \"New, professional commit message\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><em>(This only works for the <strong>very last<\/strong> commit).<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<div style=\"height:75px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udca1 Pro Tips for Staying Out of Git Trouble<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Always start with <code>--soft<\/code>.<\/strong> It&#8217;s your safety net. You can always get more aggressive later.<\/li>\n\n\n\n<li><strong>Check your status.<\/strong> Run <code>git status<\/code> frequently to see where you are.<\/li>\n\n\n\n<li><strong>Backup is key.<\/strong> If you are about to try a complex <code>reset<\/code>, just copy your project folder to your desktop first. Peace of mind guaranteed.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<div style=\"height:75px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u2753 Frequently Asked Questions (FAQ)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q: Can I recover files after <code>git reset --hard<\/code>?<\/strong><br><em>A: Yes, surprisingly easily! Git keeps a secret diary of every move you make called the <strong>Reflog<\/strong>. If you <code>reset --hard<\/code> by mistake, don&#8217;t close the terminal. Type <code>git reflog<\/code> to see a list of recent actions. Find the hash of your lost commit (the one before the reset) and type <code>git checkout &lt;hash&gt;<\/code> to bring it back from the dead.<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q: What does <code>HEAD~1<\/code> actually mean?<\/strong><br><em>A: <code>HEAD<\/code> is your current snapshot. <code>~1<\/code> means &#8220;one ancestor back&#8221; (the parent). <code>HEAD~2<\/code> would be the grandparent.<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<div style=\"height:75px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcda Further Reading &amp; Related Troubleshooting<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now that you&#8217;ve mastered undoing commits, you might be setting up a fresh development environment or comparing code. Check out these related guides for more productivity hacks:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Developer Setup:<\/strong> <a href=\"https:\/\/mukundasoftware.com\/blog\/software\/how-to\/install-nodejs-beginners-guide-windows-mac-linux.html\">How to Install Node.js: Beginner\u2019s Guide for Windows, Mac, Linux<\/a><\/li>\n\n\n\n<li><strong>Code Review Tools:<\/strong> <a href=\"https:\/\/mukundasoftware.com\/blog\/software\/how-to\/how-to-compare-files-in-notepad-top-alternatives.html\">How to Compare Files in Notepad++: Top Alternatives &amp; Guide<\/a><\/li>\n\n\n\n<li><strong>Choosing Your OS:<\/strong> <a href=\"https:\/\/mukundasoftware.com\/blog\/software\/operating-system\/comparing-the-best-os-for-programming-developers-and-coding.html\">Comparing the Best OS for Programming, Developers, and Coding<\/a><\/li>\n\n\n\n<li><strong>Fix Outlook Issues:<\/strong> <a href=\"https:\/\/mukundasoftware.com\/blog\/help\/outlook\/fix-msoutlook-runs-slowly-or-crashes-oversized-pst-file-performance.html\">Fix MS Outlook Runs Slowly or Crashes: Oversized PST File Performance<\/a> <em>(Because even devs deal with email)<\/em><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 Final Thought<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Git can feel intimidating because it speaks its own language (<code>HEAD<\/code>, <code>rebase<\/code>, <code>detached<\/code>), but the truth is simpler than it looks:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udc49 <strong>&#8220;No commit is truly final until you share it with the world. Until then, you&#8217;re the master of your timeline.&#8221;<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;ve all been there. You type git commit -m &#8220;Fix typo&#8221; and then immediately realize you committed the wrong file, forgot to add something, or the commit message is absolute garbage. Panic sets in. Is your work gone forever? Relax. Git is like a time machine. Almost every mistake is reversible \u2014 you just need &#8230; <a title=\"How to Undo the Last Git Commit Locally (Without Losing Your Work)\" class=\"read-more\" href=\"https:\/\/mukundasoftware.com\/blog\/software\/how-to\/git-undo-last-commit-locally.html\" aria-label=\"Read more about How to Undo the Last Git Commit Locally (Without Losing Your Work)\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":2738,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[236],"tags":[],"class_list":["post-2737","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to"],"_links":{"self":[{"href":"https:\/\/mukundasoftware.com\/blog\/wp-json\/wp\/v2\/posts\/2737","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mukundasoftware.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mukundasoftware.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mukundasoftware.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/mukundasoftware.com\/blog\/wp-json\/wp\/v2\/comments?post=2737"}],"version-history":[{"count":6,"href":"https:\/\/mukundasoftware.com\/blog\/wp-json\/wp\/v2\/posts\/2737\/revisions"}],"predecessor-version":[{"id":2748,"href":"https:\/\/mukundasoftware.com\/blog\/wp-json\/wp\/v2\/posts\/2737\/revisions\/2748"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mukundasoftware.com\/blog\/wp-json\/wp\/v2\/media\/2738"}],"wp:attachment":[{"href":"https:\/\/mukundasoftware.com\/blog\/wp-json\/wp\/v2\/media?parent=2737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mukundasoftware.com\/blog\/wp-json\/wp\/v2\/categories?post=2737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mukundasoftware.com\/blog\/wp-json\/wp\/v2\/tags?post=2737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}