Skip to content

Commit 67d9b39

Browse files
phillipwoodgitster
authored andcommitted
breaking-changes: switch default branch to main
Since 1296cbe (init: document `init.defaultBranch` better, 2020-12-11) "git-init.adoc" has advertised that the default name of the initial branch may change in the future. The name "main" is chosen to match the default used by the big Git forge web sites. The advice printed when init.defaultBranch is not set is updated to say that the default will change to "main" in Git 3.0. Building with WITH_BREAKING_CHANGES enabled removes the advice and changes the default branch name to "main". The code in guess_remote_head() that looks for "refs/heads/master" is left unchanged as that is only called when the remote server does not support the symref capability in the v0 protocol or the symref extension to the ls-refs list in the v2 protocol. Such an old server is more likely to be using "master" as the default branch name. With the exception of the "git-init.adoc" the documentation is left unchanged. I had hoped to parameterize the name of the default branch by using an asciidoc attribute. Unfortunately attribute expansion is inhibited by backticks and we use backticks to mark up ref names so that idea does not work. As the changes to git-init.adoc show inserting ifdef's around each instance of the branch name "master" is cumbersome and makes the documentation sources harder to read. Apart from "git-init.adoc" there are some other files where "master" is used as the name of the initial branch rather than as an example of a branch name such as "user-manual.adoc" and "gitcore-tutorial.adoc". The name appears a lot in those so updating it with ifdef's is not really practical. We can update that document in the 3.0 release cycle. The other documentation where master is used as an example branch name can be gradually converted over time. Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 2462961 commit 67d9b39

File tree

8 files changed

+54
-13
lines changed

8 files changed

+54
-13
lines changed

Documentation/BreakingChanges.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ A prerequisite for this change is that the ecosystem is ready to support the
165165
"reftable" format. Most importantly, alternative implementations of Git like
166166
JGit, libgit2 and Gitoxide need to support it.
167167

168+
* In new repositories, the default branch name will be `main`. We have been
169+
warning that the default name will change since 675704c74dd (init:
170+
provide useful advice about init.defaultBranch, 2020-12-11). The new name
171+
matches the default branch name used in new repositories by many of the
172+
big Git forges.
173+
168174
=== Removals
169175

170176
* Support for grafting commits has long been superseded by git-replace(1).

Documentation/git-init.adoc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,15 @@ If this is a reinitialization, the repository will be moved to the specified pat
7777
`-b <branch-name>`::
7878
`--initial-branch=<branch-name>`::
7979
Use _<branch-name>_ for the initial branch in the newly created
80-
repository. If not specified, fall back to the default name (currently
81-
`master`, but this is subject to change in the future; the name can be
82-
customized via the `init.defaultBranch` configuration variable).
80+
repository. If not specified, fall back to the default name
81+
ifndef::with-breaking-changes[]
82+
(currently `master`, but this will change to `main` when Git 3.0 is released).
83+
endif::with-breaking-changes[]
84+
ifdef::with-breaking-changes[]
85+
`main`.
86+
endif::with-breaking-changes[]
87+
The default name can be customized via the `init.defaultBranch` configuration
88+
variable.
8389

8490
`--shared[=(false|true|umask|group|all|world|everybody|<perm>)]`::
8591

advice.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ static struct {
5151
[ADVICE_AM_WORK_DIR] = { "amWorkDir" },
5252
[ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME] = { "checkoutAmbiguousRemoteBranchName" },
5353
[ADVICE_COMMIT_BEFORE_MERGE] = { "commitBeforeMerge" },
54+
#ifndef WITH_BREAKING_CHANGES
5455
[ADVICE_DEFAULT_BRANCH_NAME] = { "defaultBranchName" },
56+
#endif /* WITH_BREAKING_CHANGES */
5557
[ADVICE_DETACHED_HEAD] = { "detachedHead" },
5658
[ADVICE_DIVERGING] = { "diverging" },
5759
[ADVICE_FETCH_SET_HEAD_WARN] = { "fetchRemoteHEADWarn" },

advice.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ enum advice_type {
1818
ADVICE_AM_WORK_DIR,
1919
ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME,
2020
ADVICE_COMMIT_BEFORE_MERGE,
21+
#ifndef WITH_BREAKING_CHANGES
2122
ADVICE_DEFAULT_BRANCH_NAME,
23+
#endif /* WITH_BREAKING_CHANGES */
2224
ADVICE_DETACHED_HEAD,
2325
ADVICE_DIVERGING,
2426
ADVICE_FETCH_SET_HEAD_WARN,

ci/run-build-and-tests.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ run_tests=t
99

1010
case "$jobname" in
1111
linux-breaking-changes)
12-
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
1312
export WITH_BREAKING_CHANGES=YesPlease
1413
;;
1514
linux-TEST-vars)

