All fixes

Help, I deleted a branch I still needed

Deleting a branch only deletes the label. Put it back on its last commit and everything is there again.

What Git says~/my-app
git branch -D feature/login
Deleted branch feature/login (was cdc3c7a).

The fix

  1. git reflog

    Only needed if the hash has scrolled away. The branch's last commit is on the line just below “checkout: moving from feature/login to …”, not on that line itself.

  2. git branch feature/login cdc3c7a

    Recreates the branch on that commit, using the hash Git printed when it deleted the branch.

feature/login is an example. Replace it with your own.

What happens to your historygit branch feature/login cdc3c7a
BeforeABCDHEADmain
AfterABCDHEADmainfeature/login

Why this works

A branch is just a name for a commit. git branch -D removes the name, but the commits stay in the repository for weeks. Git even printed the last commit's hash when it deleted the branch. If that has scrolled away, the reflog has it. And if the branch was pushed before, git switch feature/login simply recreates it from the remote.