From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dl1-f42.google.com (mail-dl1-f42.google.com [74.125.82.42]) (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 8C5FD3D7D75 for ; Tue, 14 Apr 2026 10:50:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.42 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776163835; cv=none; b=DhlbD1jPbXyhDSvz1Si34COp+exM/6Bdxq087frY00oyocg5Wi5kHV7A1DXX0vgQN5Z8N4gXxJ9OFn1HNvym86fbx8wEAOKT8kJRfVPtn3eqhswPGggfwNf+hMsPUDnUZfGscyhdRt5Om3saSrDg991+pzf9friDWp/t2lNeh74= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776163835; c=relaxed/simple; bh=dHT6IN+TPovUs0xbScJXFAghAckDXQLhY92TtUpQPK4=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=ABCb3/wn0kWCgPuaEALrEePLIqtKopgh1OfxQ0qeQyQiDKKd14fqPWVOQielinytGciG5Ck4h9zFliCeOdjD1v2K58wNreqiOc/tRXxBkfKVsnnRTcQTwP0H4e3zd8kqP1wQ1k1XWbqwKLy70aXWvvFnoywU6T0yR9evEBZMCcg= 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=WyiX6FP3; arc=none smtp.client-ip=74.125.82.42 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="WyiX6FP3" Received: by mail-dl1-f42.google.com with SMTP id a92af1059eb24-12c1fcce8f8so10238773c88.1 for ; Tue, 14 Apr 2026 03:50:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776163828; x=1776768628; 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=6QW6AzV29/6lz0qEkWsJk7vFp1W2PngO3niVeMdkzXE=; b=WyiX6FP3jNYtk9oga6jMsPXTgCQOAvfCLWBawlvAlEOJxGGpk7yZkhr0g89oJVHF8x DhT96TbLQcvlsQOcZYHbv/lXivPez7F6eTEKe0x8u52Fxdv8ilUtEzxG6FeyCVxnga00 Qqh2TiHtava3nB1vSbCz3bAZPGOW3MHsKpcHewrBDzblqZ3E52xAseOZq0oWguOo+xt3 tQczDzzzkgb5R1jG8UBd0iQqjdw1/dK46eRDYJX1ziq9I86rjbeXwciUPuAWuExVFd0q Z5/oO8m1B/BEcLBZpNNVbW5EAKgKTitlmC/ttrGUNTS/IZ2LkkC3VcizllWtekelbPeJ AzwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776163828; x=1776768628; 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=6QW6AzV29/6lz0qEkWsJk7vFp1W2PngO3niVeMdkzXE=; b=Ilu4BammR4LoaxyfoiCeTpWOGoIcsGBjJ5RveYu03rso/Ck0bKbXKcKHXSCfuhYmNX 89Zl8mVCK3nzHABSvP5nEc+Clbb5yUvScwJOMWQMkNdRkTsQtkPAJM6LQ/H891ATlRUD dGRiSgH22pIotyp8Iq5LYa5KhPU/LN2BPLLym1Y6+bOs3qssUlJKNx25RdGub6UtT5Sf Pk/19KAjjsegaqWGeJQxY5yGjB7QDhkhQj13RRHNJgQHLkl0ENjVcUkakf9qyXAW1Ln/ 8KfZ55arHBVD5FfBkGnmrtKRjwS9VpmlrbmQ2Gkp4Ue0ScHpfZIj/gGajztHHiMfyjrd AsQg== X-Gm-Message-State: AOJu0Ywk2ow2e4xllvgLStt1Bu8vUYoMx2FSZtMzCJBf147smNo4ard8 poFCyJseoFXKKilB9HAMt5nyshDsQLyXACzBBx8yHPiLF59DGkdhZ2gX91OrnQ== X-Gm-Gg: AeBDiessOnkzD6K37MpKUgqWsQs5VK4lVviKIhqKBWr2ZTSYh4ejAoyTDKoGCD5F3oR SASH4lLrwa4x4YT1S5FcXpYdkCkQ7wyY+0XwuNPIhgTVT4+mkjGXNYHZwq4GZgfhlv6x7EALwuO JBOptaPRog5rf6beBEdsZaXP+6XIAlE7tIyzJLQuqcdD072dT+eMjZ0uZZoRJTXQk6teIx2SWLG uS5Rt4UpN64IafZBnPNN/PMdf+vKP0eURTBBpdGLaxQSOe5YOYTHmNPYqmrzX4Z9OJhsUtkLSCS dmkl62Xdsfh2lKoCm7tmA+fe7EP6APMSI/kJMbfxTQmCdHYLL8javW8crKq2Go2obflhVXAvrox Fj2PzueFJL6BWYUL8Ayir6p7zSuuzETwPeKnrPMKhQWJObRH6OurS28W3inAMYfL/wgnpDrAVNz KSYPneCoH7P7PlnASb0pZFedpa3m/1 X-Received: by 2002:a05:7022:fd01:b0:128:ca6f:adf0 with SMTP id a92af1059eb24-12c34ede8d7mr11525238c88.17.1776163827801; Tue, 14 Apr 2026 03:50:27 -0700 (PDT) Received: from [127.0.0.1] ([172.184.219.212]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-12c346fb031sm17066496c88.13.2026.04.14.03.50.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Apr 2026 03:50:27 -0700 (PDT) Message-Id: <97a5d87c811bddc3db2f74546b2ec9d030fafb72.1776163819.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "Harald Nordgren via GitGitGadget" Date: Tue, 14 Apr 2026 10:50:19 +0000 Subject: [PATCH v11 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 | 62 +++++++--- sequencer.h | 6 +- t/t3420-rebase-autostash.sh | 24 +++- t/t7201-co.sh | 208 ++++++++++++++++++++++++++++++++ t/t7600-merge.sh | 2 +- xdiff-interface.c | 12 ++ xdiff-interface.h | 1 + 10 files changed, 403 insertions(+), 150 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=