Skip to content

Commit 0cc1b47

Browse files
dschogitster
authored andcommitted
clone: use configured default branch name when appropriate
When cloning a repository without any branches, Git chooses a default branch name for the as-yet unborn branch. As part of the implicit initialization of the local repository, Git just learned to respect `init.defaultBranch` to choose a different initial branch name. We now really want that branch name to be used as a fall-back. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8747ebb commit 0cc1b47

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

Documentation/config/init.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ init.templateDir::
33
(See the "TEMPLATE DIRECTORY" section of linkgit:git-init[1].)
44

55
init.defaultBranch::
6-
Allows overriding the default branch name when initializing
7-
a new repository.
6+
Allows overriding the default branch name e.g. when initializing
7+
a new repository or when cloning an empty repository.

builtin/clone.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,9 +1264,13 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
12641264
remote_head_points_at = NULL;
12651265
remote_head = NULL;
12661266
option_no_checkout = 1;
1267-
if (!option_bare)
1268-
install_branch_config(0, "master", option_origin,
1269-
"refs/heads/master");
1267+
if (!option_bare) {
1268+
const char *branch = git_default_branch_name();
1269+
char *ref = xstrfmt("refs/heads/%s", branch);
1270+
1271+
install_branch_config(0, branch, option_origin, ref);
1272+
free(ref);
1273+
}
12701274
}
12711275

12721276
write_refspec_config(src_ref_prefix, our_head_points_at,

t/t5606-clone-options.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,19 @@ test_expect_success 'redirected clone -v does show progress' '
3535
3636
'
3737

38+
test_expect_success 'chooses correct default initial branch name' '
39+
git init --bare empty &&
40+
git -c init.defaultBranch=up clone empty whats-up &&
41+
test refs/heads/up = $(git -C whats-up symbolic-ref HEAD) &&
42+
test refs/heads/up = $(git -C whats-up config branch.up.merge)
43+
'
44+
45+
test_expect_success 'guesses initial branch name correctly' '
46+
git init --initial-branch=guess initial-branch &&
47+
test_commit -C initial-branch no-spoilers &&
48+
git -C initial-branch branch abc guess &&
49+
git clone initial-branch is-it &&
50+
test refs/heads/guess = $(git -C is-it symbolic-ref HEAD)
51+
'
52+
3853
test_done

0 commit comments

Comments
 (0)