How can I clone a branch from git?
I know it can be a silly thing to ask but I am pretty sure all of us has been there, where we needed to ask a silly question.
Now the scenario is - I have a branch named branch-99
and my user name is Christoffer. I try to clone my branch like so:
git clone -b branch-99 [email protected]/admin.git
I am receiving this error.
fatal: Remote branch branch-99 not found in upstream origin
What I am doing wrong and How can I clone my branch?
branch-99
does not exist on the remote repository. You probably misspelled the branch name or the repository.
To check which branches exist, clone the repository normally and list the remote branches.
git clone [email protected]:Christoffer/admin.git
git branch -r
Alternatively, to avoid having to clone the whole repository just to check for the branches you can use ls-remotes, but you have to init the repository and add the remote manually. Only do this if the repository is huge and you don't intent to use it.
git init admin
cd admin
git remote add origin [email protected]:Christoffer/admin.git
git ls-remote --heads origin
To be clear, git clone -b branch-99 [email protected]:Christoffer/admin.git
clones the entire repository. It just checks out branch-99
rather than master
. It's the same as...
git clone [email protected]:Christoffer/admin.git
git checkout branch-99
That bit of syntax sugar isn't worth the bother. I guess it might save you a tiny bit of time not having to checkout master.
If you want to clone just the history of one branch to save some network and disk space use --single-branch
.
git clone --single-branch -b branch-99 [email protected]:Christoffer/admin.git
However it's usually not worth the trouble. Git is very disk and network efficient. And the whole history of that branch has to be cloned which usually includes most of the repository anyway.