So, we frequently optimize clones by effectively cloning with --single-branch. However, we are then unable to get additional branches later. What is the difference, plumbing-wise, between a git clone with and without --single-branch? How can we fetch down additional branches later?
A standard clone:
$ git clone -b branch-name https://repo.url standard
$ cd standard
$ git checkout remote-branch
Branch 'remote-branch' set up to track remote branch 'remote-branch' from 'origin'.
Switched to a new branch 'remote-branch'
A single-branch clone:
$ git clone -b branch-name --single-branch https://repo.url singlebranch
$ cd singlebranch
$ git checkout remote-branch
error: pathspec 'remote-branch' did not match any file(s) known to git
UPDATE
Per the answer from @AndrewMarshall, below, you need to update the default fetch refspec in the config. Even though you can hack your way around the fetch to pull down the right commits, your attempted checkout will absolutely deny knowing anything about that branch if you don't fix your config first:
$ git fetch origin +refs/heads/remote-branch:refs/remotes/origin/remote-branch
From https://gerrit.magicleap.com/a/platform/mlmanifest
* [new branch] remote-branch -> origin/remote-branch
$ git checkout remote-branch
error: pathspec 'remote-branch' did not match any file(s) known to git
$ git remote set-branches origin --add remote-branch
$ git checkout remote-branch
Branch 'remote-branch' set up to track remote branch 'remote-branch' from 'origin'.
Switched to a new branch 'remote-branch'
Notice we fetch it, then reconfigure, then checkout. The fetch can happen in any order (though you have to pass parameters if not in the config) but the checkout is gated by the config.
Deleting a branch REMOTELY Here's the command to delete a branch remotely: git push <remote> --delete <branch> . The branch is now deleted remotely. If you get the error below, it may mean that someone else has already deleted the branch.
To see local branches, use the git branch command. The git branch command lets you see a list of all the branches stored in your local version of a repository. To see the remote branches associated with your repository, you need to append the -r flag to the end of the git branch command.
You can fetch a specific branch from remote with git fetch <remote_name> <branch_name> only if the branch is already on the tracking branch list (you can check it with git branch -r ).
The prune option removes any remote tracking branch in your local repository that points to a remote branch that has been deleted on the server. With the local branch deleted, the remote tracking branch deleted, and all instances of the remote git branch deleted as well, the remote Git branch should be gone forever.
--single-branch
works by setting the remote’s fetch
property to only be the single branch name, instead of a glob:
$ git config --get-all remote.origin.fetch
+refs/heads/master:refs/remotes/origin/master
So let’s add an entry with git remote set-branches
:
$ git remote set-branches origin --add other-branch
$ git config --get-all remote.origin.fetch
+refs/heads/master:refs/remotes/origin/master
+refs/heads/other-branch:refs/remotes/origin/other-branch
$ git fetch
From origin
* [new branch] other-branch -> origin/other-branch
$ git checkout other-branch
Branch 'other-branch' set up to track remote branch 'other-branch' from 'origin'.
Switched to a new branch 'other-branch'
Or, alternatively, make it a glob so all branches may be fetched (the default, non-single-branch behavior) (note that the *
is quoted to avoid shell expansion; the glob is for git
, not the shell):
git remote set-branches origin '*'
Very short summary:
if you have used "single-branch"
you must do these TWO things
$ git remote set-branches origin --add other-branch
$ git fetch
$ git checkout other-branch
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With