From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) (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 553E018787A for ; Tue, 17 Mar 2026 09:35:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773740149; cv=none; b=jc3t9/njYy2oR1tmaXwoq17+5horXnktoZAvPDn4madF/ctAvSdiog2wePys/xVkbVmogdp0ffWDI/STbRWRFbL7YJrpP3XLE8kWT0VluD9dUvQBkWEeMrIZY0O3r38czGIFxbv52r4rJem/wg6lF+vYvXnjbcvhyA4qXqiOeaA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773740149; c=relaxed/simple; bh=53GZTW36BW97EGvOtl8+o28HNLgPHjOkTIZ3qi5Y7sc=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=nNxl87M3aq2t9opfXp8gvqdhx+VP+8Rj4/CpJKJKttFcmy5iuWJ8GhVT80vN4+pp3YDrvyoI+3sSqVvwfA4IS4/GiXDv5cfKtnOZmnDfheL3LggHgX72Q8m4p95YQXs1vsmUuEC9h1vuJ8PDfLe6NaCEv22YBqDe0MzhHE6kQes= 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=Sq0WNalH; arc=none smtp.client-ip=209.85.215.176 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="Sq0WNalH" Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-c739d32b72cso3869821a12.2 for ; Tue, 17 Mar 2026 02:35:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773740146; x=1774344946; 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=CsgnBal+swVJkLU/NZenFA3G/sYIZnfHOUnc9N+I7nM=; b=Sq0WNalHW6z7geJ8RQCNMKdPnTqcf2Zf1tXUSvbAvT93Xb3+lYoNJZHjjxUIloD6Ia Z83O4UgDGxUrlCi/dr+ugukUVMD7Y+U4AWwlQj6E9ytrPAFMFfLshyGhHCbfP9gqDxRc acr8WUYQCgb2IQKT3Ubobvij/ssldHCOPmi8Qk7t3yqg8I+MatukGj5Azlm5lxK6+s4N geAn5IeR2eDL2SSNvFal/4xNyhpawLGhMouZEFsG+AnN584ZJ/Q9itvAIHJINXKBx/HJ MXar2P//yPRWIAppUACpDpD6iqxODlzc89KbuwhDVYfppjkNyXAig6g1fZ7rC+jjiPc+ RM5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773740146; x=1774344946; 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=CsgnBal+swVJkLU/NZenFA3G/sYIZnfHOUnc9N+I7nM=; b=TzFxYm4gwKcZHDIP1qdqSM1kI5KBJACX+/l8RaBuhvRokyrpeKGXm9snwND5pqHD/G CavT4EIIaViGcF03MQPUoBzjo/lqLSJ2XHaxw3QSi6sRBo4nLVXBFyetyKGlhUWFQF1M TfuXAMR15eqz/j4kqwZNxG0WE28IROq0xRRWkxf88nLFtZAkMuxXbII6eEPfzfuYT+q5 aNTeNYZ36PHjIcKC+4detp2O+XxkrQRL8gSgHtxnh2XK97XSun+FmaTyokXR11l3DRbP qttQsAT+kChHDq2dX4VzGRd6Fi0EKh2bE193ai7ke3JMX5qS8ggAO0SVCELarnB5LRbI /VwA== X-Gm-Message-State: AOJu0YyTi+q2RT2GjAmscdHSElT+dmgo31GYal9pIyNwKYFPr8BtBpkN 2shtpNsnqvSrb/t84Fz+AobJMO9tzCvEtwUgqjtCb2sIHZ3sGKufJHWGF9TNCw== X-Gm-Gg: ATEYQzzKQIQ1jG67IBVLj3CgsJfMCznrzd/MdC5jQ0/FIDZwKP9HumE6ApbxuN+iMVq +LnIJCewnRalDHZ7wgFdrHizv3sb6QRAdaKqETfr227iWpGk9O6juV/f1WboWfDNieKDswEuG8b WZA2CfFFepccQleZNX/AkJJy3u/ixE1/Y1wZ/SJt27IVHjp9yH9Sl2kSu6ZWphOHD4UjxO84qH0 RPkqTGyP78HED/9TXyVxKN9aqf8ixHOL/gv4SOn3drkj2lePz9o7sWfcpHzW9w1CERbQX+eaOkO 3hLRdD5hqt5H7g8Nmh+3QWB8QiDPdQ/n5lBP7U2I1x6Z7zeWDIh1+4QLPZx6T0MW2y5OjuFfPoX nmMYB7PhRwtTYB+v2km9GvQRrT148EhvPYzUqLL4jHhw30R5+aNAqTi2tPdRPmN1altSGMGfu4k bwYRLZmIJPWWHa7hZPHa1poCk= X-Received: by 2002:a17:902:ecd0:b0:2b0:63b6:dfd2 with SMTP id d9443c01a7336-2b063b6e1b8mr27987435ad.0.1773740146137; Tue, 17 Mar 2026 02:35:46 -0700 (PDT) Received: from [127.0.0.1] ([20.3.221.182]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2aece80a3a0sm131160695ad.65.2026.03.17.02.35.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Mar 2026 02:35:45 -0700 (PDT) Message-Id: <7ecb0835b7680f21b1a69d33c8d33f87ce2e48b6.1773740139.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "Harald Nordgren via GitGitGadget" Date: Tue, 17 Mar 2026 09:35:39 +0000 Subject: [PATCH v6 4/4] checkout: -m (--merge) uses 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: Harald Nordgren , Harald Nordgren From: Harald Nordgren When switching branches with "git checkout -m", local modifications can block the switch. Teach the -m flow to create a temporary stash before switching and reapply it after. On success, only "Applied autostash." is shown. 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 | 27 +++--- builtin/checkout.c | 137 ++++++++++++--------------- builtin/stash.c | 30 ++++-- sequencer.c | 18 +++- t/t3420-rebase-autostash.sh | 24 +++-- t/t7201-co.sh | 160 ++++++++++++++++++++++++++++++++ t/t7600-merge.sh | 2 +- xdiff-interface.c | 12 +++ xdiff-interface.h | 1 + 10 files changed, 332 insertions(+), 137 deletions(-) diff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc index 43ccf47cf6..18022e610e 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=