Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking out a remote git branch that does not exist locally?

Tags:

git

branch

Let's say there is a remote clone of the same git repository that my local repository is cloned from - and there is a branch on that remote clone that isn't present in my clone (not the default branch).

Is there a way to clone that remote branch into my local copy? I don't want to merge it with the current branch or anything like that. I just want to start a local copy of the remote branch. Is there a way to do this?

I would also like to know (once this is done) how to add the mentioned branch also to the default remote-copy that my local clone checks in with by default.

like image 852
Sophia_ES Avatar asked Nov 17 '14 23:11

Sophia_ES


2 Answers

Just use next:

git fetch <remote repo name>
git checkout -b <local branch name> <remote repo name>/<remote branch name>

It was helpful in my case and this procedure is identical to automatically generated action through TortoiseGit on my Windows machine.

like image 122
Paul Basenko Avatar answered Sep 22 '22 02:09

Paul Basenko


One command will do this work:

git checkout -b <local_branch_name> origin/<remote_branch_name>

Edit: If an error occurred: fatal: 'origin/<branch_name>' is not a commit and a branch '<branch_name>' cannot be created from it.

You may try:

git pull
git checkout -b <local_branch_name> origin/<remote_branch_name>
like image 29
zhulj Avatar answered Sep 22 '22 02:09

zhulj