Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to undelete a branch on github?

Tags:

git

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.

like image 914
mans Avatar asked May 06 '13 12:05

mans


People also ask

Is it possible to recover deleted git branch?

A deleted Git branch can be restored at any time, regardless of when it was deleted. Open your repo on the web and select the Branches view. Search for the exact branch name using the Search all branches box in the upper right. Click the link to Search for exact match in deleted branches.

Can you undelete from GitHub?

If you have deleted the file and already committed the changes, you need to use the ` git checkout` command to restore the file. First, you need to find out the checksum of the commit that deleted the file, and then check out the file from the previous commit.

How long does GitHub keep deleted branches?

I asked GitHub Support, this was their response (emphasis mine): We use a separate ref namespace for all Pull Requests which we use for various things including restoring the branch. Since we keep those [Pull Request] refs indefinitely, there's no time limit on restoring a branch.

What happens when a branch is deleted in git?

In Git, branches are just pointers (references) to commits in a directed acyclic graph (DAG) of commits. This means that deleting a branch removes only references to commits, which might make some commits in the DAG unreachable, thus invisible.

How to recover a deleted branch in GitHub?

I am using Github as remote server. If you know the last commit message of the deleted branch you can do this: If this branch was deleted during the Pull Request, you can undo that right there in the UI using the "restore branch" button.

How to delete a local branch in Git?

Local branches are branches on your local machine and do not affect any remote branches. The command to delete a local branch in Git is: git branch is the command to delete a branch locally. -d is a flag, an option to the command, and it's an alias for --delete.

How do I undo a deleted branch?

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.

What is the retention policy on deleted Git branches?

There is no retention policy on deleted branches. A deleted Git branch can be restored at any time, regardless of when it was deleted. Open your repo on the web and select the Branches view. Search for the exact branch name using the Search all branches box in the upper right. Click the link to Search for exact match in deleted branches .


1 Answers

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 
like image 106
sites Avatar answered Sep 23 '22 23:09

sites