Skip to content

Commit 83ce176

Browse files
committed
Merge branch 'cn/branch-set-upstream-to'
"git branch --set-upstream origin/master" is a common mistake to create a local branch 'origin/master' and set it to integrate with the current branch. With a plan to deprecate this option, introduce "git branch (-u|--set-upstream-to) origin/master" that sets the current branch to integrate with 'origin/master' remote tracking branch. * cn/branch-set-upstream-to: branch: deprecate --set-upstream and show help if we detect possible mistaken use branch: add --unset-upstream option branch: introduce --set-upstream-to
2 parents c2b9279 + b347d06 commit 83ce176

File tree

3 files changed

+140
-4
lines changed

3 files changed

+140
-4
lines changed

Documentation/git-branch.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ SYNOPSIS
1313
[--column[=<options>] | --no-column]
1414
[(--merged | --no-merged | --contains) [<commit>]] [<pattern>...]
1515
'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
16+
'git branch' (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
17+
'git branch' --unset-upstream [<branchname>]
1618
'git branch' (-m | -M) [<oldbranch>] <newbranch>
1719
'git branch' (-d | -D) [-r] <branchname>...
1820
'git branch' --edit-description [<branchname>]
@@ -48,7 +50,7 @@ branch so that 'git pull' will appropriately merge from
4850
the remote-tracking branch. This behavior may be changed via the global
4951
`branch.autosetupmerge` configuration flag. That setting can be
5052
overridden by using the `--track` and `--no-track` options, and
51-
changed later using `git branch --set-upstream`.
53+
changed later using `git branch --set-upstream-to`.
5254

5355
With a `-m` or `-M` option, <oldbranch> will be renamed to <newbranch>.
5456
If <oldbranch> had a corresponding reflog, it is renamed to match
@@ -173,6 +175,16 @@ start-point is either a local or remote-tracking branch.
173175
like `--track` would when creating the branch, except that where
174176
branch points to is not changed.
175177

178+
-u <upstream>::
179+
--set-upstream-to=<upstream>::
180+
Set up <branchname>'s tracking information so <upstream> is
181+
considered <branchname>'s upstream branch. If no <branchname>
182+
is specified, then it defaults to the current branch.
183+
184+
--unset-upstream::
185+
Remove the upstream information for <branchname>. If no branch
186+
is specified it defaults to the current branch.
187+
176188
--edit-description::
177189
Open an editor and edit the text to explain what the branch is
178190
for, to be used by various other commands (e.g. `request-pull`).

builtin/branch.c

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
714714
int delete = 0, rename = 0, force_create = 0, list = 0;
715715
int verbose = 0, abbrev = -1, detached = 0;
716716
int reflog = 0, edit_description = 0;
717-
int quiet = 0;
717+
int quiet = 0, unset_upstream = 0;
718+
const char *new_upstream = NULL;
718719
enum branch_track track;
719720
int kinds = REF_LOCAL_BRANCH;
720721
struct commit_list *with_commit = NULL;
@@ -728,6 +729,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
728729
BRANCH_TRACK_EXPLICIT),
729730
OPT_SET_INT( 0, "set-upstream", &track, N_("change upstream info"),
730731
BRANCH_TRACK_OVERRIDE),
732+
OPT_STRING('u', "set-upstream-to", &new_upstream, "upstream", "change the upstream info"),
733+
OPT_BOOLEAN(0, "unset-upstream", &unset_upstream, "Unset the upstream info"),
731734
OPT__COLOR(&branch_use_color, N_("use colored output")),
732735
OPT_SET_INT('r', "remotes", &kinds, N_("act on remote-tracking branches"),
733736
REF_REMOTE_BRANCH),
@@ -796,10 +799,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
796799
argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
797800
0);
798801

799-
if (!delete && !rename && !edit_description && argc == 0)
802+
if (!delete && !rename && !edit_description && !new_upstream && !unset_upstream && argc == 0)
800803
list = 1;
801804

802-
if (!!delete + !!rename + !!force_create + !!list > 1)
805+
if (!!delete + !!rename + !!force_create + !!list + !!new_upstream + !!unset_upstream > 1)
803806
usage_with_options(builtin_branch_usage, options);
804807

