mans
mans

Reputation: 18228

How to undelete a branch on github?

It seems that I delete a branch on github when I should not do it.

What I did was as follow:

1- I add a new .gitignore to my system

2- I use

 git rm -r --cached .  
 git add .  
 git commit -m ".gitignore is now working"  

When I did this, I had one branch on my local system but the server had two branch.

Then I pushed my branches to server and since I had not the second branch, the second branch was deleted on server.

How can I bring it back?

I am using Github as remote server.

Upvotes: 33

Views: 39235

Answers (4)

cire
cire

Reputation: 124

Inside github.com you can restore it after the fact by Pull requests, open the last request and you should see a Recover/Restore branch button

Upvotes: 0

Brock
Brock

Reputation: 1655

If this branch was deleted during the Pull Request, you can undo that right there in the UI using the "restore branch" button.

Tricky part is actually finding a PR that has been merged and closed, you just need to know the URL or the PR number to put into the URL. You can try looking in your deleted notification emails or just guess the PR number.

Writing this cause reflog didn't help me restoring a teammate commit to a branch I've never pulled on my local git.

Upvotes: 19

sites
sites

Reputation: 21815

If you know the last commit message of the deleted branch you can do this:

git reflog

# search for message

fd0e4da HEAD@{14}: commit: This is the commit message I want

# checkout revision

git checkout fd0e4da 

or

git checkout HEAD@{14}

# create branch

git branch my-recovered-branch

# push branch

git push origin my-recovered-branch:my-recovered-branch

Upvotes: 84

Luiz E.
Luiz E.

Reputation: 7279

what if you git fetch remote && git checkout -b remote_branch_name

Upvotes: 4

Related Questions