Skip to content

Commit b8de3df

Browse files
HaraldNordgrengitster
authored andcommitted
checkout -m: autostash when switching branches
When switching branches with "git checkout -m", the attempted merge of local modifications may cause conflicts with the changes made on the other branch, which the user may not want to (or may not be able to) resolve right now. Because there is no easy way to recover from this situation, we discouraged users from using "checkout -m" unless they are certain their changes are trivial and within their ability to resolve conflicts. Teach the -m flow to create a temporary stash before switching and reapply it after. On success, the stash is silently applied and the list of locally modified paths is shown, same as a successful "git checkout" without "-m". If reapplying causes conflicts, the stash is kept and the user is told they can resolve and run "git stash drop", or run "git reset --hard" and later "git stash pop" to recover their changes. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 68f431b commit b8de3df

File tree

9 files changed

+359
-141
lines changed

9 files changed

+359
-141
lines changed

Documentation/git-checkout.adoc

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -251,20 +251,19 @@ working tree, by copying them from elsewhere, extracting a tarball, etc.
251251
are different between the current branch and the branch to
252252
which you are switching, the command refuses to switch
253253
branches in order to preserve your modifications in context.
254-
However, with this option, a three-way merge between the current
255-
branch, your working tree contents, and the new branch
256-
is done, and you will be on the new branch.
257-
+
258-
When a merge conflict happens, the index entries for conflicting
259-
paths are left unmerged, and you need to resolve the conflicts
260-
and mark the resolved paths with `git add` (or `git rm` if the merge
261-
should result in deletion of the path).
254+
With this option, the conflicting local changes are
255+
automatically stashed before the switch and reapplied
256+
afterwards. If the local changes do not overlap with the
257+
differences between branches, the switch proceeds without
258+
stashing. If reapplying the stash results in conflicts, the
259+
entry is saved to the stash list. Resolve the conflicts
260+
and run `git stash drop` when done, or clear the working
261+
tree (e.g. with `git reset --hard`) before running `git stash
262+
pop` later to re-apply your changes.
262263
+
263264
When checking out paths from the index, this option lets you recreate
264265
the conflicted merge in the specified paths. This option cannot be
265266
used when checking out paths from a tree-ish.
266-
+
267-
When switching branches with `--merge`, staged changes may be lost.
268267
269268
`--conflict=<style>`::
270269
The same as `--merge` option above, but changes the way the
@@ -578,39 +577,44 @@ $ git checkout mytopic
578577
error: You have local changes to 'frotz'; not switching branches.
579578
------------
580579
581-
You can give the `-m` flag to the command, which would try a
582-
three-way merge:
580+
You can give the `-m` flag to the command, which would carry your local
581+
changes to the new branch:
583582
584583
------------
585584
$ git checkout -m mytopic
586-
Auto-merging frotz
585+
Switched to branch 'mytopic'
587586
------------
588587
589-
After this three-way merge, the local modifications are _not_
588+
After the switch, the local modifications are reapplied and are _not_
590589
registered in your index file, so `git diff` would show you what
591590
changes you made since the tip of the new branch.
592591
593592
=== 3. Merge conflict
594593
595-
When a merge conflict happens during switching branches with
596-
the `-m` option, you would see something like this:
594+
When the `--merge` (`-m`) option is in effect and the locally
595+
modified files overlap with files that need to be updated by the
596+
branch switch, the changes are stashed and reapplied after the
597+
switch. If this process results in conflicts, a stash entry is saved
598+
and made available in `git stash list`:
597599
598600
------------
599601
$ git checkout -m mytopic
600-
Auto-merging frotz
601-
ERROR: Merge conflict in frotz
602-
fatal: merge program failed
603-
------------
602+
Your local changes are stashed, however, applying it to carry
603+
forward your local changes resulted in conflicts:
604604
605-
At this point, `git diff` shows the changes cleanly merged as in
606-
the previous example, as well as the changes in the conflicted
607-
files. Edit and resolve the conflict and mark it resolved with
608-
`git add` as usual:
605+
- You can try resolving them now. If you resolved them
606+
successfully, discard the stash entry with "git stash drop".
609607
608+
- Alternatively you can "git reset --hard" if you do not want
609+
to deal with them right now, and later "git stash pop" to
610+
recover your local changes.
610611
------------
611-
$ edit frotz
612-
$ git add frotz
613-
------------
612+
613+
You can try resolving the conflicts now. Edit the conflicting files
614+
and mark them resolved with `git add` as usual, then run `git stash
615+
drop` to discard the stash entry. Alternatively, you can clear the
616+
working tree with `git reset --hard` and recover your local changes
617+
later with `git stash pop`.
614618
615619
CONFIGURATION
616620
-------------

