From: Phillip Wood Date: Tue, 28 Apr 2026 09:35:29 GMT Subject: Re: [PATCH v15 5/5] checkout -m: autostash when switching branches Message-ID: <88a89e06-5223-4a6f-8f9e-66e72b632ee2@gmail.com> In-Reply-To: <96b14db827e51966aceb6513f7d16ed526274ec2.1777065012.git.gitgitgadget@gmail.com> Hi Harald On 24/04/2026 22:10, Harald Nordgren via GitGitGadget wrote: > From: Harald Nordgren > > 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. This is looking good, there are just a few small issues. Hopefully the next iteration will be the last. > Signed-off-by: Harald Nordgren > --- > Documentation/git-checkout.adoc | 58 ++++++------ > Documentation/git-switch.adoc | 33 +++---- > builtin/checkout.c | 160 ++++++++++++++------------------ > sequencer.c | 14 ++- > t/t3420-rebase-autostash.sh | 16 ++-- > t/t7201-co.sh | 61 +++++++++++- > t/t7600-merge.sh | 3 +- > xdiff-interface.c | 12 +++ > xdiff-interface.h | 1 + > 9 files changed, 211 insertions(+), 147 deletions(-) > > diff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc > index 43ccf47cf6..70dd211ee3 100644 > --- a/Documentation/git-checkout.adoc > +++ b/Documentation/git-checkout.adoc > @@ -251,20 +251,19 @@ working tree, by copying them from elsewhere, extracting a tarball, etc. > are different between the current branch and the branch to > which you are switching, the command refuses to switch > branches in order to preserve your modifications in context. > - However, with this option, a three-way merge between the current > - branch, your working tree contents, and the new branch > - is done, and you will be on the new branch. > -+ > -When a merge conflict happens, the index entries for conflicting > -paths are left unmerged, and you need to resolve the conflicts > -and mark the resolved paths with `git add` (or `git rm` if the merge > -should result in deletion of the path). > + With this option, the conflicting local changes are > + automatically stashed before the switch and reapplied > + afterwards. If the local changes do not overlap with the > + differences between branches, the switch proceeds without > + stashing. If reapplying the stash results in conflicts, the > + entry is saved to the stash list. Resolve the conflicts > + and run `git stash drop` when done, or clear the working > + tree (e.g. with `git reset --hard`) before running `git stash > + pop` later to re-apply your changes. > + > When checking out paths from the index, this option lets you recreate > the conflicted merge in the specified paths. This option cannot be > used when checking out paths from a tree-ish. > -+ > -When switching branches with `--merge`, staged changes may be lost. > > `--conflict=