Subject: Re: [PATCH v16 5/5] checkout -m: autostash when switching branches

From: Phillip Wood <[email-masked]>
Date: Wed, 29 Apr 2026 11:02:17 +0100

Hi Harald

On 28/04/2026 19:39, Harald Nordgren via GitGitGadget wrote:
> From: Harald Nordgren <[email-masked]>
> 
>   	if (do_merge) {
> -		ret = merge_working_tree(opts, &old_branch_info, new_branch_info, &writeout_error);
> +		ret = merge_working_tree(opts, &old_branch_info, new_branch_info,
> +					 opts->merge, &writeout_error);
> +		if (ret == MERGE_WORKING_TREE_UNPACK_FAILED && opts->merge) {
> +			strbuf_addf(&autostash_msg,
> +				    "autostash while switching to '%s'",
> +				    new_branch_info->name);
> +			create_autostash_ref(the_repository,
> +					     "CHECKOUT_AUTOSTASH_HEAD",
> +					     autostash_msg.buf, true);

If there are no local changes then doing this is pointless - it means 
unpack_trees() failed for another reason. Having said that the current 
code also tries a 3-way merge unconditionally so I think we can happily 
leave this for the future as #leftoverbits

> +			created_autostash = 1;
> +			ret = merge_working_tree(opts, &old_branch_info, new_branch_info,
> +						 false, &writeout_error);
> +		}
> +		if (created_autostash) {
> +			if (opts->conflict_style >= 0) {
> +				struct strbuf cfg = STRBUF_INIT;
> +				strbuf_addf(&cfg, "merge.conflictStyle=%s",
> +					    conflict_style_name(opts->conflict_style));
> +				git_config_push_parameter(cfg.buf);
> +				strbuf_release(&cfg);
> +			}
> +			apply_autostash_ref(the_repository,
> +					    "CHECKOUT_AUTOSTASH_HEAD",
> +					    new_branch_info->name,
> +					    "local",
> +					    stash_label_base,
> +					    autostash_msg.buf);
> +		}
>   		if (ret) {
>   			branch_info_release(&old_branch_info);
> -			return ret;
> +			strbuf_release(&old_commit_shortname);
> +			strbuf_release(&autostash_msg);
> +			return ret < 0 ? 1 : ret;
>   		}
>   	}

If popping the stash created merge conflicts then it would be nice to 
print a blank line before the message about which branch we've switched 
to so that it is visually separated from the conflicts advice. That 
would mean apply_autostash_ref() would have to tell us if there we're 
conflicts. Again we can happily leave that for the future as #leftoverbits

Thanks

Phillip