Documentation/git-switch.adoc

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,19 @@ variable.
123123

124124
`-m`::
125125
`--merge`::
126-
If you have local modifications to one or more files that are
127-
different between the current branch and the branch to which
128-
you are switching, the command refuses to switch branches in
129-
order to preserve your modifications in context. However,
130-
with this option, a three-way merge between the current
131-
branch, your working tree contents, and the new branch is
132-
done, and you will be on the new branch.
133-
+
134-
When a merge conflict happens, the index entries for conflicting
135-
paths are left unmerged, and you need to resolve the conflicts
136-
and mark the resolved paths with `git add` (or `git rm` if the merge
137-
should result in deletion of the path).
126+
If you have local modifications to one or more files that
127+
are different between the current branch and the branch to
128+
which you are switching, the command normally refuses to
129+
switch branches in order to preserve your modifications in
130+
context. However, with this option, the conflicting local
131+
changes are automatically stashed before the switch and
132+
reapplied afterwards. If the local changes do not overlap
133+
with the differences between branches, the switch proceeds
134+
without stashing. If reapplying the stash results in
135+
conflicts, the entry is saved to the stash list. Resolve
136+
the conflicts and run `git stash drop` when done, or clear
137+
the working tree (e.g. with `git reset --hard`) before
138+
running `git stash pop` later to re-apply your changes.
138139

139140
`--conflict=<style>`::
140141
The same as `--merge` option above, but changes the way the
@@ -217,15 +218,15 @@ $ git switch mytopic
217218
error: You have local changes to 'frotz'; not switching branches.
218219
------------
219220
220-
You can give the `-m` flag to the command, which would try a three-way
221-
merge:
221+
You can give the `-m` flag to the command, which would carry your local
222+
changes to the new branch:
222223
223224
------------
224225
$ git switch -m mytopic
225-
Auto-merging frotz
226+
Switched to branch 'mytopic'
226227
------------
227228
228-
After this three-way merge, the local modifications are _not_
229+
After the switch, the local modifications are reapplied and are _not_
229230
registered in your index file, so `git diff` would show you what
230231
changes you made since the tip of the new branch.
231232

builtin/checkout.c

