Sitemap

How to Recover Lost Files from Deleted Git Branches: A Step-by-Step Guide

1 min readDec 10, 2024

Have you ever faced a situation where some critical changes you made to files seemed lost, and redoing the work wasn’t an option? It happened to me recently, and I want to share how I recovered my lost files after a branch deletion.

Here’s the backstory: I had made changes to a few files, committed them, and pushed everything to the origin. Later, I needed those files but couldn’t find them in any commit or branch. Worse, the feature branch where I initially committed the changes had been deleted both locally and on the origin. I was left thinking my work was lost forever.

Determined to recover my files, I discovered a powerful yet lesser-known command: git fsck. Here’s how it saved the day:

1. Run the following command to check for dangling objects in your repository:

git fsck --lost-found

Output:

Checking object directories: 100% (256/256), done.
Checking objects: 100% (233/233), done.
dangling blob ***********************123
dangling blob ***********************456

This command lists all the dangling blobs in your repository — these are orphaned objects not linked to any branch or commit but still present in your Git database.

2. Examine each dangling blob to find your lost file:

git show ***********************456

3. Create a New Branch from the Dangling Commit:

Once you’ve identified the commit, create a new branch pointing to it:

git checkout -b recovered-branch ***********************123

--

--