0

I pushed my branch to the gitlab and then deleted my local branch but someone deleted remote branch by mistake is there any way to recover my branch

Liam
  • 25,247
  • 27
  • 110
  • 174
Nehic
  • 11
  • 3
    You can use [`git reflog`](https://git-scm.com/docs/git-reflog) to find out the past locations of your branch then re-create it on its last position. – axiac Aug 19 '20 at 07:10

1 Answers1

1
  1. If you just deleted the branch, you'll see something like this in your
Deleted branch <your-branch> (was <sha>)

    To restore the branch, use:


git checkout -b <branch> <sha>
  1. If you don't know the 'sha' off the top of your head, you can:

    Find the 'sha' for the commit at the tip of your deleted branch using:

git reflog

    To restore the branch, use:

git checkout -b <branch> <sha>
  1. If your commits are not in your reflog:

    You can try recovering a branch by reseting your branch to the sha of the commit found using a command like:

git fsck --full --no-reflogs --unreachable --lost-found | grep commit | cut -d\  -f3 | xargs -n 1 git log -n 1 --pretty=oneline > .git/lost-found.txt

    You can then display each commit using one of these:

git log -p <commit>
git cat-file -p <commit>
spike 王建
  • 1,350
  • 4
  • 12