Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Recover deleted (remote) branch

Tags:

git

github

I need to recover two Git branches that I somehow deleted during a push.

These two branches were created on a different system and then pushed to my "shared" (github) repository.

On my system, I (apparently) retrieved the branches during a fetch:

~/myfolder> git fetch remote: Counting objects: 105, done. remote: Compressing objects: 100% (58/58), done. remote: Total 62 (delta 29), reused 0 (delta 0) Unpacking objects: 100% (62/62), done. From github.com:mygiturl  * [new branch]      contact_page -> origin/contact_page    731d1bb..e8b68cc  homepage   -> origin/homepage  * [new branch]      new_pictures -> origin/new_pictures 

Right after that I did a push to send my local changes up to the central repo. For some reason, these branches were deleted from both my local system and the central repo:

~/myfolder> git push Counting objects: 71, done. Delta compression using up to 2 threads. Compressing objects: 100% (43/43), done. Writing objects: 100% (49/49), 4.99 KiB, done. Total 49 (delta 33), reused 0 (delta 0) To [email protected]:mygiturl.git  - [deleted]         contact_page  + e8b68cc...731d1bb homepage -> homepage (forced update)    bb7e9f2..e0d061c  master -> master  - [deleted]         new_pictures    e38ac2e..bb7e9f2  origin/HEAD -> origin/HEAD    731d1bb..e8b68cc  origin/homepage -> origin/homepage    e38ac2e..bb7e9f2  origin/master -> origin/master  * [new branch]      origin/contact_page -> origin/contact_page  * [new branch]      origin/new_pictures -> origin/new_pictures 

It's not terribly easy to get the branches off of their birthplace machine, so I'd like to try and recover them from my local if possible.

All of the git "undo" information I've googled has to with recovering lost commits. I don't think that applies here, since I don't have commit UIDs for these branches.

I'd like to know how I can get these back. I'd also like to know how they were deleted in the first place and how I can avoid this in the future.

EDIT: by request, here's my repo configuration

user.name=Craig Walker [email protected] alias.unadd=reset HEAD core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true core.ignorecase=true remote.origin.fetch=+refs/heads/*:refs/remotes/origin/* [email protected]:MyGitURL.git remote.origin.mirror=true branch.master.remote=origin branch.master.merge=refs/heads/master alias.undo=reset --hard alias.test=push -f ci HEAD:master alias.st=status alias.ci=commit alias.br=branch alias.co=checkout alias.ch=checkout alias.df=diff alias.lg=log -p alias.who=shortlog -s -- remote.ci.url=ContinuousIntegrationGitURL remote.ci.fetch=+refs/heads/*:refs/remotes/ci/* branch.photo.remote=origin branch.photo.merge=refs/heads/photos remote.foo.url=FooGitURL remote.foo.fetch=+refs/heads/*:refs/remotes/cynthia/* branch.homepage.remote=origin branch.homepage.merge=refs/heads/homepage 
like image 243
Craig Walker Avatar asked Jan 02 '10 18:01

Craig Walker


People also ask

Can I recover deleted branch in git?

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 I see deleted branches in GitHub?

Restoring a deleted branchOn GitHub.com, navigate to the main page of the repository. Under your repository name, click Pull requests. Click Closed to see a list of closed pull requests. In the list of pull requests, click the pull request that's associated with the branch that you want to restore.

Can we recover deleted branch from bitbucket?

In the case of manual search using reflog tool, we must be aware that even the Bitbucket in their official documentation recommends a full backup of our project before starting this process. This means that using the GitProtect.io solution will allow us to reverse the lost branch with one click.


1 Answers

I'm not an expert. But you can try

git fsck --full --no-reflogs | grep commit 

to find the HEAD commit of deleted branch and get them back.

like image 107
iamamac Avatar answered Oct 04 '22 09:10

iamamac