Blog / git
Get back a branch a coding agent deleted
An agent ran git branch -D or deleted a branch on the remote. Find its last commit with the delete message, the reflog or git fsck, and put it back.
On this page
- The delete message has the hash
- No message? Use the reflog
- Not in the reflog either: git fsck
- A branch from a removed worktree
- Check before you rely on it
- Deleted on the remote too
- Keep agents from deleting branches
- Where Joxo fits
- Frequently asked questions
- Can I recover a branch deleted with git branch -D?
- How do I find the last commit of a deleted branch?
- How long can I recover a deleted git branch?
- How do I restore a branch deleted on GitHub?
- Try it with your team
An agent cleaning up "old branches" deleted one you still needed, or deleted it on the remote too. A deleted branch is only a name: its commits stay in your repository for weeks, so you can almost always get it back. We ran every command with git 2.43 on a scratch repository.
The delete message has the hash#
If the agent's output is still on screen, look for the line git printed:
git branch -D searchDeleted branch search (was 74c858a).That hash is the branch's last commit. Make the branch again from it:
git branch search 74c858agit log --oneline search -374c858a Search by tag
c8383bc Add search
78eaf3a Initial commitEvery commit is back, with the same hashes as before.
No message? Use the reflog#
Git keeps a log of where HEAD has been. If anyone worked on the branch on this machine, its last commit is in there:
git reflog -578eaf3a HEAD@{0}: checkout: moving from search to main
74c858a HEAD@{1}: commit: Search by tag
c8383bc HEAD@{2}: commit: Add search
78eaf3a HEAD@{3}: checkout: moving from main to search
78eaf3a HEAD@{4}: commit (initial): Initial commitRead it from the bottom up: the line just before "moving from search" is the last thing done on search, here 74c858a. In a long reflog, search for the branch name with git reflog | grep search. Then git branch search 74c858a as above.
Not in the reflog either: git fsck#
A branch that was only fetched, or made by an agent in another worktree, may not show up in your HEAD reflog. Ask git for commits that nothing points to any more:
git fsck --lost-founddangling commit 74c858a9c0bbf61e6e79e810999ab6c8e5d63bcbCheck each one with git log --oneline -3 <hash> and make a branch from the one that matches. There can be several dangling commits from old rebases and amends; the branch tip is the one whose log has the commits you expect.
Git removes unreachable commits during garbage collection, after two weeks by default (longer for those still in a reflog), so don't wait weeks to look.
A branch from a removed worktree#
Agents often work in their own git worktree, and a cleanup step may remove the worktree and its branch together:
git worktree remove --force ../app-login
git branch -D agent/loginDeleted branch agent/login (was 8984163).Each worktree keeps its own HEAD reflog, and removing the worktree removes that log, so git reflog in your main folder finds nothing for this branch. The delete message and git fsck --lost-found still work: the commits live in the shared repository, not in the worktree's folder. Files the agent never committed were in the removed folder and are gone, so ask agents to commit before they stop.
Check before you rely on it#
Once the branch is back, compare it with what you expected before anyone builds on it:
git log --oneline main..searchThis lists the commits the branch has that main doesn't. If you had pushed the branch before, compare the tip with the hash on your host or in the pull request. If you recovered from a dangling commit and the newest work is missing, there may be a later dangling commit: check the rest of the git fsck list.
Deleted on the remote too#
If the agent also ran git push origin --delete search, the branch is gone from the host but the commits are probably still on your machine. Recover it locally as above, then push it again:
git push -u origin search * [new branch] search -> search
branch 'search' set up to track 'origin/search'.If the branch only ever existed on the remote, recover it from a teammate's clone, or from your host: GitHub shows a "Restore branch" button on a pull request whose branch was deleted.
Keep agents from deleting branches#
Add to your project instructions: "Never delete branches, local or remote. List the ones you think are stale and ask me." git branch -d (lower case) is the safe form if you do let an agent clean up: it refuses to delete a branch that hasn't been merged. On your host, a branch protection rule can block deleting main and release branches.
Before you let an agent tidy up, have it show you what is safe to delete:
git branch --merged main
git branch --no-merged main* main
old-fix search
wipThe first list is already in main and can go; the second still has work that exists nowhere else.
Where Joxo fits#
An agent usually deletes a branch because it doesn't know someone is still on it. Joxo puts each agent's task on a shared board with its status, so an agent can see that a teammate is still working before it cleans up. Setup is one line pasted into your agent. For a single file rather than a branch, see restore a file a coding agent deleted.
Frequently asked questions#
Can I recover a branch deleted with git branch -D?#
Yes. Run git branch <name> <hash>, using the hash from the "Deleted branch ... (was <hash>)" message or from git reflog. The commits are still in the repository.
How do I find the last commit of a deleted branch?#
Look in git reflog for the last line before "checkout: moving from <branch>". If it isn't there, git fsck --lost-found lists commits nothing points to, and one of them is the branch tip.
How long can I recover a deleted git branch?#
Until git's garbage collection removes the unreachable commits, which is two weeks by default for commits not in a reflog and longer for those that are. Recover it as soon as you notice.
How do I restore a branch deleted on GitHub?#
If you have it locally, push it again with git push -u origin <branch>. If not, open the pull request it belonged to and use Restore branch.