Skip to content

Commit b84869e

Browse files
carlosmngitster
authored andcommitted
branch: add --unset-upstream option
We have ways of setting the upstream information, but if we want to unset it, we need to resort to modifying the configuration manually. Teach branch an --unset-upstream option that unsets this information. Signed-off-by: Carlos Martín Nieto <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6183d82 commit b84869e

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

Documentation/git-branch.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ SYNOPSIS
1414
[(--merged | --no-merged | --contains) [<commit>]] [<pattern>...]
1515
'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
1616
'git branch' (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
17+
'git branch' --unset-upstream [<branchname>]
1718
'git branch' (-m | -M) [<oldbranch>] <newbranch>
1819
'git branch' (-d | -D) [-r] <branchname>...
1920
'git branch' --edit-description [<branchname>]
@@ -180,6 +181,10 @@ start-point is either a local or remote-tracking branch.
180181
considered <branchname>'s upstream branch. If no <branchname>
181182
is specified, then it defaults to the current branch.
182183

184+
--unset-upstream::
185+
Remove the upstream information for <branchname>. If no branch
186+
is specified it defaults to the current branch.
187+
183188
--edit-description::
184189
Open an editor and edit the text to explain what the branch is
185190
for, to be used by various other commands (e.g. `request-pull`).

builtin/branch.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
712712
int delete = 0, rename = 0, force_create = 0, list = 0;
713713
int verbose = 0, abbrev = -1, detached = 0;
714714
int reflog = 0, edit_description = 0;
715-
int quiet = 0;
715+
int quiet = 0, unset_upstream = 0;
716716
const char *new_upstream = NULL;
717717
enum branch_track track;
718718
int kinds = REF_LOCAL_BRANCH;
@@ -728,6 +728,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
728728
OPT_SET_INT( 0, "set-upstream", &track, "change upstream info",
729729
BRANCH_TRACK_OVERRIDE),
730730
OPT_STRING('u', "set-upstream-to", &new_upstream, "upstream", "change the upstream info"),
731+
OPT_BOOLEAN(0, "unset-upstream", &unset_upstream, "Unset the upstream info"),
731732
OPT__COLOR(&branch_use_color, "use colored output"),
732733
OPT_SET_INT('r', "remotes", &kinds, "act on remote-tracking branches",
733734
REF_REMOTE_BRANCH),
@@ -796,10 +797,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
796797
argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
797798
0);
798799

799-
if (!delete && !rename && !edit_description && !new_upstream && argc == 0)
800+
if (!delete && !rename && !edit_description && !new_upstream && !unset_upstream && argc == 0)
800801
list = 1;
801802

802-
if (!!delete + !!rename + !!force_create + !!list + !!new_upstream > 1)
803+
if (!!delete + !!rename + !!force_create + !!list + !!new_upstream + !!unset_upstream > 1)
803804
usage_with_options(builtin_branch_usage, options);
804805

805806
if (abbrev == -1)
@@ -865,6 +866,20 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
865866
* info and making sure new_upstream is correct
866867
*/
867868
create_branch(head, branch->name, new_upstream, 0, 0, 0, quiet, BRANCH_TRACK_OVERRIDE);
869+
} else if (unset_upstream) {
870+
struct branch *branch = branch_get(argv[0]);
871+
struct strbuf buf = STRBUF_INIT;
872+
873+
if (!branch_has_merge_config(branch)) {
874+
die(_("Branch '%s' has no upstream information"), branch->name);
875+
}
876+
877+
strbuf_addf(&buf, "branch.%s.remote", branch->name);
878+
git_config_set_multivar(buf.buf, NULL, NULL, 1);
879+
strbuf_reset(&buf);
880+
strbuf_addf(&buf, "branch.%s.merge", branch->name);
881+
git_config_set_multivar(buf.buf, NULL, NULL, 1);
882+
strbuf_release(&buf);
868883
} else if (argc > 0 && argc <= 2) {
869884
if (kinds != REF_LOCAL_BRANCH)
870885
die(_("-a and -r options to 'git branch' do not make sense with a branch name"));

t/t3200-branch.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,28 @@ test_expect_success 'use --set-upstream-to modify a particular branch' \
383383
test "$(git config branch.my13.remote)" = "." &&
384384
test "$(git config branch.my13.merge)" = "refs/heads/master"'
385385

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+
386408
# Keep this test last, as it changes the current branch
387409
cat >expect <<EOF
388410
$_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master

0 commit comments

Comments
 (0)