↑ ↓ select, Enter open, Esc close

Restore an accidentally deleted branch

Caution: This command changes the system. Understand what it does before you run it.

Adjust the values, the command updates live
user@server
git branch feature/contact-form a1b2c3d

Creates the branch again and points it at the given commit, which brings back the branch’s complete history. git branch -D prints the hash right when deleting (Deleted branch ... (was a1b2c3d)). If that output is gone, git reflog helps, as it lists the last checkout of the branch with its hash.

Note: This only works as long as the commits have not been removed by garbage collection. If the branch still exists on the remote, git switch with the branch name is enough.

Also searched as

  • git recover deleted branch
  • undo git branch -D
  • accidentally deleted branch

Related one-liners

All in Git

Read first, then run.

The commands on myline.de act directly on servers, files and databases. A wrong path or placeholder can delete data irreversibly or make a server unreachable.

  • All commands are provided without warranty and are not tested on every system.
  • Understand what a command does before running it, and check every placeholder.
  • Make a backup first and, if possible, try it on a test system.
  • You run commands at your own risk. Liability for damages is excluded to the extent permitted by law.