From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f178.google.com (mail-qk1-f178.google.com [209.85.222.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 12F882E0938 for ; Wed, 15 Apr 2026 11:12:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776251531; cv=none; b=rH3DCsRWf9lIOKaiqx3kwcvdZZjCQIRtACMRNuJyuVptsXBn0uxv2v+uqRt+l2nDRO55Dw+aQ9kSCGAev35/34Ew2QvbVsiPPYl5uPz8e5Zbz9mAOZ7hBfT7RkpLeKCqyOgpX2CDin2Wb/l9N/XjL008aWVy6eIXC7yzGnPXxrU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776251531; c=relaxed/simple; bh=lcJn5ca0Oci8WM6NcotNa+LQYLj1MflA5IDEqBbEUUg=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=PDRsIdnU7F8TwFIRvFL8qXIzFa1JG0u3/RF5JFNWKYgvvZpLH//4KLBOSfuqKTL+vMhUYMK38LUXRTDJS327k6XMKUQDFa+MQzNudBVZg6q/Z4OM6CcL4q27WbWfKPbe8Th7rG0gRMPqbqgAscuofOYMJ0TsutKJRIbhMbdSMBI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=gyBNRMAL; arc=none smtp.client-ip=209.85.222.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gyBNRMAL" Received: by mail-qk1-f178.google.com with SMTP id af79cd13be357-8dbbc6c16b2so858705685a.0 for ; Wed, 15 Apr 2026 04:12:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776251525; x=1776856325; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=tFQ6zyUhtkrTIUcKXMtPvvM6bBx1zltsummrVNyjCb0=; b=gyBNRMALBJzGJDCROUQlor1hcE5sjCfwJPeKTyE0RSs6oz3CkEy+gmy9RRv3BR7UCd fFsW7Zz+DFH1EUwbN0pPMkajXx2S0qTlrE553hiiC4bhsUg7DzfWCW7w0SLw6BgH9D0h /V0yZhplFMEQ4WT3K74Qv5nHpYaZ9WyElWIUD0JBWcDgCv0E3l5V1DS80g7PW4ONsUhY DB4UlQZ+QChJDii8xcl9TeDPeiMyr4ABC/dv0bTVMPebxLD3voLNNeIFJYm19L+xqQ7E JxRTaP9HK0R0/59anXcETCZEU9GTX+JEiF/SnFDiFgencrYY9QQJVX3ADMZRx359xccz zaBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776251525; x=1776856325; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=tFQ6zyUhtkrTIUcKXMtPvvM6bBx1zltsummrVNyjCb0=; b=Uc6Ri2fG7skyVbDFHa2JlnrHnIh3iJaK6b/jG6thjEW5nKvePi+jKhIuXrovqAxNWb AijQcEDL/J9apI2B4QXb0NcOezjNXLJDqXnhadUGTIGX3Rd3Kns9lzAs168BoJhiNInD Ws8G+Bs78qM0V4zJwZuGKK9xjdu6tRvFSwA8k7dRdE1n9FO7LvekdL9LHtTLjaCYvpAz bdsnefzBbzwu5yOpHEwWgfhl4nOF9TqR50WIt37n9ACcaBkrJG2YJWOHEQ/VJcPDwU/n U1wo83X5IEPFyb16mbW9HuXWKBuiy/oHIo/+G7bhm3GhHEbMx2XqYb3UBMz9/ZjLRIgt bkFw== X-Gm-Message-State: AOJu0YxEtC8UFLZHiujEou1NWQJqbv32Iw7J3iwwDuxFlNeGGXD0K2Pq hkhENUvy/p3jbQthn2+uRnRF2dCuDeI4OUpqe8BVxTyzO+fjttjP2MigCgmHLjZt X-Gm-Gg: AeBDiet6w6W4Moz7c8SbPXqNGzUrtEV7ruGw1j5MH7XFx6drXzOhzO8gM1ybyqYQ1+V N5qwBfGs+fFBLM5iY177hYH8d7AJENY6EZ7q6IHGPcnYlAFHGlByEguNdNePC97+vQQ09ZaG1fM mdXLkv94O5M7Q0xfCtpvDlRkJc4AM9eh9eWdMnGq1+Q6MAVxtEYTOVC+0r1kwdoV3FNDZHSf2I9 pn3S+SmF6Que83TArRNeq62AxpYO+tJiL/ZwRqF9c/BvgPjv6Pm/oZvgf4Fs0N5Mly0xhMzOf7F UNj9IlQII8ajMAwOhKMucn7v8QuXjU68Dgfc5I0Wo/Zof2dSEHqgvBJcTr1aaAxiBdkc2FBEii7 VTePBW/QJ9bHz+NDiggeKfZnfAQCiiApgdKNcu/41FlGaJP/ihpntaXWGO42AaAtTUA427Hg3mO Xoj14KYWuxZ+YzHd/fAeG6+FY2/OqlYDwqMhKDoA0= X-Received: by 2002:a05:620a:4146:b0:8cd:d688:7aef with SMTP id af79cd13be357-8ddd03a2654mr2562319585a.19.1776251525075; Wed, 15 Apr 2026 04:12:05 -0700 (PDT) Received: from [127.0.0.1] ([135.232.227.146]) by smtp.gmail.com with ESMTPSA id af79cd13be357-8e4ef33bc4csm92232085a.12.2026.04.15.04.12.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Apr 2026 04:12:04 -0700 (PDT) Message-Id: <4b3c6025ac4fdf8731f7c7894b68044adf961eda.1776251517.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "Harald Nordgren via GitGitGadget" Date: Wed, 15 Apr 2026 11:11:57 +0000 Subject: [PATCH v13 5/5] checkout -m: autostash when switching branches Fcc: Sent Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To: git@vger.kernel.org Cc: Phillip Wood , Chris Torek , Jeff King , Harald Nordgren , Harald Nordgren 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. Signed-off-by: Harald Nordgren --- Documentation/git-checkout.adoc | 58 ++++----- Documentation/git-switch.adoc | 33 ++--- builtin/checkout.c | 144 +++++++++------------- sequencer.c | 18 ++- t/t3420-rebase-autostash.sh | 24 +++- t/t7201-co.sh | 208 ++++++++++++++++++++++++++++++++ t/t7600-merge.sh | 2 +- xdiff-interface.c | 12 ++ xdiff-interface.h | 1 + 9 files changed, 359 insertions(+), 141 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=