1

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.

4
  • You have either to run git checkout -b in the first place, or name branch with git branch. Commented Jan 18, 2017 at 20:05
  • I have already named it but after naming it... if I do git pull I am getting below error. Commented Jan 18, 2017 at 20:06
  • Yes, you have to link your local branch to remote one. last line in the cited text shows what you need. Commented Jan 18, 2017 at 20:08
  • the ls-remote command actually goes to remote and checks there to discover what you already know, i.e. that it's gone from there. However You already have locally the 'remote tracking branch' origin/alpha, so simply check that out checkout -b NewAlpha origin/alpha and now you have it 'front of house' and can push it to the remote replace the missing 'alpha'. Commented Jan 18, 2017 at 23:41

1 Answer 1

4

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 
Sign up to request clarification or add additional context in comments.

1 Comment

Note that if your own origin/alpha resolves to 45430f8..., the two commands will do the same thing. Aside from a bunch of special case magic (looking up whether a name is a branch or a tag automatically), git push <local-name>:<remote-name> and git push <id>:<remote-name> mean the same thing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.