Reputation: 1350
I am trying to rename my master
branch to development
:
git checkout -b development master
git push -u origin development
git branch -d master
git push --delete origin master
git remote prune origin
It works fine. However, if I try to clone my repository, I will land on an empty folder and I need to do git checkout development
.
How do I set development
to be my default branch when I clone?
Upvotes: 1
Views: 44
Reputation: 489233
To add on to Schleis' answer, when you run git clone
with -b <branchname>
you are telling your Git what to do in its last step. Remember that git clone url dest
is effectively shorthand for this six-step command sequence:
mkdir dest
, to create a new empty directory. If you omit the dest
argument, Git will construct one based on the url
you supply. (If you name an existing directory, it must be empty.) The remaining operations will run in this directory as if via cd dest
, although this does not affect your shell.
git init
: this creates a new, empty repository—the .git
directory—in the otherwise-empty directory.
git remote add origin url
: this adds a remote, named origin
, to the configuration of this new empty repository. (You can choose a name other than origin
if you like, via the -o
option to git clone
.) If you used --single-branch
, the single branch is effectively passed through at this point. (I say "effectively" only because this part of the process is built in to the git clone
command itself.)
Set up any additional git config
commands you specify in the git clone
command (with -c
options added after the word clone
: -c
options before the word clone
may apply to the cloning process, but are not set in the clone).
Run git fetch origin
(or whatever remote name you chose). This obtains whatever commits are published at the URL. It also gets a list of branch and tag names (and other references) from the other Git; those branch names become your remote-tracking names, and those tag names are generally copied to your repository as well, at this point.1
Finally, your Git picks some branch name to, in effect, pass to git checkout
. Which branch name is your Git going to git checkout
? The answer depends on whether you provide a -b
flag.
If you provide -b foo
, your Git will try to git checkout foo
. This depends on the existence of origin/foo
, created in step 5 from their Git's foo
. If you provide -b master
, your Git will try to git checkout master
. But if you don't provide any -b
, your Git asks their Git which branch they recommend.
What's happening in this case is that their Git is recommending master
, or not recommending anything. We can't tell which of these is the case from what you've posted—but it doesn't matter, because they don't have a master
. If they recommend that your Git use their master
to make your master
, this will fail: they don't have a master
. If they make no recommendation at all, your Git will assume, as a last ditch thing, that it should use master
, and will look for your origin/master
as copied from their master
, but you won't have it.
So, no matter what happens at this point, your Git has no branch to check out. You do have origin/development
, though, as copied from their development
. So you can run git checkout development
, which looks around, sees that you don't have a development
, looks around some more, sees that you do have origin/development
, and does what you wanted.
How do I set development to be my default branch when I clone?
You can try to get their Git to recommend that name.
To do that in a traditional (non-GitHub-ish) way, you log in to the server, navigate to the --bare
repository (wherever it is), and run:
git symbolic-ref HEAD refs/heads/development
and now their Git will recommend development
.
To do that with GitHub, see GitHub's instructions. To do that with some other web hosting service, see their instructions, wherever they may be.
1A branch or tag name can only exist if the object ID recorded in that branch or tag name exists too. Hence, if you limited the set of commits that get copied, your Git may have to omit some names. For instance, if one of their tags, such as v0.1
, points to a commit that you deliberately omitted via --single-branch
and/or --shallow
, you can't have that tag. If their branch debug
points to some commit that you didn't take because you said --single-branch foo
, you can't have origin/debug
. Note that --shallow
implies --single-branch
.
Upvotes: 1
Reputation: 43750
By default git will want to use master
and most repos would be using this branch as the "stable". So in general you would want to have this behavior.
However in your git clone command there is an option -b <branchname>
that will checkout a particular branch or just clone the named branch.
https://git-scm.com/docs/git-clone
So in your case, you would want to change your clone command to git clone -b development <repo>
Upvotes: 0