nanosoft
nanosoft

Reputation: 3091

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?

Upvotes: 0

Views: 952

Answers (3)

Nico v
Nico v

Reputation: 17

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.

Upvotes: 1

nanosoft
nanosoft

Reputation: 3091

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. šŸ™

Upvotes: 0

eftshift0
eftshift0

Reputation: 30156

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

Upvotes: 1

Related Questions