805808
if (abbrev == -1)
@@ -854,11 +857,62 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
854857
rename_branch(argv[0], argv[1], rename > 1);
855858
else
856859
usage_with_options(builtin_branch_usage, options);
860+
} else if (new_upstream) {
861+
struct branch *branch = branch_get(argv[0]);
862+
863+
if (!ref_exists(branch->refname))
864+
die(_("branch '%s' does not exist"), branch->name);
865+
866+
/*
867+
* create_branch takes care of setting up the tracking
868+
* info and making sure new_upstream is correct
869+
*/
870+
create_branch(head, branch->name, new_upstream, 0, 0, 0, quiet, BRANCH_TRACK_OVERRIDE);
871+
} else if (unset_upstream) {
872+
struct branch *branch = branch_get(argv[0]);
873+
struct strbuf buf = STRBUF_INIT;
874+
875+
if (!branch_has_merge_config(branch)) {
876+
die(_("Branch '%s' has no upstream information"), branch->name);
877+
}
878+
879+
strbuf_addf(&buf, "branch.%s.remote", branch->name);
880+
git_config_set_multivar(buf.buf, NULL, NULL, 1);
881+
strbuf_reset(&buf);
882+
strbuf_addf(&buf, "branch.%s.merge", branch->name);
883+
git_config_set_multivar(buf.buf, NULL, NULL, 1);
884+
strbuf_release(&buf);
857885
} else if (argc > 0 && argc <= 2) {
886+
struct branch *branch = branch_get(argv[0]);
887+
int branch_existed = 0, remote_tracking = 0;
888+
struct strbuf buf = STRBUF_INIT;
889+
858890
if (kinds != REF_LOCAL_BRANCH)
859891
die(_("-a and -r options to 'git branch' do not make sense with a branch name"));
892+
893+
if (track == BRANCH_TRACK_OVERRIDE)
894+
fprintf(stderr, _("The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to\n"));
895+
896+
strbuf_addf(&buf, "refs/remotes/%s", branch->name);
897+
remote_tracking = ref_exists(buf.buf);
898+
strbuf_release(&buf);
899+
900+
branch_existed = ref_exists(branch->refname);
860901
create_branch(head, argv[0], (argc == 2) ? argv[1] : head,
861902
force_create, reflog, 0, quiet, track);
903+
904+
/*
905+
* We only show the instructions if the user gave us
906+
* one branch which doesn't exist locally, but is the
907+
* name of a remote-tracking branch.
908+
*/
909+
if (argc == 1 && track == BRANCH_TRACK_OVERRIDE &&
910+
!branch_existed && remote_tracking) {
911+
fprintf(stderr, _("\nIf you wanted to make '%s' track '%s', do this:\n\n"), head, branch->name);
912+
fprintf(stderr, _(" git branch -d %s\n"), branch->name);
913+
fprintf(stderr, _(" git branch --set-upstream-to %s\n"), branch->name);
914+
}
915+
862916
} else
863917
usage_with_options(builtin_branch_usage, options);
864918

t/t3200-branch.sh

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,76 @@ test_expect_success \
369369
'git tag foobar &&
370370
test_must_fail git branch --track my11 foobar'
371371

372+
test_expect_success 'use --set-upstream-to modify HEAD' \
373+
'test_config branch.master.remote foo &&
374+
test_config branch.master.merge foo &&
375+
git branch my12
376+
git branch --set-upstream-to my12 &&
377+
test "$(git config branch.master.remote)" = "." &&
378+
test "$(git config branch.master.merge)" = "refs/heads/my12"'
379+
380+
test_expect_success 'use --set-upstream-to modify a particular branch' \
381+
'git branch my13
382+
git branch --set-upstream-to master my13 &&
383+
test "$(git config branch.my13.remote)" = "." &&
384+
test "$(git config branch.my13.merge)" = "refs/heads/master"'
385+
386+
test_expect_success '--unset-upstream should fail if given a non-existent branch' \
387+
'test_must_fail git branch --unset-upstream i-dont-exist'
388+
389+
test_expect_success 'test --unset-upstream on HEAD' \
390+
'git branch my14
391+
test_config branch.master.remote foo &&
392+
test_config branch.master.merge foo &&
393+
git branch --set-upstream-to my14 &&
394+
git branch --unset-upstream &&
395+
test_must_fail git config branch.master.remote &&
396+
test_must_fail git config branch.master.merge &&
397+
# fail for a branch without upstream set
398+
test_must_fail git branch --unset-upstream
399+
'
400+
401+
test_expect_success 'test --unset-upstream on a particular branch' \
402+
'git branch my15
403+
git branch --set-upstream-to master my14 &&
404+
git branch --unset-upstream my14 &&
405+
test_must_fail git config branch.my14.remote &&
406+
test_must_fail git config branch.my14.merge'
407+
408+
test_expect_success '--set-upstream shows message when creating a new branch that exists as remote-tracking' \
409+
'git update-ref refs/remotes/origin/master HEAD &&
410+
git branch --set-upstream origin/master 2>actual &&
411+
test_when_finished git update-ref -d refs/remotes/origin/master &&
412+
test_when_finished git branch -d origin/master &&
413+
cat >expected <<EOF &&
414+
The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
415+
416+
If you wanted to make '"'master'"' track '"'origin/master'"', do this:
417+
418+
git branch -d origin/master
419+
git branch --set-upstream-to origin/master
420+
EOF
421+
test_cmp expected actual
422+
'
423+
424+
test_expect_success '--set-upstream with two args only shows the deprecation message' \
425+
'git branch --set-upstream master my13 2>actual &&
426+
test_when_finished git branch --unset-upstream master &&
427+
cat >expected <<EOF &&
428+
The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
429+
EOF
430+
test_cmp expected actual
431+
'
432+
433+
test_expect_success '--set-upstream with one arg only shows the deprecation message if the branch existed' \
434+
'git branch --set-upstream my13 2>actual &&
435+
test_when_finished git branch --unset-upstream my13 &&
436+
cat >expected <<EOF &&
437+
The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
438+
EOF
439+
test_cmp expected actual
440+
'
441+
372442
# Keep this test last, as it changes the current branch
373443
cat >expect <<EOF
374444
$_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master

0 commit comments

Comments
 (0)