Git: Recover deleted (remote) branch

125,685

Solution 1

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.

Solution 2

just two commands save my life

1. This will list down all previous HEADs

git reflog

2. This will revert the HEAD to commit that you deleted.

git reset --hard <your deleted commit>
ex. git reset --hard b4b2c02

Solution 3

Your deleted branches are not lost, they were copied into origin/contact_page and origin/new_pictures “remote tracking branches” by the fetch you showed (they were also pushed back out by the push you showed, but they were pushed into refs/remotes/origin/ instead of refs/heads/). Check git log origin/contact_page and git log origin/new_pictures to see if your local copies are “up to date” with whatever you think should be there. If any new commits were pushed onto those branches (from some other repo) between the fetch and push that you showed, you may have “lost” those (but probably you could probably find them in the other repo that most recently pushed those branches).

Fetch/Push Conflict

It looks like you are fetching in a normal, ‘remote mode’ (remote refs/heads/ are stored locally in refs/remotes/origin/), but pushing in ‘mirror mode’ (local refs/ are pushed onto remote refs/). Check your .git/config and reconcile the remote.origin.fetch and remote.origin.push settings.

Make a Backup

Before trying any changes, make a simple tar or zip archive or your whole local repo. That way, if you do not like what happens, you can try again from a restored repo.

Option A: Reconfigure as a Mirror

If you intend to use your remote repo as a mirror of your local one, do this:

git branch contact_page origin/contact_page &&
git branch new_pictures origin/new_pictures &&
git config remote.origin.fetch '+refs/*:refs/*' &&
git config --unset remote.origin.push &&
git config remote.origin.mirror true

You might also eventually want to do delete all your refs/remotes/origin/ refs, since they are not useful if you are operating in mirror mode (your normal branches take the place of the usual remote tracking branches).

Option B: Reconfigure as a Normal Remote

But since it seems that you are using this remote repo with multiple “work” repos, you probably do not want to use mirror mode. You might try this:

git config push.default tracking &&
git config --unset remote.origin.push
git config --unset remote.origin.mirror

Then, you will eventually want to delete the bogus refs/remotes/origin refs in your remote repo: git push origin :refs/remotes/origin/contact_page :refs/remotes/origin/new_pictures ….

Test Push

Try git push --dry-run to see what it git push would do without having it make any changes on the remote repo. If you do not like what it says it is going to do, recover from your backup (tar/zip) and try the other option.

Solution 4

  1. find out commit id

    git reflog

  2. recover local branch you deleted by mistake

    git branch <NEED-RECOVER-BRANCH-NAME> commitId

  3. push need-recover-branch-name again if you deleted remote branch too before

    git push origin <NEED-RECOVER-BRANCH-NAME>

Solution 5

If the delete is recent enough (Like an Oh-NO! moment) you should still have a message:

Deleted branch <branch name> (was abcdefghi).

you can still run:

git checkout abcdefghi

git checkout -b <some new branch name or the old one>

Share:
125,685

Related videos on Youtube

gigimon
Author by

gigimon

Software Developer and Technology Consultant

Updated on July 08, 2022

Comments

  • gigimon
    gigimon almost 2 years

    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
    
    • Chris Johnsen
      Chris Johnsen over 14 years
      Your remote.origin.fetch refspec is not appropriate for use with remote.origin.mirror = true. Do you want to mirror or do you want to use the GitHub repo as a normal remote? My answer should have the commands you need either way.
  • gigimon
    gigimon over 14 years
    I tried fsck earlier; do you know how to find out which commit is the correct one? I've got 20 to try.
  • gigimon
    gigimon over 14 years
    I don't think the remote tracking branches were kept, if they were copied at all. 'git branch -a' doesn't show them, and I can't find any files with those names in the .git dir either. Lastly, the "git log" commands you recommended return "fatal: ambiguous argument 'origin/contact_page': unknown revision or path not in the working tree" :-\ Thanks though.
  • gigimon
    gigimon over 14 years
    As with my comment to @Chris Johnson, it appears that the branches no longer (never?) exist locally. When I git push origin origin/contact_page:contact_page I get this: error: src refspec origin/contact_page does not match any
  • Chris Johnsen
    Chris Johnsen over 14 years
    Well, those branches were there, your push log shows it. When looking for refs in the .git dir, be sure to check .git/packed_refs in addition to .git/refs/. git show-ref will dump out all your local refs (packed or ‘loose’). You should still be able to find the refs in the repo that originally pushed them to your GitHub repo (on a different machine? someone else's repo?). Failing that, as long as you have not done a gc or prune, you should be able to the git fsck output to examine the dangling commits and reattach them: git branch contact_page-recovered <SHA-1-of-dangling-commit>.
  • gigimon
    gigimon over 14 years
    This did it; once I had the commit messages, git branch <uid> got them back. Thanks!
  • Chris Johnsen
    Chris Johnsen over 14 years
    Good to hear. Be sure to also resolve the conflict between your remotes.origin.mirror and remotes.origin.fetch settings, otherwise you are bound to run into the problem again (or unintentionally clobber commits pushed from other repos).
  • iamamac
    iamamac over 14 years
    @Craig: Glad to be helpful :)
  • gigimon
    gigimon over 14 years
    packed_refs didn't have it either. The commits were definitely dangling; no idea how that happened. Thanks for your help though!
  • CB Bailey
    CB Bailey over 14 years
    OK, I think I see what's happened, (although the full error would be helpful). push has updated the deleted branch and removed the ref locally as well as it's a tracking ref. What does git rev-parse refs/remotes/origin/origin/contact_page say ? Because of the bogus 'mirror' config, the branch my now be referenced here in the local repository.
  • gigimon
    gigimon over 14 years
    Hi Charles; Since I wrote this I've munged (and fixed) my config so I can't get the (meaninful) rev-parse output any more. However, I don't think there was a double-nested "origin" directory in remotes.
  • Xtro
    Xtro over 8 years
    Saved my life! :) It was hard to use "git show" on all of the reported commits but at the end, I found the correct one and recovered it.
  • Gus
    Gus about 7 years
    Best git command for the absent-minded! Doing a git checkout <branch> <commit_id> creates the branch with the changes made in the commit.
  • CoolMind
    CoolMind almost 7 years
    Didn't restore a remote branch.
  • spezifanta
    spezifanta over 6 years
    I lost a release candidate branch today. Did not know the commit id. Got it recovered by using: git fsck --full --no-reflogs | cut -d' ' -f3 | xargs -P8 git log --oneline | grep 'Release 2.60.0.157'
  • theUtherSide
    theUtherSide about 5 years
    This worked for me. I prefer over the accepted answer because it was far fewer steps. I was able to see my commit message from git reflog, rather than having to guess and git show.
  • zyy
    zyy almost 4 years
    I never checked into the branch locally so my HEAD has never been there, therefore I can not find the commit ID with git reflog. Is there anything else I can try?
  • OmGanesh
    OmGanesh almost 4 years
    Same as @zyy The commit has been deleted by other team member in remote, so I have to get it back in my local machine ( I never had that commit locally) and push it back...
  • navid_gh
    navid_gh over 3 years
    this saved me 5 days working non-stop 13 hours. thank you so much. I just by mistake removed my remote and local branch but I could recover it with this

Related