in Technology by

Can you recover a deleted branch in Git?

1 Answer

0 votes
by

 Yes, you can. To recover a deleted branch, you should know the SHA off the top of your head. SHA or hash is a unique ID that Git creates with every operation.

When you delete a branch, you get the SHA displayed on the terminal:

Deleted branch <your-branch-name> (was <sha>)

You can use the below command to recover the deleted branch:

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

If you don’t know the SHA for the commit at the tip of your branch then you can first use the git reflog command to know the SHA value and then apply the above checkout command to restore your branch.

Related questions

0 votes
    What work is restored when the deleted branch is recovered?...
asked Nov 2, 2020 in Technology by JackTerrance
0 votes
    When you run git fetch from my local repo, will it update your local code and target branch? (1)Yes, the command ... new commits and keep them in a separate branch in local repo...
asked Oct 26, 2020 in Technology by JackTerrance
0 votes
    How to find and restore a deleted file in a Git repository?...
asked Jan 8, 2021 in Technology by JackTerrance
0 votes
    When you run git fetch from my local repo, will it update your local code and target branch? A. Yes, the command ... new commits and keep them in a separate branch in local repo...
asked Dec 20, 2022 in Education by JackTerrance
0 votes
    I created a local branch that I want to 'push' upstream. There is a similar question here on Stack ... the upstream repository? Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    Say I'm in a Git repository. I delete a file and commit that change. I continue working and make some ... my original project checkout. Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    How can we create a remote Git branch?...
asked Jan 8, 2021 in Technology by JackTerrance
0 votes
    How can we check out a remote Git branch?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    How can we rename a local Git branch?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    What is the Git command to move branch pointer to different commit without checkout? A. Git cherrypick B. Git update-ref C. Git reset head...
asked Dec 24, 2022 in Technology by JackTerrance
0 votes
    What is the Git command to create a branch? A. git invoke branch branchname B. git add branch branchname C. git create branch branchname D. git branch branchname...
asked Dec 20, 2022 in Technology by JackTerrance
0 votes
    How to rename and remove branch in remote Git?...
asked Aug 24, 2021 in Technology by JackTerrance
0 votes
    How to get the current branch name in Git?...
asked Jan 8, 2021 in Technology by JackTerrance
0 votes
    How to push a new local branch to a remote Git repository and track it too?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    Move the most recent commit(s) to a new branch with Git?...
asked Jan 7, 2021 in Technology by JackTerrance
...