Skip to content

Commit 3ec37ad

Browse files
pcloudsgitster
authored andcommitted
switch: add --discard-changes
--discard-changes is a better name than --force for this option since it's what really happens. --force is turned to an alias for --discard-changes. But it's meant to be an alias for potentially more force options in the future. Side note. It's not obvious from the patch but --discard-changes also affects submodules if --recurse-submodules is used. The knob to force updating submodules is hidden behind unpack-trees.c Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent b7b5fce commit 3ec37ad

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

builtin/checkout.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ struct checkout_opts {
5353
int count_checkout_paths;
5454
int overlay_mode;
5555
int no_dwim_new_local_branch;
56+
int discard_changes;
5657

5758
/*
5859
* If new checkout options are added, skip_merge_working_tree
@@ -680,7 +681,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
680681
return error(_("index file corrupt"));
681682

682683
resolve_undo_clear();
683-
if (opts->force) {
684+
if (opts->discard_changes) {
684685
ret = reset_tree(get_commit_tree(new_branch_info->commit),
685686
opts, 1, writeout_error);
686687
if (ret)
@@ -802,7 +803,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
802803
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
803804
die(_("unable to write new index file"));
804805

805-
if (!opts->force && !opts->quiet)
806+
if (!opts->discard_changes && !opts->quiet)
806807
show_local_changes(&new_branch_info->commit->object, &opts->diff_options);
807808

808809
return 0;
@@ -1309,6 +1310,9 @@ static int checkout_branch(struct checkout_opts *opts,
13091310
if (opts->force && opts->merge)
13101311
die(_("'%s' cannot be used with '%s'"), "-f", "-m");
13111312

1313+
if (opts->discard_changes && opts->merge)
1314+
die(_("'%s' cannot be used with '%s'"), "--discard-changes", "--merge");
1315+
13121316
if (opts->force_detach && opts->new_branch)
13131317
die(_("'%s' cannot be used with '%s'"),
13141318
"--detach", "-b/-B/--orphan");
@@ -1445,6 +1449,8 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
14451449
opts->merge = 1; /* implied */
14461450
git_xmerge_config("merge.conflictstyle", opts->conflict_style, NULL);
14471451
}
1452+
if (opts->force)
1453+
opts->discard_changes = 1;
14481454

14491455
if ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) > 1)
14501456
die(_("-b, -B and --orphan are mutually exclusive"));
@@ -1600,6 +1606,8 @@ int cmd_switch(int argc, const char **argv, const char *prefix)
16001606
N_("create and switch to a new branch")),
16011607
OPT_STRING('C', "force-create", &opts.new_branch_force, N_("branch"),
16021608
N_("create/reset and switch to a branch")),
1609+
OPT_BOOL(0, "discard-changes", &opts.discard_changes,
1610+
N_("throw away local modifications")),
16031611
OPT_END()
16041612
};
16051613
int ret;

0 commit comments

Comments
 (0)