]> git.ipfire.org Git - thirdparty/git.git/commit
checkout -m: autostash when switching branches
authorHarald Nordgren <haraldnordgren@gmail.com>
Tue, 28 Apr 2026 18:39:12 +0000 (18:39 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 29 Apr 2026 12:46:03 +0000 (21:46 +0900)
commitc07039ebc4bbf2eb6c852fb1280891a448d1bf48
treeb57e9940626b24c3a2e9b233ae12ea259cd46c15
parent26e4e50d463e1427c6288b33054e5d9a4a99a8f0
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>
Documentation/git-checkout.adoc
Documentation/git-switch.adoc
builtin/checkout.c
sequencer.c
t/t3420-rebase-autostash.sh
t/t7201-co.sh
t/t7600-merge.sh
xdiff-interface.c
xdiff-interface.h