Financial Markets

Recovering a Deleted Local Branch on GitHub- A Step-by-Step Guide

How to Get Back a Deleted Local Branch on GitHub

Managing branches in a GitHub repository can sometimes lead to accidental deletions, especially when working with multiple branches. If you’ve found yourself in a situation where you’ve deleted a local branch and now need to recover it, don’t worry; there are several methods you can use to get back a deleted local branch on GitHub. In this article, we’ll discuss the steps to follow to retrieve your deleted branch and ensure that your project remains intact.

Method 1: Using Git Log to Find the Commit SHA

The first method involves using the `git log` command to find the commit SHA of the last commit in the deleted branch. Once you have the commit SHA, you can use it to create a new branch or cherry-pick the commits back into an existing branch.

1. Open your terminal or command prompt.
2. Navigate to your local repository using the `cd` command.
3. Run the following command to display the commit SHA of the last commit in the deleted branch:
“`
git log –oneline –decorate –all
“`
4. Look for the commit SHA that corresponds to the deleted branch.
5. Use the commit SHA to create a new branch or cherry-pick the commits:
“`
git checkout -b new-branch-name
“`
or
“`
git cherry-pick
“`

Method 2: Using Git Reflog to Find the Deleted Branch

Another method to recover a deleted local branch is by using the `git reflog` command. The `git reflog` command keeps a log of all changes made to the repository, including deleted branches. By examining the `git reflog`, you can find the deleted branch and its last commit SHA.

1. Open your terminal or command prompt.
2. Navigate to your local repository using the `cd` command.
3. Run the following command to display the `git reflog`:
“`
git reflog
“`
4. Look for the deleted branch in the list of commits. The branch name will be followed by the word “deleted.”
5. Once you find the deleted branch, note the commit SHA.
6. Use the commit SHA to create a new branch or cherry-pick the commits:
“`
git checkout -b new-branch-name
“`
or
“`
git cherry-pick
“`

Method 3: Using GitHub Web Interface

If you have access to the GitHub web interface, you can also recover a deleted local branch by following these steps:

1. Go to your GitHub repository.
2. Click on the “Branches” tab.
3. Find the deleted branch in the list of branches.
4. Click on the “Compare & pull” button next to the deleted branch.
5. In the “Base branch” dropdown, select the branch where you want to merge the deleted branch’s commits.
6. Click on the “Pull request” button to create a pull request that merges the deleted branch’s commits into the selected branch.

By following these methods, you can successfully get back a deleted local branch on GitHub and continue working on your project without any interruptions. Remember to regularly back up your local repository to avoid such situations in the future.

Related Articles

Back to top button