Generally, the only way to recover from this is to find a repository—often one used by a colleague / friend / co-worker—who has the right commit(s), or to use the server's backups (your server does have backups made and tested regularly, right? Right?!? ). The reason for this is:
- Server-side Git repositories have reflogs disabled by default.
- Even if not, deleting the server's branch name with
git push --delete
deletes any relevant reflog the server had.
- It deletes your own remote-tracking branch as well: your
origin/branch
is gone, along with any reflog you had for that.
The branch-name deletion may not have triggered a git gc
in the server (this is not predictable) and definitely did not trigger one in your own repository (this, fortunately, is), so if you know the right hash ID, you can run:
git push origin <hash>:refs/heads/branch
but you usually don't know the right hash ID: that information was in the branch name on the server (gone now), your remote-tracking name (gone now), and any relevant reflogs in your and/or the server's repository (all of which are gone now).
That information does exist in any colleague's clone, provided that they (a) have run git fetch origin
relatively recently and (b) have not run git fetch origin
in such a way that their Git also deleted the information. This is the only reason I know of that one might not want to set fetch.prune
to true
: it causes your own Git repository to retain stale remote-tracking names, in case one of your colleagues executes this particular stunt.
Since responsible server sites make regular backups, though, you can always recover the information from a backup.