From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dy1-f173.google.com (mail-dy1-f173.google.com [74.125.82.173]) (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 60589214A8B for ; Sun, 15 Mar 2026 11:19:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773573568; cv=none; b=fm95Nc6AxViu2H31ueqxOUnhB5pG6U9UhVn/vXNOLhTKQzMrpXzr/WZxaBOQXRWxzNzQ3mx6y0ROg089HiQ+0SkbEtyaHrRBZgo3TmwtLvu8jCmY30++ghalCyeoMTh8Tx8tMaSYhCna6Ld5liIHJpIESSIFYn/jQ+wJKMTvDDI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773573568; c=relaxed/simple; bh=EUVYiE1QgOaXG4qII4g+T6cgWD6Hlc4ZSbCYcjp/iH4=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=bbfi830FtM+fiBNtL0g8zwf+IJbXu2dKe6QoGnzVC2+e+qmOb7FSK7E/gqE1+lFixh7HVL+UFsO7SqEhkgDOTnk/ayCQAakc9mrChaILLBMkajNxEFE5WPZLCe4tK6zcFFYpEyxB7oQNte4usnF7B1lboaMMT6WfmOy/iRXcKAs= 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=G0eZxYzy; arc=none smtp.client-ip=74.125.82.173 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="G0eZxYzy" Received: by mail-dy1-f173.google.com with SMTP id 5a478bee46e88-2be1b5fe11cso5510830eec.0 for ; Sun, 15 Mar 2026 04:19:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773573564; x=1774178364; 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=nh8dIRr1j7tlMddb9G9e7cjF4+t4XQsr2gq7fbaJHCc=; b=G0eZxYzyrfIewKolgUN7vVyaJMCT/vi87/8UZa2ScEKx1hCEyXhwvyYKDqO/F/Q6zE yNdy1CfWoXRGUMZj+OMxb+1TmcrqX3TdbMH1cJ4DDSSRxOPnbSUR9ON4mm8MtgZzDs7c LwVnwcvY7uPpgxSpVpJjD1G8nQigcspRzO7uk2XAwXv2CcGU2Ns8FzhcDFgFfKKz8a6H sP9MREFlLvfBOjC6l094arGuPhf8eMFUuu6NoXFYpFRWX+0+ZI6TVL/ESUmsdrLaFutS Da5rs8l09VSmF+sFCMlNnu3kh1YXpumJHmShXM680GVu9UhwBy12ikyOeAmlpbEa/E6O 1BQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773573564; x=1774178364; 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=nh8dIRr1j7tlMddb9G9e7cjF4+t4XQsr2gq7fbaJHCc=; b=aHlHf2xRCBcidnJq0K2BihuaaTLj4NPdH6bc3UAk+om1HVx+z2E8dfJfK89RxWzDss PQa/p76MpIj+2tLmy3bu/oNAXQT3pn6cc1Tbib1Y5cuCJJToT3dgQFOUa5ufgHUrI929 IE04gi5qUJMoV+afOgTLLP9FI/y+Al0Xkdv3D209rk5WuvnXlNf/VzATjjEs6/IeHiBB FGBtbcXexx2MIURqgAHgrBuH/Dqxrr8k8aLaP76wJWE+1rGCoqk2FgevA6A68Gr6mGaB sKXaPuFDkpjgFDZWK3shOielawb750GxXi7rOoMqClsrY7paXU4dqbtEtqF40BIcpTan KIJw== X-Gm-Message-State: AOJu0YyWUZAXXWQr73L5m4QK7/woONk7LmiEEnz2FVHsrmmFS48lwaeq NT7WD0z+HiXCRww2hZPU4xfwHC5/qXgs2oowDFHUvJHMPZnS9y4mRNtI84G+Dw== X-Gm-Gg: ATEYQzyynmE0ZxECgm6qBJG6Pca5bQJt0Mwb6RBRYNnq3kysdU7m/6ujVXpWiEQITZx QITO/7iQZ0RXJjAPBO6ND6JyL6bslmN4a3Ri62UuE4ghB2dU1Npg4oxhA81Ybuy86L34MOxHmWF YJPmp8S7YHswHeymUdTcCY8Ci+DQPd1QOHIIsCnDDX382dWafDvWYRvTotKJcXVjlJgcjji+U6u 1E0xyRWQUh95c/aWq3nD9wJSLB62/a+aIpK2U9IH1Q4laXlv2oDgRBhCllhH4brtPEEQ1ch2Bmf AskavB02Bc1XIIblSFOIzWVe/P4uB/JP8Aqjq8sSQ+NXMee4c26qMHPRsUHYq9LX15dfgiswS01 fkRtlw3awP3NbgtDk7fFCN9URLy2Q5ODTH5zQIkyhCWRyQeIbrq0a+SpckYPIDAd7Z20oItKJbo O6UC4d0aMDSXvqOhZnKSnsK89COFc= X-Received: by 2002:a05:693c:60c1:b0:2c0:bd37:a75a with SMTP id 5a478bee46e88-2c0bd37ae65mr316504eec.1.1773573563749; Sun, 15 Mar 2026 04:19:23 -0700 (PDT) Received: from [127.0.0.1] ([172.215.216.194]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-2c0b1fc876csm4707333eec.29.2026.03.15.04.19.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Mar 2026 04:19:23 -0700 (PDT) Message-Id: <7f3735d40ef9045595085064340676cc6058e627.1773573553.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "Harald Nordgren via GitGitGadget" Date: Sun, 15 Mar 2026 11:19:13 +0000 Subject: [PATCH v5 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 | 180 ++++++++++++++++++-------------- 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, 376 insertions(+), 136 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=