Lines changed: 58 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "merge-ll.h"
1818
#include "lockfile.h"
1919
#include "mem-pool.h"
20-
#include "merge-ort-wrappers.h"
2120
#include "object-file.h"
2221
#include "object-name.h"
2322
#include "odb.h"
@@ -30,6 +29,7 @@
3029
#include "repo-settings.h"
3130
#include "resolve-undo.h"
3231
#include "revision.h"
32+
#include "sequencer.h"
3333
#include "setup.h"
3434
#include "submodule.h"
3535
#include "symlinks.h"
@@ -853,90 +853,8 @@ static int merge_working_tree(const struct checkout_opts *opts,
853853
ret = unpack_trees(2, trees, &topts);
854854
clear_unpack_trees_porcelain(&topts);
855855
if (ret == -1) {
856-
/*
857-
* Unpack couldn't do a trivial merge; either
858-
* give up or do a real merge, depending on
859-
* whether the merge flag was used.
860-
*/
861-
struct tree *work;
862-
struct tree *old_tree;
863-
struct merge_options o;
864-
struct strbuf sb = STRBUF_INIT;
865-
struct strbuf old_commit_shortname = STRBUF_INIT;
866-
867-
if (!opts->merge) {
868-
rollback_lock_file(&lock_file);
869-
return 1;
870-
}
871-
872-
/*
873-
* Without old_branch_info->commit, the below is the same as
874-
* the two-tree unpack we already tried and failed.
875-
*/
876-
if (!old_branch_info->commit) {
877-
rollback_lock_file(&lock_file);
878-
return 1;
879-
}
880-
old_tree = repo_get_commit_tree(the_repository,
881-
old_branch_info->commit);
882-
883-
if (repo_index_has_changes(the_repository, old_tree, &sb))
884-
die(_("cannot continue with staged changes in "
885-
"the following files:\n%s"), sb.buf);
886-
strbuf_release(&sb);
887-
888-
/* Do more real merge */
889-
890-
/*
891-
* We update the index fully, then write the
892-
* tree from the index, then merge the new
893-
* branch with the current tree, with the old
894-
* branch as the base. Then we reset the index
895-
* (but not the working tree) to the new
896-
* branch, leaving the working tree as the
897-
* merged version, but skipping unmerged
898-
* entries in the index.
899-
*/
900-
901-
add_files_to_cache(the_repository, NULL, NULL, NULL, 0,
902-
0, 0);
903-
init_ui_merge_options(&o, the_repository);
904-
o.verbosity = 0;
905-
work = write_in_core_index_as_tree(the_repository,
906-
the_repository->index);
907-
908-
ret = reset_tree(new_tree,
909-
opts, 1,
910-
writeout_error, new_branch_info);
911-
if (ret) {
912-
rollback_lock_file(&lock_file);
913-
return ret;
914-
}
915-
o.ancestor = old_branch_info->name;
916-
if (!old_branch_info->name) {
917-
strbuf_add_unique_abbrev(&old_commit_shortname,
918-
&old_branch_info->commit->object.oid,
919-
DEFAULT_ABBREV);
920-
o.ancestor = old_commit_shortname.buf;
921-
}
922-
o.branch1 = new_branch_info->name;
923-
o.branch2 = "local";
924-
o.conflict_style = opts->conflict_style;
925-
ret = merge_ort_nonrecursive(&o,
926-
new_tree,
927-
work,
928-
old_tree);
929-
if (ret < 0)
930-
die(NULL);
931-
ret = reset_tree(new_tree,
932-
opts, 0,
933-
writeout_error, new_branch_info);
934-
strbuf_release(&o.obuf);
935-
strbuf_release(&old_commit_shortname);
936-
if (ret) {
937-
rollback_lock_file(&lock_file);
938-
return ret;
939-
}
856+
rollback_lock_file(&lock_file);
857+
return ret;
940858
}
941859
}
942860

