'Git checkout remote branch deleted locally

In one of my systems while working on a project, I made changes and pushed it. Now I have a new system with all the same ssh/user details, I cloned that project and wanted to continue my unfinished change by checking out my branch. Here I did one mistake: I made checkout with -b option i.e.

git checkout -b mybranch
//instead of 
git checkout mybranch

So a blank branch was presented to me instead of cloned remote branch.

So I deleted locally created branch using:

git branch -d mybranch

I tried below things(by googling) without any success:

git checkout --track origin/mybranch
git pull origin origin/mybranch

How can I pull and checkout to mybranch?



Solution 1:[1]

Steps:

  1. Asked a colleague to switch to myBranch.
  2. Replace contents of my project/.git/config file to his contents for the same file.
  3. Checkout myBranch. Works!

This trick worked for me when just commands didn't helped. ?

Solution 2:[2]

If there's no work laying around there, make the branch look like it was started from the remote branch.

# dangerous, that's why i said _if_ there's no work laying around
git reset --hard origin/mybranch # place the local branch where the remote branch is (local branch and worktree content)
git branch --set-upstream origin/mybranch

That should be good enough.

But given that you deleted the local branch already

git checkout mybranch

Should be good enough for git to create the local branch from the remote

Solution 3:[3]

Once deleted locally, you cannot recover it but you can recreate it with an other name with the following command :

git checkout -b name_of_the_branch origin/name_of_the_branch_on_github

git checkout -b name_of_the_branch allows you to create a branch starting from the one where you are, if you add an argument, the new branch will start from this argument origin/name_of_the_branch_on_github in this example.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nanosoft
Solution 2 eftshift0
Solution 3 Nico v