Samir
Samir

Reputation: 691

how to recover remote branch git?

Someone has accidentally deleted alpha branch in my team.

I have remote branch origin/alpha. I am not able to find this origin/alpha branch using git ls-remote.

I know last I did commit was XYZ on origin/alpha branch. I have SHA of that branch.

I am trying to create a new branch using this SHA. I used git checkout 45430f8834b0ebda6e89668cc4a4ba3f6a2067a4.

after that I tried to check out new branch using git checkout -b [NEW_BRANCH]

I am trying to git pull this branch. but I am getting below error

There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

git branch --set-upstream-to=origin/<branch> alpha_recovered

Any idea how can I recover my remote branch which I am not able to see in git ls-remote.

Upvotes: 1

Views: 295

Answers (1)

jthill
jthill

Reputation: 60555

Since you were tracking the branch, just push that:

git push origin origin/alpha:refs/heads/alpha

The odd syntax is necessary because push's heuristics for filling out the spelling of refnames are ... maybe just appropriately careful about guessing what you wanted to do to a remote repo. I'm on the fence about that call myself, but regardless, origin/alpha resolves to your repo's refs/remotes/origin/alpha ref, the conventional tracking ref for origin's refs/heads/alpha, but push isn't set up to make resetting a remote branch to something unusual like that an easy thing to do. Fair enough I suppose, as it's not a common operation, so perhaps it's best that it forces the full spelling of the target ref on the remote here.

edit: since you have the sha, you could also

git push origin 45430f8834b0ebda6e89668cc4a4ba3f6a2067a4:refs/heads/alpha

Upvotes: 4

Related Questions