Human error is one of the most common causes leading to data loss or data breaches. In the ITIC report, they state that 64 % of downtime incidents have their roots in human errors.

If you think that in SaaS environments all your data is safe, you need to think once again. All SaaS providers, including Microsoft, follow the Shared Responsibility Model, which states that the service provider is responsible for the accessibility of its infrastructure and services, while a user is responsible for their data availability, including backup and disaster recovery.

So, who will be responsible for data recovery if your Azure DevOps branch is deleted? This can be caused by a range of things, from accidental deletion or simple cleanups to incorrect coding practices. Hovewer, the user is the one who needs to deal with the consequences of deletion, such as data loss, failure of compliance, or legal requirements. Thus, it is necessary that your organization can recover fast from any event of failure, including accidental deletion.

In this article, we will outline the best ways and practices to restore your deleted Azure DevOps branch. Though it’s worth stating that the safest and most reliable method is to implement a backup & DR solution to ensure your Azure DevOps security strategy. Such tools, as GitProtect backup & DR software for Azure DevOps, can provide you with the most flexible options for securing and restoring branches.

Accidental deletion?

Human error remains one of the leading causes of failures, including accidental deletions. In fact, research from ResearchGate shows that 32% of data loss incidents are due to human mistakes. This makes it critical to pair secure coding practices with a reliable backup strategy to keep your Azure DevOps branches both protected and recoverable.

Consider this scenario: your teammate accidentally deletes a feature branch just before it’s merged. How quickly can you restore it and resume work? With the cost of downtime reaching up to $9,000 per minute, and that’s without accounting for regulatory penalties… So, every second counts.

Accidental deletion is a common risk that organizations must plan for in both their operational and compliance strategies. When equipped with the right backup solution and a well-tested Disaster Recovery plan, restoring any branch can be fast, seamless, and secure.

Proper branch management in Azure DevOps

To avoid losing any branches to accidental deletions, it is crucial to implement secure coding practices along with proper configuration of branch policies. To edit branch policies, you will need to be a member of the Project Administrators security group or at least have repository-level Edit policies permissions. Manage your branch policies by locating and selecting Repos, then Branches to open the web portal. Now, simply find the branch you need to adjust using the Search branch name in the upper right corner. Click on More options right next to the branch and select Branch policies from the menu available.

Apart from implementing a third-party backup & DR solution and adjusting the policies for your branches, it is important to pay attention to other key best practices. To avoid errors, accidental deletions, and branches being lost, try to keep your strategy simple and build according to the following practices:

  • Maintain good quality and an up-to-date main branch
  • Make use of feature branches for any new features or bug fixes
  • Using pull requests, merge your feature branches into the main branch

Possible ways to restore deleted branches

You can use Git commands to restore the branch. This process relies on Git’s reflog (reference log), which keeps track of changes to the repository, including branch deletions. You can also restore from your local repo or use the web portal.

Method # 1: Azure DevOps web portal

As stated by Azure DevOps official documentation, 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 method is a convenient way to restore a deleted git branch. To start off, locate your repository, open it, and go to the Branches view. Now, find your specific branch, using the Search all branches option in the top right corner. Then, click the link to search for the exact match in deleted branches

This option helps to gain info about the commits, who deleted the branch, and when. Finally, in order to restore a deleted git branch in your Azure DevOps, select the “…” icon, which is right by the branch name, and then click Restore branch from the list. The branch will then be recreated at the last commit to which it pointed.

restore deleted branch in Azure DevOps 1

Source: Restore a deleted Git branch from the web portal

Bear in mind, branch policies and permissions do not get restored. Additionally, if you have used the same branch name for a number of different commits, you might not see all the commits you expected to have when you restore the deleted branch. To help with that, simply go to the Pushes page of your restored branch and then see the whole history of your branch. 

Also, to get to a specific commit, you will need to select New branch from the “…” icon. Then, you can use a pull request, cherry-pick, or merge to add any of the commits back into your desired branch.

restore deleted branch in Azure DevOps 2

Source: Restore a deleted Git branch from the web portal

It is best suited for those who:

  • prefer a GUI-based method rather than a command-line,
  • have full access to the repo, need quick restore
  • want to review any commit info (for example, who deleted the branch)

Hovewer, there are some facts that you need to keep in mind. This process of restore isn’t automated; thus, it increases the risk of mistakes, especially under time pressure or during the incident. Another important thing is a limited time frame. Azure DevOps retains deleted branches for only 90 days. After that, the data is permanently deleted, and if you notice the deletion after that time, your data might be lost.

What is required to restore a branch?

It is important to remember that there are certain requirements outlined that you will have to meet in order to be able to restore branches. Keep in mind that users with Stakeholder access will have full access to Azure Repos (including viewing, cloning, and contributing). Take a look below to see more detailed requirements.

restore deleted branch in Azure DevOps 3

Source: Azure DevOps documentation

Method # 2: Restore from the local repository

Since git is a distributed system, the developer’s local repository is actually a full copy of the project history. So all the commits and references are there, provided ‘git fetch’ has not been run, as it removes or updates the references from remotes. 

