Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Git

Git: Recover a Commit You Accidentally Amended Over the Wrong Change

- 26.09.26 - ErcanOPAK

🔀 The Amend That Overwrote a Commit You Actually Wanted to Keep

`git commit –amend` is meant to fix the most recent commit – a typo in the message, a forgotten file – but running it at the wrong moment (after switching what’s staged, or simply out of habit) replaces that commit’s content entirely, and the version that existed a moment earlier isn’t shown anywhere in `git log` once it’s gone.

🔎 The Problem

$ git log --oneline -1
a1b2c3d Add invoice PDF export

$ git add forgotten_file.cs
$ git commit --amend --no-edit

# Intended to just add one forgotten file to the last commit - but the
# working directory also had unrelated, half-finished changes staged
# from a different task, and they got folded into the same commit too.
# The original "Add invoice PDF export" commit, exactly as it was
# before the amend, no longer exists under that commit hash - a1b2c3d
# now points at different content entirely.

✅ Fix: The Old Commit Is Still There in the Reflog

  • `git reflog` lists every position HEAD has pointed to, including the commit as it existed immediately before the amend – look for the entry just above the `commit (amend):` line, which is the original commit’s hash before it was replaced.
  • `git show <old-sha>` confirms it’s the right one, then `git cherry-pick <old-sha>` (or `git branch recovered-commit <old-sha>` to just point a branch at it) brings that original content back without disturbing the amended commit that’s already there now.
  • If the amend hasn’t been pushed anywhere yet, this recovery window is effectively unlimited for practical purposes – the reflog keeps entries for 90 days by default, far longer than most people take to notice an amend went wrong.

⚠️ Why This Is Worth Double-Checking Before Every Amend

  • `git status` right before running `–amend` shows exactly what’s staged and about to be folded into the previous commit – a five-second check that catches this class of mistake before it happens, rather than after.
  • Once an amended commit has been pushed and someone else has pulled it, recovering the original locally doesn’t undo it for them – it becomes a rewritten-history conversation, which is a good reason to treat `–amend` as local-only until a commit is shared.

Amending a commit doesn’t edit history quietly in place – it replaces one commit with another, and the reflog is what remembers the one that got replaced.

— Version Control Tip

Related posts:

Git: Fix a Signed Commit Showing as Unverified After a Rebase Even Though Nothing in the Diff Actual...

Git: Fix a Signed Commit Showing as Unverified After a Rebase Even Though Nothing in the Diff Actual...

Git: Use Git Stash to Save Work Temporarily

Git: Use Git Stash to Save Work Temporarily

Git Reverts Remove the Wrong Code

Git Reverts Remove the Wrong Code

Views: 14

Post navigation

Photoshop: Fix a Smart Object That Won’t Update After You Edit Its Linked Source File
Windows 11: Fix a Windows Update That Restarts Itself Over and Over Without Ever Finishing

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,068)
  • Get the User Name and Domain Name from an Email Address in SQL (990)
  • How to add default value for Entity Framework migrations for DateTime and Bool (986)
  • Get the First and Last Word from a String or Sentence in SQL (865)
  • How to select distinct rows in a datatable in C# (859)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (661)
  • Add Constraint to SQL Table to ensure email contains @ (619)
  • Average of all values in a column that are not zero in SQL (577)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (553)
  • Find numbers with more than two decimal places in SQL (481)

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • Ağ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store
  • WordPress: Fix a Navigation Block Losing Submenu Items After Sync
  • Visual Studio: Fix CodeLens Reference Counts Stuck at Stale Numbers
  • C#: Fix a UTF-8 String Literal Behaving Differently in a Switch
  • Photoshop: Fix a Liquify Mesh Snapping Back on a Smart Object
  • The AI Prompt That Turns a Crash Dump Into a Short Repro Script

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,068)
  • Get the User Name and Domain Name from an Email Address in SQL (990)
  • How to add default value for Entity Framework migrations for DateTime and Bool (986)
  • Get the First and Last Word from a String or Sentence in SQL (865)
  • How to select distinct rows in a datatable in C# (859)

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • Ağ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com