From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f181.google.com (mail-qk1-f181.google.com [209.85.222.181]) (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 485363E5591 for ; Tue, 14 Apr 2026 12:59:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776171597; cv=none; b=HtpP+RSzLUiglHi4GPgC9emYyuTy6FekQMBcKKwxCB1oKzpj+IyqHZBhOA7FSSqDaGPIwAko0PTWL8+MANNETX96aDnBoeAgHPEYty0AcphgessMZHrlx6A3xr7TKG79PiIME2C3uG6hUm7jmhuSHH/3FeYx0iQMKsh9AYsb4Pc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776171597; c=relaxed/simple; bh=394DMgd/v9GnnjoVide+XyuGWMXRmJijnHBqQ6CC1VY=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=MDPS3jATsVCfgvF3NkIAul3Mluor0V89hIU07+iTUP3tE5Z4UZdnGo6z+uBUuieP8nonFZXQjw0TiyywMtkeMu1ChinGvqZfxSQpEnTWp3TNiH9xdVj1M0P0MfB5x6AxbJYm/UqcsN/Xbk4z6i/xMQ0xvYTwscECtqmH8Ex07c8= 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=euNM8Sqc; arc=none smtp.client-ip=209.85.222.181 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="euNM8Sqc" Received: by mail-qk1-f181.google.com with SMTP id af79cd13be357-8c6f21c2d81so417189785a.2 for ; Tue, 14 Apr 2026 05:59:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776171594; x=1776776394; 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=hTq5ct3TV/y9mTQyqg5rKP7giBzp/swzMHlcSZ1LG3c=; b=euNM8SqcEuFH1KQpmeww3viLihCkLJuczKp5OC0rwwOpa2thvetQXxaMF8gy7sGZs3 xJhv54dKeYNn8VqIMk4xR3Ic6aG7IXRssVPNkWnZz4cS0rfulTFnw0iLBqdrnULDajJ1 jQ5g/cBXV7KBfmOuCn2xWGVbz4JKQByFeWsubnYl4UdBrLBzHYp9w0mddFup55YarCBp D+noqN4z/UbMhvj0ELJX1sDCYsaSZ+dpSO2xYAi44qBf4JbgGD7GzTVNj0NS/mQQ/3k1 J49BAAqArNC4NoJeO375Fn4iOX2TInogbcQgpk+sSM0ffoaTxEvT1cdtu1A0bA9t1/6k TeCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776171594; x=1776776394; 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=hTq5ct3TV/y9mTQyqg5rKP7giBzp/swzMHlcSZ1LG3c=; b=CJDQgKusBL/Uiah07Oj0np7FunY+ZUWS40TZIkef5DzveOfVotSHsxq53XeWyS53FR zCQw81GzXsJFCpwNKuqrIXl8afH5C24QVdzdUe5cKwqpt+DGEDha6Lo144XLzGa1QH2i jZSZxHzS7cbwhTKiQyqVTUuPTTkLU3z9gSVfrsbMvX/zzITtP54s1D/SmH5w8nJ5IQDw otLmDicmHYLxE7l7bpEvEsj3dGpr0EVHNuso6NHdS656VpVncvfay6Z7JFziCZLp725Z 5cxUmvMNLBM+dotU80lJ4BKiwVPXBhvS9V0NhSg8K3q+2ne69tqwyQjT48NIAJ5PtQ+I GcyQ== X-Gm-Message-State: AOJu0Yzx4Hi4qer3iYObfADqHx/haSB1jJ4LGlf/T0OOo6Ztf8KMGi/z /WqWdl1sO45EGnndC3Xy/IYUJ8i1Hm+7e9vrTawM270lVLx+pe+GpQ5JPNbHfw== X-Gm-Gg: AeBDiev2nkTgTPQxs5hf80yzamvAUoUANWQR8eEcXbD0Kt1347S9ufAqYRCncV+oRAb +PfsEfVwjZjQcfwYjvTqXqzLThvVMZn39ZTD0Pb3ZJgKt/YtXtIMKvC8UG4WG8CNO7Gkbq9RBVn CuRvsdKryAUrPShAJ69AO2srhrt86FFQuyN4xVtbup7zqDKgvmffUzKNDrv0tXq/1OvZhXPjMRY 5b5MAS9b2xqv9S4hHXFlWcBk5WQGT0dwL33Z8lIxe5s4Olp+KIocO/0eq5pFsz/x1zyejX7N90x NOMA/Bl37FMZeT49HAdjaTk21Z6oOP2Y3b38IsMzKSCmXktFD0QivP8rk1gC2dqukafobCkVT7j iDeTdXR5n5hfnTZZ5oJpDYeyUMUNF5GyVN3PQ3AJhvnEF6+EmSUg5NT2YwvLca3w5mfhHNvMX4G fHa2+j1CZy/OJ2hR8Dj5GNGFrWrec= X-Received: by 2002:a05:620a:4586:b0:8d9:3cb9:9905 with SMTP id af79cd13be357-8ddcfbad948mr2596542385a.54.1776171593710; Tue, 14 Apr 2026 05:59:53 -0700 (PDT) Received: from [127.0.0.1] ([135.232.232.51]) by smtp.gmail.com with ESMTPSA id af79cd13be357-8ddb5f8886csm1048086685a.6.2026.04.14.05.59.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Apr 2026 05:59:52 -0700 (PDT) Message-Id: <911e520431e9923d0b5795532e2abe794477707d.1776171585.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "Harald Nordgren via GitGitGadget" Date: Tue, 14 Apr 2026 12:59:45 +0000 Subject: [PATCH v12 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: Phillip Wood , Chris Torek , Jeff King , 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 | 33 ++--- builtin/checkout.c | 147 ++++++++++------------ 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, 368 insertions(+), 135 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=