If your branch was deleted from the remote, restoring from the local repo is a viable option. Now, in your local repo, run ‘git branch -r’ and this should list all the remote branches. So, when someone deletes ‘stage’ on the remote, but you haven’t fetched yet, your local still has origin/stage. Now, create a new local branch – ‘stage’, that points to the desired commit that your deleted branch pointed to. To do this, run ‘git checkout -b stage origin/stage’. Now that you have restored your branch locally, simply ‘git push’ it to Azure DevOps.

However, if you did fetch after deletion, there is a chance your reference origin/stage, along with the deleted branch, may be gone.

Method # 3: Recovering a Deleted Branch with ‘git reflog’ 

If you’ve accidentally deleted a branch using git branch -D <branch_name>, then git reflog can help. Reflog (reference log) keeps track of where your HEAD and branch references have pointed in your local repository, even after actions like deletions, resets, or rebases. It allows you to view the recent history of commits, including those that may no longer be part of a visible branch.

Option 1: Immediate Recovery After Deletion

If you just deleted the wrong branch and haven’t run many additional Git commands, the recovery can be almost instantaneous. Simply:

  1. Check your terminal output — it often displays the last commit SHA associated with the deleted branch.
  2. If not, run git reflog to locate the commit hash tied to the deleted branch.
  3. Once you have the correct SHA, restore the branch with: git checkout <SHA> This checks out the commit in a detached HEAD state.
  4. To recreate the branch: git checkout -b <branch_name> This command creates a new branch at that commit and reattaches HEAD.

This approach ensures that your work isn’t lost, provided the deleted commits haven’t yet been garbage-collected by Git (which typically occurs after 90 days).

Option 2: Recovering a Branch a while after deletion

If you didn’t notice the deletion right away, or have made other changes since, you can still recover the branch using git reflog:

  1. Run the reflog to inspect your recent Git history: git reflog This will show a list of recent HEAD movements, including the commit where your deleted branch last pointed.
  2. Find the correct entry — look for the commit SHA that corresponds to the last known state of your deleted branch. It might be labeled something like: abc1234 HEAD@{4}: checkout: moving from <branch_name> to master
  3. Check out that commit using: git checkout abc1234 This puts you in a detached HEAD state at the desired commit.
  4. Recreate the branch at that commit: git checkout -b <branch_name>
  5. Optionally, push it back to the remote if needed: git push origin <branch_name>

This method works even if some time has passed, as long as the commit hasn’t been garbage-collected (which by default happens after 90 days in Git).

Method # 4: Third-party solutions to address risks

Relying on a third-party backup and disaster recovery solution is the most dependable way to restore your Azure DevOps data, even beyond the 90-day mark after a branch has been deleted. Every project within Azure DevOps should be properly secured to avoid disruptions and data loss.

Microsoft itself outlines in its documentation that securing Azure DevOps data is the user’s responsibility, and it is recommended to implement third-party backup and disaster recovery solutions for maximum security and compliance. 

Unlike manual restore methods or temporary workarounds, a trusted backup solution functions as an insurance policy; it simplifies and accelerates restore processes while providing a reliable safety net. It reduces downtime, minimizes risk, and offers guaranteed data recovery, something that manual processes can’t ensure.

How to restore a deleted branch in Azure DevOps with GitProtect

Without a proper backup strategy aligned with industry best practices, recovery may not be possible when disaster strikes. That’s why your backup must include:

  • Automated, scheduled backups that meet your defined RPO and RTO objectives,
  • Long-term or even infinite retention, enabling point-in-time recovery and fulfilling Shared Responsibility Model obligations,
  • Multi-storage localization, which allows you to have your backup copies in multiople locations, both cloud and local,
  • Replication,
  • AES encryption at rest and in transit, with the option to use your own encryption keys,
  • Ransomware protection, essential as backup, is your last line of defense,

These capabilities ensure your Disaster Recovery plan is ready for every scenario, including accidental deletions. GitProtect provides all these backup features and even goes beyond to make sure that every disaster scenario is foreseen. Thus, with the solution, you get key restore capabilities:

  • Point-in-time restores – go back to any specific point in time and restore data from that period.
  • Granular restore – specify what pieces of Azure DevOps data you need to restore.
  • Full data recovery – use to recover the entire Azure DevOps environment.
  • Cross-over recovery – restore your Azure DevOps data to another Git hosting service (GitHub, GitLab, Bitbucket).

So, to restore your deleted Azure DevOps branch from a backup, you just need to pick up the moment from which you need to restore your data, and run the restore.

Let’s imagine that your organization’s branch was deleted yesterday, so all you need to do is roll back your data from before yesterday.

restore deleted branch in Azure DevOps 4

Conclusion

To sum up, for the highest level of security and most flexible restore options, it is recommended to implement third-party backup and disaster recovery vendors, such as GitProtect. This way, you get a preventive safety net in the form of immutable, automated, and scheduled backups along with many different restore capabilities to accommodate any restore scenario you may be facing in Azure DevOps.

[FREE TRIAL] Ensure compliant DevOps backup and recovery with a 14-day trial 🚀
[CUSTOM DEMO] Let’s talk about how backup & DR software for DevOps can help you mitigate the risks

Comments are closed.

You may also like