larryq
larryq

Reputation: 16299

Restore branch deleted from GitHub

I had a branch on a GitHub project that I merged into master. I then clicked the 'delete branch' button on GitHub, and thought I was all set.

Turns out I wasn't, and I want to restore/reactivate the branch. I did not delete the branch on my local respository, nor did I run any git fetch/pull afterward. Just clicked the delete button on GitHub.

Wanted to sound out what a good next step should be. Thinking of doing git push from my local box but wasn't sure what the repercussions might be, would the remote repo on GitHub squawk about a dead branch being brought back, etc.

Upvotes: 4

Views: 7922

Answers (3)

Loke
Loke

Reputation: 1065

Go to your list of commits. Find the commit with the merge and click on the pull request number (the number prefixed with #). This will direct you to a page with info about the merge and a button with the label 'Restore branch'. Click that and it is restored.

Upvotes: 2

Shadi
Shadi

Reputation: 10335

It looks like there is a "Restore branch" button now that shows up in place of the "Delete branch"

Upvotes: 1

Guillermo Maschwitz
Guillermo Maschwitz

Reputation: 1106

If you didn't remove your branch from your local machine, and you got rights to push to GitHub, you can restore it on Github by pushing it again

git checkout localBranchName
git push origin localBranchName

It doesn't matter if you make a fetch from Github, git wont remove your local branch until you explicitly tell it to do so with

git branch -D localBranchName

In fact, even if you had removed your local branch, if you merged it previously with master, you can restore it locally. You have to go to the last commit, prior to the merge and branch from there. Something like this for example:

git checkout master
git checkout -b localBranchName
git reset --hard HEAD~1 ( 1 is the number of commits you want to undo )

The second command will create a new branch pointing to your last commit on master The third command will the last commit undoing (only on that branch ) the merge with master.

Another thing you can do is use "git reflog". That command is very usefull since it will show each time you moved between branches and/or commits.

Upvotes: 6

Related Questions