71

I was wondering if there is a way to restore a remote deleted branch in github. History clearly keeps record of the branch and merges with other branches but I'm not sure if it's possible to restore a deleted branch.

Thanks.

luisgo
  • 2,385
  • 4
  • 24
  • 30

6 Answers6

77

Yes, it's possible to restore a deleted branch from git.

Find your Commit ID: Search for a branch using git reflog

If you had the branch in your local git repo within the last 30 days, you may be able to find it in the reflog using the following:

git reflog

Search for the branch name in the reflog and note the HEAD{x} point or the commit ID.

Re-create the branch from the Reflog HEAD point:

git checkout -b branch_name HEAD@{27}

Re-create the branch from the commit ID:

You can checkout the commit ID and create a branch off of that commit point:

git checkout -b branch_name <commit id>
Andreas
  • 2,821
  • 25
  • 30
Highway of Life
  • 22,803
  • 16
  • 52
  • 80
  • 34
    This doesn't address the OP's problem. All you're doing is explaining a roundabout way to find out what commit you currently have checked out (whether or not it's on a branch). If the deleted remote branches weren't pointing to HEAD, this isn't helpful. They could perhaps be farther back in the reflogs, if you checked them out at some point, though. – Cascabel Jan 13 '11 at 03:47
  • @Jefromi, this is the method for recovering a deleted branch, whether it's further back or at head, you still recover it the same way. – Highway of Life Jan 13 '11 at 19:33
  • 3
    It is... sort of. It's a bit jumbled. You really want to use `reflog` followed by `git branch `. (You're missing an argument there). If for some strange reason you don't want to actually get the original branch back, but just skip ahead and merge it into your current commit, then yes you could `git checkout -b ; git merge `. I don't know why that would be your default suggestion though. – Cascabel Jan 13 '11 at 20:27
  • 3
    Am I missing something?? I don't think this answer even "sort of" answers the question. `git reflog` will show _HEAD_'s history. You have to search that for occurrences of the deleted `foo` branch. finding "HEAD@{0}" does not help at all. See [my answer](http://stackoverflow.com/a/15927338/10608) below. – Alexander Bird Apr 10 '13 at 13:34
  • @AlexanderBird, "Am I missing something??" Yes, please read: http://gitolite.com/concepts/reflog.html – Highway of Life Apr 11 '13 at 03:51
  • @AlexanderBird, yeah after your comment and re-reading the answer, I realized I needed a better explanation. :) -- Thank you!! – Highway of Life Apr 11 '13 at 22:21
  • so if I have a name of a branch which was created 2 years ago with one commit, and then deleted. I have new laptop (so no local commits nor branches). I am unable to restore that branch along with its commits? – fascynacja May 07 '19 at 07:53
  • Not if it was never pushed to your remote. – Highway of Life May 09 '19 at 00:13
  • This is a good answer 99% of the time - but it doesn't cover the edge case where you've deleted a branch in the remote repo that you don't have in your local reflog (eg because someone else created it on the remote and you've never checked it out locally). – piersb May 30 '23 at 16:53
14

It is possible to ask for GitHub support and have them look into the reflog of your remote repo (like in this thread for example).
If this is close enough (less than 30 days per default) from the deletion, the reflog still contains the commits which are no longer referenced by any branch.
Creating a branch on one of those commits allow them to be again accessible.

For more on reflog, see "what the heck is a reflog and why is it so important?"


Update: the repo owner can also query the GitHub EVents API:
See "Does GitHub remember commit IDs?"

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
8

When the branch has been deleted for a very long time (in my case, 1 year), but you had opened a pull request for that branch, you may be able to resurrect it by searching in the pull requests history.

Once I found the pull request for that branch I could restore the branch. Relevant commit information, etc. are also available from the pull request.

Gordon Bean
  • 4,272
  • 1
  • 32
  • 47
  • My branch deletion is older than 30 days - 8 months in fact, any way to restore it ? I couldn't find anything in reflog as expected & I don't have pull request for it. – C.Dhruv Jun 29 '20 at 08:01
7

It's a bit of a runaround, but here's how to do it.

Get yourself a new Personal Access Token from Profile / Settings / Developer Settings / Personal Access Tokens if you don't have one already.

curl -u "username:PersonalAccessToken" -H "Accept: application/vnd.github.v3+json"  https://api.github.com/repos/RepoOwner/Repo/events

Find the DeleteEvent in the response; in there you'll be able to find the orphaned SHA of the branch you deleted.

git fetch SHA
git switch -c name-of-your-deleted branch

Problem solved.

piersb
  • 609
  • 9
  • 22
  • 1
    I ran into an interesting edge case. If "Automatically delete head branches" is set to true these branches won't show up in your events. In this case (and if a branch was in a PR) you can navigate to that PR and click the "Restore branch" button. – runamok Aug 17 '23 at 20:05
1

git reflog will show you the history of HEAD. If the branch you deleted was named foo, then in that output, you should see lines like 48534f5 HEAD@{0}: checkout: moving from master to foo or 48534f5 HEAD@{1}: merge foo: Fast-forward. You can search the output of git reflog to figure out which commit must be the latest one that foo pointed to.

Do realize, that the "foo" reflog file itself is deleted when foo was deleted, but since the HEAD's reflog is different it still exists.

Alexander Bird
  • 38,679
  • 42
  • 124
  • 159
1

Take a look at this python script for github events. https://github.com/jimzucker/githubutils/blob/master/githubreflog.py

I created it to pull events and make them readable, you can pipe it in to grep and look for the branch you are interested in. if there is enough history you will see the delete event for the branch in question, the next line will be the last push event and that is the sha you are interested in.

Jim Zucker
  • 850
  • 9
  • 10