Marcio Cunha

How to recover corrupted files in Git directly from terminal

Learn how to rescue lost commits, revert unwanted changes, and restore accidentally deleted files in Git using native terminal commands.

Marcio Cunha•4 min
Also available in:PortuguêsEspañol
Summary
  • The checkout command restores local files modified by mistake back to the last saved version.
  • The reference log reflog tracks every local repository action even after drastic deletions.
  • The reset tool allows adjusting the history pointer to rescue project source code.
  • The cherry-pick operation rescues isolated commits from other branches without mixing the rest.
  • The cleanup routine demands caution to prevent permanent data loss of untracked system items.

What happens when Git corrupts files or code is lost?

Working with version control is like having a time machine for your code. However, during rush moments, merge conflicts, or disk failures, we may face incorrectly modified files, accidentally deleted assets, or entire commits vanishing from sight. In practice, this means the tracking ecosystem maintains internal safety nets that we often ignore. Just as technicians use file system recovery tools to rescue lost disk data, Git has its own native mechanisms operated directly through the terminal to get us out of trouble without relying on external backups.

Many developers panic when they realize they have overwritten hours of work or executed a destructive command. The good news is that Git rarely deletes data immediately. It stores history and references inside a hidden folder named .git at the root of your project. Understanding the difference between the working tree, the staging index, and the commit history is the first step to mastering the art of undoing mistakes without damaging software structure.

How to restore locally modified or deleted files by mistake

When you change a file on your machine and realize the modification ruined everything, the quick restore command becomes your best friend. To discard local modifications in a specific file and bring it back to the exact state of the last save, we use the restore command or the classic checkout. In practice, Git replaces the current file with the one recorded in the last commit made on your active branch.

To execute this restoration safely in the terminal, open the command line in your repository folder and type the following instruction:

git restore file_name.txt

If you completely deleted the file using your operating system's rm command instead of Git's own deletion command, the same instruction serves to bring it back to life. The version control engine checks the object database, locates the most recent copy, and recreates it in your working directory instantly.

How to rescue lost commits using the reference history

The most terrifying scenario happens when we execute a drastic reset or delete an entire development branch, causing recent commits to disappear from our normal view. This is where Git's most powerful secret comes into play: the reflog. The reflog is a chronological record of every time the reference pointer changed position in your local repository, recording even the steps that seemed lost forever.

To consult this secret logbook and find the commit identification number before the disaster, run the command below in the terminal:

git reflog

The terminal will display a list of numbered lines containing alphanumeric codes and descriptions of recent actions. By identifying the exact moment when the code was sound, you can create a new temporary branch or restore the state pointing to that specific code. This is the ultimate rescue tool when we need to reverse complex operations, working similarly to a rigorous command line text processing routine to isolate what truly matters.

How to rescue isolated code with the cherry-pick technique

Sometimes, the issue is not losing an entire file, but needing to bring a specific block of changes created in another branch or project into your current environment without mixing all outside history. For this, the cherry-pick command acts as a surgical selector. It grabs a specific commit from anywhere in the repository's family tree and applies it directly where you are working.

The syntax to apply this surgical operation in the terminal is quite straightforward, requiring only that you provide the unique identification of the desired commit:

git cherry-pick a1b2c3d4

This approach prevents you from having to manually redo complex alterations or resort to manual file copies via clipboard. Git takes care of applying the modifications and checking if there are specific conflicts requiring human validation before consolidating the process. To explore these architectural concepts further in production, see our related technical analysis on How to test JFET transistors on the bench with a multimeter.

Final considerations on data rescue in Git

Mastering the Git terminal turns crisis moments into minor routing incidents solvable with precise commands. Understanding that the system is designed to protect developers from their own mistakes removes the fear of experimenting and pushing forward in complex projects. Always check repository status frequently, use the reflog as your ultimate safety net, and practice these commands in controlled environments to gain confidence in your daily development routine.