The fastest way to recover a local branch's changes is to find them before Git cleans them up: check git reflog, recover uncommitted work through git stash list or your editor's local history, and restore deleted or reset commits by branch name or hash. The key condition is that Git never deletes a commit the moment you lose sight of it — it becomes unreachable and is eventually pruned, so act promptly and avoid running git gc on the repository until you've recovered what you need.
That framing matters because "getting the local branch's changes back" covers several different accidents that look similar in the moment but need different fixes. You may have deleted a branch whose work was already committed, reset a branch to an older commit and watched newer commits disappear from the log, checked out another branch with uncommitted edits and found them gone, or discarded changes in your working tree. Each leaves traces in a different place. Working through them in order, from cheapest check to most involved recovery, keeps you from making the situation worse.
First, identify which kind of loss you're dealing with
Before touching any command, answer three questions.
Was the work committed? If you made at least one commit on the branch, the commits almost certainly still exist in Git's object database. Deleting a branch, resetting it, or switching away from it only removes references; the commit objects survive until garbage collection prunes them.
Was the work staged or unstaged only? Uncommitted edits are not stored as commits. If they were stashed, they're recoverable from the stash list. If they were never stashed, Git itself has no record — but most editors and IDEs keep their own local history, and that's often the only path back.
What exactly did you run? A git branch -d, a git reset --hard, a git checkout -- ., or a git clean -fd each destroy different things. Knowing the command narrows the search immediately. If you're unsure, shell history (history in bash or zsh, or your terminal's scrollback) is worth a look.
A useful mental model: Git has three storage layers that matter here. Commits live in the object database and are protected by references (branches, tags, HEAD, the reflog). The stash is a separate stack of commit-like snapshots. The working tree and index are not stored anywhere until you commit or stash them. Recovery works from the most durable layer down.
Check the reflog first — it's almost always the answer
The reflog is a log of where HEAD and each branch tip have pointed, recorded every time they move. It's local to your clone, kept by default for 90 days for reachable entries and 30 days for unreachable ones, and it survives branch deletion and resets. This is where you look first for committed work.
Start with the general view:
You'll see lines like a1b2c3d HEAD@{3}: reset: moving to HEAD~2 or e4f5g6h HEAD@{7}: checkout: moving from feature/login to main. Read from the top (most recent) down. The entry just before the destructive action usually points at what you lost.
If you deleted a specific branch, its own reflog may still exist:
This prints the sequence of commits that branch tip pointed to. The top entry is where it was when you deleted it. Even if git reflog show returns nothing for a deleted branch, the main HEAD reflog often captured a checkout onto that branch, which records its tip.
Once you've found the hash, restore it:
That recreates the branch at the recovered commit without touching your working tree. If you'd rather inspect first, git show e4f5g6h or git log --oneline e4f5g6h -10 lets you confirm it's the right work.
Success check: git log --oneline feature/login -5 shows the commits you expected, and git status on that branch is clean. If the recovered tip looks right but the branch was mid-rebase or mid-merge when you lost it, you may also want the entry one or two steps earlier — rebases rewrite commits, and the pre-rebase state is a separate reflog entry labeled rebase (start) or similar.
Recover a branch after a hard reset
A git reset --hard moves the current branch backward and discards working-tree changes in one step. The commits it "removed" are still in the reflog, typically labeled HEAD@{1}: reset: moving to HEAD~3 right at the top.
Two recovery options:
1: Restore the branch to its old tip. Find the hash in git reflog immediately above the reset entry, then run git reset --hard . This is the cleanest fix when nothing else has happened since. 2: Create a new branch at the old tip and cherry-pick or merge. Safer if you've already built new work on top of the reset state: git branch rescue , then merge or rebase as you prefer.
If the reset happened more than a few commands ago, git reflog may show several entries since. Scan for the line matching the reset action rather than assuming HEAD@{1} is correct.
Watch out for: running git reset --hard again as part of recovery "just to be safe." Each hard reset adds another reflog entry and can discard uncommitted work you'd forgotten about. Recover into a new branch first when in doubt.
Get back stashed changes (and find stashes you forgot)
If you stashed work before switching branches or pulling, it's sitting in the stash stack:
Entries look like stash@{0}: WIP on feature/login: 1a2b3c4 Add validation. The newest is stash@{0}. To inspect without applying: git stash show -p stash@{2}. To apply and keep the stash entry: git stash apply stash@{2}. To apply and remove it from the stack: git stash pop stash@{2}.
The stash is easy to lose track of because git stash pop removes the entry after applying. If a pop went wrong — say, it applied cleanly but you then reset — the stash entry may be gone, but the commits it created are still in the reflog of the stash ref. git fsck --unreachable (discussed below) or git reflog show refs/stash can often surface them.
A subtle trap: git stash apply with conflicts leaves the stash entry intact but the working tree in a conflicted state. If you then run git checkout -- . to "clean up," you've thrown away both the conflict resolution and the applied changes. Resolve conflicts or git stash again before any cleanup command.
Recover uncommitted changes Git never saw
If the edits were never committed and never stashed, Git has no copy. Don't run more Git commands on that working tree — every checkout, reset, or clean risks overwriting whatever fragments remain.
Look in these places, roughly in order of likelihood:
Your editor's local history. VS Code keeps a per-file timeline (right-click a file → Open Timeline, or the Timeline panel in the Explorer). JetBrains IDEs (IntelliJ, PyCharm, WebStorm) keep Local History under right-click → Local History → Show History, which can restore files even after they're deleted from disk. Vim and Emacs keep swap files and auto-save files (.filename.swp, #filename#) that may hold recent edits. OS-level recovery. macOS Time Machine, Windows File History or Previous Versions, and Linux filesystem snapshots (Btrfs, ZFS, LVM) may have recent copies. On macOS, some editors also autosave into ~/Library/Application Support/. Backup copies of the project. Cloud-synced folders (Dropbox, OneDrive, Google Drive) keep version history for files stored inside them, though this is fragile for repositories and not a substitute for commits. Open buffers in a running editor. If the editor is still open, the in-memory buffer may hold edits that were never written to disk. Save to a new path before doing anything else.
This is the one case where Git can't help, and it's worth saying plainly: the habit that prevents it is committing early and often, even to a throwaway branch, and stashing before any operation that touches the working tree.
Restore deleted files and discarded working-tree changes
git checkout -- and git restore discard unstaged edits to tracked files. If the file was staged at some point, the index still holds the staged version, which you can recover:
More usefully, git fsck --lost-found writes dangling blobs (file contents Git stored but no longer references) into .git/lost-found/other/. Each file is named by its hash. You can inspect them with git cat-file -p or just read the files. This is laborious but has saved many people's work after an accidental git checkout -- ..
If the file was committed at some earlier point and then deleted, it's still in history:
That second command restores the file as it existed just before the commit that deleted it.
When reflog and stash come up empty: reachability and pruning
If recovery attempts fail, the commits may have become unreachable and then pruned. Two things drive this:
Reflog expiry. Default is 90 days for entries reachable from a ref and 30 days for unreachable ones (gc.reflogExpire and gc.reflogExpireUnreachable). After expiry, the reflog entries vanish, but the objects may survive until garbage collection. Garbage collection. git gc — run automatically by some commands and by Git itself after enough loose objects accumulate — prunes unreachable objects older than the grace period, default two weeks (gc.pruneExpire).
So the practical rule: stop running git gc and avoid commands that trigger it automatically until you've recovered. git fsck --unreachable lists objects that no ref points to but that still exist:
Each unreachable commit shows up as unreachable commit . You can inspect them with git show and, if one is the lost work, create a branch: git branch recovered . This is the last Git-level resort, and it works only within the pruning window. On a repository where git gc has run recently with default settings, expect the window to be short.
A related check for mid-operation losses: if a merge, rebase, or cherry-pick was interrupted, the original state is recorded in .git/ORIG_HEAD, .git/MERGE_HEAD, or the rebase directories under .git/rebase-merge/ and .git/rebase-apply/. git rebase --abort or git merge --abort returns to the pre-operation state, which may be all you need.
Commands that destroy work, and safer replacements
Most losses trace to a small set of commands. Knowing the safer alternative is worth more than any recovery technique.
| Destructive command | What it loses | Safer habit |
|---|---|---|
| git reset --hard | Uncommitted changes; moves branch tip | Commit or stash first; use git reset --soft or --mixed when only the index should move |
| git checkout -- . / git restore . | All unstaged edits | git stash push -m "wip" before cleanup |
| git clean -fd | Untracked files and directories | git clean -nd (dry run) first; never combine with -x casually |
| git branch -D | The branch ref (commits survive until pruned) | git branch -d refuses to delete unmerged branches |
| git push --force | Remote history others may depend on | --force-with-lease, and coordinate with collaborators |
Two of these deserve a note. git clean -fd is the most dangerous because untracked files have no Git record at all — they're gone the moment the command finishes, and only editor history or OS backups can bring them back. And git push --force is often described as a local problem when it isn't: it rewrites shared history, and recovering on the remote side requires the same reflog approach on the server or someone's local clone.
A worked recovery, start to finish
Suppose you're on main, you'd been building a feature on feature/checkout, and you ran git branch -D feature/checkout by mistake after deciding it was merged — then realized it wasn't. Steps:
1: git reflog — scan for the last time you were on that branch. You see 9f8e7d6 HEAD@{12}: checkout: moving from feature/checkout to main. That hash is the branch tip as of your last checkout. 2: git log --oneline 9f8e7d6 -5 — confirm the commits are the checkout work. 3: git branch feature/checkout 9f8e7d6 — recreate the branch. 4: git checkout feature/checkout and git status — clean tree, commits present.
If step 1 had shown nothing for that branch, git reflog show feature/checkout would have been the next check, and git fsck --unreachable the fallback.
A second scenario: you ran git reset --hard HEAD~2 on main and then made three new commits before realizing the two you dropped were needed. Here you don't want to reset main back — that would discard the three new commits. Instead:
1: Find the pre-reset tip in git reflog (the entry labeled reset: moving to HEAD~2 is the one after the good state; the entry above it is the tip you want). 2: git branch rescued to anchor it. 3: git cherry-pick onto main, or merge rescued if the history shape allows it.
The arithmetic here is simple but easy to get wrong: HEAD~2 means two commits back from the current tip, so if you've made new commits since, the old tip is not at a fixed offset from HEAD anymore. Always work from hashes copied out of the reflog, not from relative expressions.
Preventing the next loss
The recovery steps above work because Git keeps history longer than most people expect. A few habits make recovery unnecessary:
Commit in small increments, even on work-in-progress branches. A commit costs nothing and turns an unrecoverable edit into a recoverable one. Use git stash push -m "message" with a descriptive label before switching branches or pulling. Labeled stashes are far easier to find weeks later. Enable editor local history. VS Code's timeline and JetBrains Local History are the only safety nets for never-committed, never-stashed edits. Prefer git branch -d over -D, and let Git refuse when the branch isn't merged. Don't run git gc reflexively. It's rarely needed manually, and running it during a recovery attempt can close the window you're working in.
One more: on shared repositories, a git push --force by a collaborator can make your local branch appear to have "lost" changes when the remote was rewritten. In that case, your reflog is again the answer — the commits are in your local object database from before the fetch, and you can rebase or cherry-pick them onto the new remote history.
What to check before you declare it unrecoverable
Before concluding the work is gone, verify three things. First, run git reflog and git reflog show and read every entry, not just the top few — the relevant line may be older than you expect. Second, run git stash list and git fsck --unreachable and inspect any dangling commits and blobs. Third, check the editor's local history and any OS-level backups, which are the only sources for never-committed edits.
If all three come up empty and the repository has had a garbage collection run past its prune grace period, the work is likely beyond Git's reach. At that point, a data-recovery tool or a filesystem snapshot is the remaining option, and the honest answer is that it may not work. The reflog, the stash, and editor history cover the large majority of real-world cases — which is why the first step is always to look, quietly, before running anything that writes.