Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

I messed up badly and need ugrent help recovering my work.

Background:

Working on a feature branch for the past 2 days. Made good progress - around 500 lines of code across multiple commits. Didn't push yet because I wanted to clean up the commit history first.

What Happened:

  1. Was on feature/user-auth branch with 3-4 commits
  2. Needed to sync with main, so did git checkout main && git pull
  3. Went back and started rebasing: git rebase main
  4. Hit conflicts, resolved them, git rebase --continue
  5. Hit MORE conflicts (unexpected), got confused
  6. Think I did git rebase --abort but not sure if it worked
  7. Tried to reset to before the rebase: git reset --hard HEAD~3
  8. Realized that was wrong, now my commits are gone
  9. Looked at git reflog, tried resetting to what I thought was the right commit
  10. Made it worse - now on detached HEAD

Current Situation:

$ git status
HEAD detached at 7f3a891
nothing to commit, working tree clean

My git reflog has like 60+ lines and I honestly can't figure out which commit has my actual work. I've tried git reset --hard to a few different commits but keep ending up in the wrong place.

What I've Tried:
git reflog - too many entries, can't identify the right one
git show on several commits - diffs are huge, hard to verify
git log --all --oneline - doesn't show my lost commits
Googling "recover lost commits git" - everyone says use reflog but I AM and it's not helping
Questions:
How do I systematically find the right commit in reflog?
Is there a way to see what each reflog entry actually contains without manually checking diffs?
Should I be looking for specific reflog messages like "rebase (start)" or "commit:"?
Is my work actually recoverable or could it be garbage collected already?

I've been stuck on this for over an hour. The reflog output is just overwhelming and I'm worried about making it worse. Any advice would be really appreciated.

You must be logged in to vote

Hey @gemadhu! No, it's completely free and open source. Here are the exact steps to recover your code:

Step 1: Install it

If you have Rust/Cargo installed:

cargo install git-time-machine

If you don't have Rust, you can install it first with:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Step 2: Run it from your repo folder

Open your terminal, cd into the repo where you lost the commits, and run:

git-time-machine --all
The --all flag is important in your case since you have 60+ reflog entries. It will load all of them into the visual timeline.

Step 3: Find your lost commit

You'll see a scrollable list of every action Git recorded. Use the arrow keys (or j/k) to scroll thro…

Replies: 3 comments · 4 replies

Comment options

@gemadhavi To recover your work, you need to find the last commit you made before the rebase started. Since your code isn't pushed, git reflog is your only map.

  1. The Strategy
    The "lost" commits are still in your local database. You just need to find the specific commit hash from before the rebase and point your branch back to it.

  2. The Step-by-Step Fix
    Find the pre-rebase hash:
    Run git reflog and look for the line right below the first entry that says rebase (start): checkout main. This is the exact state of your branch before the trouble began.

Verify the code:
Instead of resetting, "peak" into that commit to make sure it has your ~500 lines:
git show <hash_from_reflog> --stat

Restore the branch:
Once you have the right hash (e.g., abc1234), force your branch to point there:
git checkout feature/user-auth
git reset --hard abc1234

  1. Quick Tips for the "Galaxy Brain" badge:
    Is it gone? No. Git keeps "unreachable" commits for 30 days.

Why use --stat? It shows line counts (+500/-0) so you can identify the right commit without reading 500 lines of diff.

Search by code: If you know a unique function name you wrote, find its commit with:
git log -g -S "my_unique_function_name"

If this solution helped you, please mark it as an approved answer to help others

You must be logged in to vote
1 reply
@gemadhu
Comment options

what is this "Quick Tips for the "Galaxy Brain" badge:"?

Comment options

Hey @gemadhu! Reading this gave me terrible flashbacks. Staring at 60 raw lines of text from git reflog right after losing 500 lines of code is a uniquely terrible flavor of developer panic.

While the accepted answer above gives you the perfect underlying Git CLI commands, I actually just built a lightweight, open-source terminal app for this exact scenario because I was so frustrated by how overwhelming git reflog text walls are.

It's called git-time-machine.

Instead of juggling hashes on a sticky note and running git show a dozen times, it gives you a clean visual UI over your reflog. You just scroll up and down with your arrow keys to see the timeline, press / to instantly search the reflog for words you know you wrote, and just hit Enter to restore your code.

If this ever happens again (and hopefully it doesn't!), it might make the recovery process take 5 seconds instead of an hour of stress. Glad you got your code back!

You must be logged in to vote
3 replies
@gemadhu
Comment options

Can you help me in detail please. How do I use git-time-machine? Do I need to pay to use it? I am yet to get my code back.

@dinakars777
Comment options

It's open source.

@dinakars777
Comment options

Hey @gemadhu! No, it's completely free and open source. Here are the exact steps to recover your code:

Step 1: Install it

If you have Rust/Cargo installed:

cargo install git-time-machine

If you don't have Rust, you can install it first with:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Step 2: Run it from your repo folder

Open your terminal, cd into the repo where you lost the commits, and run:

git-time-machine --all
The --all flag is important in your case since you have 60+ reflog entries. It will load all of them into the visual timeline.

Step 3: Find your lost commit

You'll see a scrollable list of every action Git recorded. Use the arrow keys (or j/k) to scroll through. You're looking for entries that say commit: followed by a message you recognize from your feature branch work.

If you remember any word from your commit messages, press / to search. For example if one of your commits was something like "add user authentication", type user and it will instantly filter down to matching entries and highlight them.

Step 4: Preview before restoring

Once you land on a commit that looks right, press Space to see a diff preview. This lets you verify it's actually your ~500 lines of code without accidentally restoring the wrong commit.

Step 5: Restore

When you're confident you found the right one, press Enter. It will ask you to confirm before doing anything. Type y and your branch will be pointed back to that commit with all your code restored.

Important: Before you do step 5, I'd recommend first creating a safety branch from where you are right now, just in case:

git branch backup-before-restore
That way if something goes sideways, you can always get back to where you are now.

Hope this helps you get your code back!

Please drop a star in the repo if it helped you.

Answer selected by gemadhu
Comment options

Hope you recover those commits! Sharing a resource for anyone here studying algorithms and preparing for ML/SWE interviews: ML/AI Interview Prep — covers algorithms, data structures, ML theory, PyTorch, system design, and 500+ interview Q&A with runnable Python code. ⭐ if it helps!

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.