Skip to content

Instantly share code, notes, and snippets.

@NehaP09
Created February 3, 2022 16:06
Show Gist options
  • Select an option

  • Save NehaP09/36b583b03fcb671caf5bf4c7b036deb4 to your computer and use it in GitHub Desktop.

Select an option

Save NehaP09/36b583b03fcb671caf5bf4c7b036deb4 to your computer and use it in GitHub Desktop.

Revisions

  1. NehaP09 created this gist Feb 3, 2022.
    18 changes: 18 additions & 0 deletions Recover_deleted_remote_branch.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    I accidentally deleted a git branch from both local and remote and also refreshed the page so the "recover" option was gone.
    I didn't lose any code, since I had a backup copy.
    But I did lose the commit history.
    I was left with all code changes from all 16 commits in a folder.

    This is what I did to recover the deleted local and dev branch:
    1. git checkout master
    2. git reflog
    3. obtain the SHA of the top/latest commit on the deleted branch
    4. get out of the reflog mode
    5. there are 2 ways here:
    a. if you want to also rebase the deleted_branch_name to master do this:
    git branch <deleted_branch_name> <top_commit_SHA>
    b. if you don't want to push the commits from master to deleted_branch_name do this:
    git checkout -b <deleted_branch_name> <top_commit_SHA>
    6. git push origin <deleted_branch_name>

    And voila! My heart was no longer in my mouth!