refs.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,10 +627,12 @@ void expand_ref_prefix(struct strvec *prefixes, const char *prefix)
627627
strvec_pushf(prefixes, *p, len, prefix);
628628
}
629629

630+
#ifndef WITH_BREAKING_CHANGES
630631
static const char default_branch_name_advice[] = N_(
631632
"Using '%s' as the name for the initial branch. This default branch name\n"
632-
"is subject to change. To configure the initial branch name to use in all\n"
633-
"of your new repositories, which will suppress this warning, call:\n"
633+
"will change to \"main\" in Git 3.0. To configure the initial branch name\n"
634+
"to use in all of your new repositories, which will suppress this warning,\n"
635+
"call:\n"
634636
"\n"
635637
"\tgit config --global init.defaultBranch <name>\n"
636638
"\n"
@@ -639,8 +641,9 @@ static const char default_branch_name_advice[] = N_(
639641
"\n"
640642
"\tgit branch -m <name>\n"
641643
);
644+
#endif /* WITH_BREAKING_CHANGES */
642645

643-
char *repo_default_branch_name(struct repository *r, int quiet)
646+
char *repo_default_branch_name(struct repository *r, MAYBE_UNUSED int quiet)
644647
{
645648
const char *config_key = "init.defaultbranch";
646649
const char *config_display_key = "init.defaultBranch";
@@ -649,14 +652,18 @@ char *repo_default_branch_name(struct repository *r, int quiet)
649652

650653
if (env && *env)
651654
ret = xstrdup(env);
652-
else if (repo_config_get_string(r, config_key, &ret) < 0)
655+
if (!ret && repo_config_get_string(r, config_key, &ret) < 0)
653656
die(_("could not retrieve `%s`"), config_display_key);
654657

655658
if (!ret) {
659+
#ifdef WITH_BREAKING_CHANGES
660+
ret = xstrdup("main");
661+
#else
656662
ret = xstrdup("master");
657663
if (!quiet)
658664
advise_if_enabled(ADVICE_DEFAULT_BRANCH_NAME,
659665
_(default_branch_name_advice), ret);
666+
#endif /* WITH_BREAKING_CHANGES */
660667
}
661668

662669
full_ref = xstrfmt("refs/heads/%s", ret);

t/t0001-init.sh

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ test_expect_success 'overridden default initial branch name (config)' '
868868
grep nmb actual
869869
'
870870

871-
test_expect_success 'advice on unconfigured init.defaultBranch' '
871+
test_expect_success !WITH_BREAKING_CHANGES 'advice on unconfigured init.defaultBranch' '
872872
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= git -c color.advice=always \
873873
init unconfigured-default-branch-name 2>err &&
874874
test_decode_color <err >decoded &&
@@ -883,6 +883,22 @@ test_expect_success 'advice on unconfigured init.defaultBranch disabled' '
883883
test_grep ! "hint: " err
884884
'
885885

886+
test_expect_success 'default branch name' '
887+
if test_have_prereq WITH_BREAKING_CHANGES
888+
then
889+
expect=main
890+
else
891+
expect=master
892+
fi &&
893+
echo "refs/heads/$expect" >expect &&
894+
(
895+
sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
896+
git init default-initial-branch-name
897+
) &&
898+
git -C default-initial-branch-name symbolic-ref HEAD >actual &&
899+
test_cmp expect actual
900+
'
901+
886902
test_expect_success 'overridden default main branch name (env)' '
887903
test_config_global init.defaultBranch nmb &&
888904
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=env git init main-branch-env &&

t/test-lib.sh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,13 @@ then
127127
export GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS
128128
fi
129129

130-
# Explicitly set the default branch name for testing, to avoid the
131-
# transitory "git init" warning under --verbose.
132-
: ${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME:=master}
133-
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
130+
if test -z "$WITH_BREAKING_CHANGES"
131+
then
132+
# Explicitly set the default branch name for testing, to avoid the
133+
# transitory "git init" warning under --verbose.
134+
: ${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME:=master}
135+
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
136+
fi
134137

135138
################################################################
136139
# It appears that people try to run tests without building...

0 commit comments

Comments
 (0)