Skip to content

Commit 6183d82

Browse files
carlosmngitster
authored andcommitted
branch: introduce --set-upstream-to
The existing --set-uptream option can cause confusion, as it uses the usual branch convention of assuming a starting point of HEAD if none is specified, causing git branch --set-upstream origin/master to create a new local branch 'origin/master' that tracks the current branch. As --set-upstream already exists, we can't simply change its behaviour. To work around this, introduce --set-upstream-to which accepts a compulsory argument indicating what the new upstream branch should be and one optinal argument indicating which branch to change, defaulting to HEAD. The new options allows us to type git branch --set-upstream-to origin/master to set the current branch's upstream to be origin's master. Signed-off-by: Carlos Martín Nieto <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 889d358 commit 6183d82

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

Documentation/git-branch.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ 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>]
1617
'git branch' (-m | -M) [<oldbranch>] <newbranch>
1718
'git branch' (-d | -D) [-r] <branchname>...
1819
'git branch' --edit-description [<branchname>]
@@ -48,7 +49,7 @@ branch so that 'git pull' will appropriately merge from
4849
the remote-tracking branch. This behavior may be changed via the global
4950
`branch.autosetupmerge` configuration flag. That setting can be
5051
overridden by using the `--track` and `--no-track` options, and
51-
changed later using `git branch --set-upstream`.
52+
changed later using `git branch --set-upstream-to`.
5253

5354
With a `-m` or `-M` option, <oldbranch> will be renamed to <newbranch>.
5455
If <oldbranch> had a corresponding reflog, it is renamed to match
@@ -173,6 +174,12 @@ start-point is either a local or remote-tracking branch.
173174
like `--track` would when creating the branch, except that where
174175
branch points to is not changed.
175176

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

builtin/branch.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
713713
int verbose = 0, abbrev = -1, detached = 0;
714714
int reflog = 0, edit_description = 0;
715715
int quiet = 0;
716+
const char *new_upstream = NULL;
716717
enum branch_track track;
717718
int kinds = REF_LOCAL_BRANCH;
718719
struct commit_list *with_commit = NULL;
@@ -726,6 +727,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
726727
BRANCH_TRACK_EXPLICIT),
727728
OPT_SET_INT( 0, "set-upstream", &track, "change upstream info",
728729
BRANCH_TRACK_OVERRIDE),
730+
OPT_STRING('u', "set-upstream-to", &new_upstream, "upstream", "change the upstream info"),
729731
OPT__COLOR(&branch_use_color, "use colored output"),
730732
OPT_SET_INT('r', "remotes", &kinds, "act on remote-tracking branches",
731733
REF_REMOTE_BRANCH),
@@ -794,10 +796,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
794796
argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
795797
0);
796798

797-
if (!delete && !rename && !edit_description && argc == 0)
799+
if (!delete && !rename && !edit_description && !new_upstream && argc == 0)
798800
list = 1;
799801

800-
if (!!delete + !!rename + !!force_create + !!list > 1)
802+
if (!!delete + !!rename + !!force_create + !!list + !!new_upstream > 1)
801803
usage_with_options(builtin_branch_usage, options);
802804

803805
if (abbrev == -1)
@@ -852,6 +854,17 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
852854
rename_branch(argv[0], argv[1], rename > 1);
853855
else
854856
usage_with_options(builtin_branch_usage, options);
857+
} else if (new_upstream) {
858+
struct branch *branch = branch_get(argv[0]);
859+
860+
if (!ref_exists(branch->refname))
861+
die(_("branch '%s' does not exist"), branch->name);
862+
863+
/*
864+
* create_branch takes care of setting up the tracking
865+
* info and making sure new_upstream is correct
866+
*/
867+
create_branch(head, branch->name, new_upstream, 0, 0, 0, quiet, BRANCH_TRACK_OVERRIDE);
855868
} else if (argc > 0 && argc <= 2) {
856869
if (kinds != REF_LOCAL_BRANCH)
857870
die(_("-a and -r options to 'git branch' do not make sense with a branch name"));

t/t3200-branch.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,20 @@ 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+
372386
# Keep this test last, as it changes the current branch
373387
cat >expect <<EOF
374388
$_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master

0 commit comments

Comments
 (0)