@@ -1181,6 +1099,10 @@ static int switch_branches(const struct checkout_opts *opts,
11811099
struct object_id rev;
11821100
int flag, writeout_error = 0;
11831101
int do_merge = 1;
1102+
int created_autostash = 0;
1103+
struct strbuf old_commit_shortname = STRBUF_INIT;
1104+
struct strbuf autostash_msg = STRBUF_INIT;
1105+
const char *stash_label_base = NULL;
11841106

11851107
trace2_cmd_mode("branch");
11861108

@@ -1218,11 +1140,39 @@ static int switch_branches(const struct checkout_opts *opts,
12181140
do_merge = 0;
12191141
}
12201142

1143+
if (old_branch_info.name)
1144+
stash_label_base = old_branch_info.name;
1145+
else if (old_branch_info.commit) {
1146+
strbuf_add_unique_abbrev(&old_commit_shortname,
1147+
&old_branch_info.commit->object.oid,
1148+
DEFAULT_ABBREV);
1149+
stash_label_base = old_commit_shortname.buf;
1150+
}
1151+
12211152
if (do_merge) {
12221153
ret = merge_working_tree(opts, &old_branch_info, new_branch_info, &writeout_error);
1154+
if (ret == -1 && opts->merge) {
1155+
strbuf_addf(&autostash_msg,
1156+
"autostash while switching to '%s'",
1157+
new_branch_info->name);
1158+
create_autostash_ref(the_repository,
1159+
"CHECKOUT_AUTOSTASH_HEAD",
1160+
autostash_msg.buf, true);
1161+
created_autostash = 1;
1162+
ret = merge_working_tree(opts, &old_branch_info, new_branch_info, &writeout_error);
1163+
}
12231164
if (ret) {
1165+
if (created_autostash)
1166+
apply_autostash_ref(the_repository,
1167+
"CHECKOUT_AUTOSTASH_HEAD",
1168+
new_branch_info->name,
1169+
"local",
1170+
stash_label_base,
1171+
autostash_msg.buf);
12241172
branch_info_release(&old_branch_info);
1225-
return ret;
1173+
strbuf_release(&old_commit_shortname);
1174+
strbuf_release(&autostash_msg);
1175+
return ret < 0 ? 1 : ret;
12261176
}
12271177
}
12281178

@@ -1231,8 +1181,30 @@ static int switch_branches(const struct checkout_opts *opts,
12311181

12321182
update_refs_for_switch(opts, &old_branch_info, new_branch_info);
12331183

1184+
if (opts->conflict_style >= 0) {
1185+
struct strbuf cfg = STRBUF_INIT;
1186+
strbuf_addf(&cfg, "merge.conflictStyle=%s",
1187+
conflict_style_name(opts->conflict_style));
1188+
git_config_push_parameter(cfg.buf);
1189+
strbuf_release(&cfg);
1190+
}
1191+
apply_autostash_ref(the_repository, "CHECKOUT_AUTOSTASH_HEAD",
1192+
new_branch_info->name, "local",
1193+
stash_label_base,
1194+
autostash_msg.buf);
1195+
1196+
discard_index(the_repository->index);
1197+
if (repo_read_index(the_repository) < 0)
1198+
die(_("index file corrupt"));
1199+
1200+
if (created_autostash && !opts->quiet && new_branch_info->commit)
1201+
show_local_changes(&new_branch_info->commit->object,
1202+
&opts->diff_options);
1203+
12341204
ret = post_checkout_hook(old_branch_info.commit, new_branch_info->commit, 1);
12351205
branch_info_release(&old_branch_info);
1206+
strbuf_release(&old_commit_shortname);
1207+
strbuf_release(&autostash_msg);
12361208

12371209
return ret || writeout_error;
12381210
}

sequencer.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4765,15 +4765,23 @@ static int apply_save_autostash_oid(const char *stash_oid, int attempt_apply,
47654765
strvec_push(&store.args, stash_oid);
47664766
if (run_command(&store))
47674767
ret = error(_("cannot store %s"), stash_oid);
4768+
else if (attempt_apply)
4769+
fprintf(stderr,
4770+
_("Your local changes are stashed, however, applying it to carry\n"
4771+
"forward your local changes resulted in conflicts:\n"
4772+
"\n"
4773+
" - You can try resolving them now. If you resolved them\n"
4774+
" successfully, discard the stash entry with \"git stash drop\".\n"
4775+
"\n"
4776+
" - Alternatively you can \"git reset --hard\" if you do not want\n"
4777+
" to deal with them right now, and later \"git stash pop\" to\n"
4778+
" recover your local changes.\n"));
47684779
else
47694780
fprintf(stderr,
4770-
_("%s\n"
4781+
_("Autostash exists; creating a new stash entry.\n"
47714782
"Your changes are safe in the stash.\n"
47724783
"You can run \"git stash pop\" or"
4773-
" \"git stash drop\" at any time.\n"),
4774-
attempt_apply ?
4775-
_("Applying autostash resulted in conflicts.") :
4776-
_("Autostash exists; creating a new stash entry."));
4784+
" \"git stash drop\" at any time.\n"));
47774785
}
47784786

47794787
return ret;

0 commit comments

Comments
 (0)