Source Control (4-5), Git - Recover Deleted Branch

NoteThis is a series of articles related to Source Control. If you have read my other articles in this series, you may skip the top general part and jump to INTRODUCTION for the specific topic of this article.

This is a series of articles related to Source Control or Version Control issues, from a stand-alone app, such as MS SourceSafe, to a Server app, such as MS TFS (Team Foundation Server), to web services such as GitHub, AWS, and MS Azure DevOps. We have tried to categorize this series of articles as Source Control or Version Control, but this site does not have these categories. So, we put the articles in the DevOps category, as explained in the Wiki:

DevOps is a set of practices that combines software development (Dev) and IT operations (Ops). It aims to shorten the systems development life cycle and provide continuous delivery with high software quality.[1] DevOps is complementary to Agile software development; several DevOps aspects came from the Agile methodology.

The structure of this article series will cover:

  • Stand Alone App:
    • MS Source Safe
  • Server App
    • MS TFS (Team Foundation Server)
  • Online (Cloud) Centralized Service:
    • MS Azure: DevOps
      • Boards
      • Repos
      • Pipelines
      • Test Plans
      • Artifacts
    • GitHub
    • AWS GitHub Enterprise
  • Distributed App:
    • Git

Because these are huge topics, I will not go step by step. Instead, each section will be relatively independent and will become a reading unit.

A - Introduction

Once I accidentally hard deleted an important branch from both local and server (DevOps). Then I tried to figure out how I could recover the deleted branch in git.  This is the result:

  • A - Introduction
  • B - Recover Deleted Branch from Local
  • C - Recover Deleted Branch from and for Server
    • C.1 - Git Reflog
    •  

B - Recover Deleted Branch from Local

These are solutions searched from online:

Git undo local branch delete - Stack Overflow

Git undo local branch delete - Stack Overflow

Recover deleted git branch from local | by Imran Ahmad | Medium

This article, How to Restore a Deleted Branch or Commit with Git Reflog - Rewind, has a detailed discussion of procedure and reasoning, we just skip that.  This article addresses the practical issue.

So, basically, we have two methods to recover a deleted Branch:

  • git branch <branch name> <sha>
  • git checkout -b <branch name> <sha>

Demo:

We make a new project, saving it into Git repository, we have a master branch by default:

Run command: "git reflog", we have

From Visual Studio, add one new branch as Brach_1:

Branch_1 added:

Run "git reflog", we have:

Add one more new branch as Branch_2:

"git reflog":

We can see three branches, master, Branch_1, and Branch_2 share the same HEAD name as cc2a52e.

Delete Branch_1 from Visual Studio:

We have:

"git reflog":

Delete Branch_2 from Visual Studio, we have:

Run "git reflog":

Now, we retrieve the deleted branch, Branch_1, by the command:

git branch Branch_1 cc2a52e

We have:

 Now, we retrieve the deleted branch, Branch_2, by the command:

git checkout -b Branch_1 cc2a52e

We have:

C - Recover Deleted Branch from Local

From Server (DevOps),

We have a Microsoft article,

However, it does not work, in our case, on the branch page, I do not have a  Search all branches box, but Search branch name box, it just does not work as the article stated.

On the other hand, in my own DevOps online, it even does not include the Branches view:

Those suggested the server is somewhat based on configuration.

For Server

Fortunately, in the article above, it says:

  • This article addresses how to restore a deleted Git branch using the web portal in Azure Repos. If you need to restore a Git branch in your own repo from Visual Studio or the command line, push your branch from your local repo to Azure Repos to restore it.

This does work.  So, when we have a local branch, or if we can recover the deleted local branch, then just simply push to server, then we have server one for it.

Reference


Similar Articles