From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dy1-f170.google.com (mail-dy1-f170.google.com [74.125.82.170]) (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 B466137BE6C for ; Sun, 12 Apr 2026 11:51:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775994716; cv=none; b=LxSEF5cqq/LNJFGIAAu8d3fqhDa1zUYVQmZiWD3+YXPgjFn55mlF1Wkq8CQnMKZuku0mUax/oVa8FAu/dyYtPt1zz+6W0oRA+8lqBfAUk8XpNfiQoRA2PWeeDfYjKpGVXHtNJA4S84zvuQSZr3xx1m+s2H0zsROGPQj8ZYkpnlY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775994716; c=relaxed/simple; bh=tSpI7+ZvEMt61PME02TIogQxl6mqyD/r3HH1ZMMdRQU=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=b1VwZu52R19gaxy52Qucauo2S7gliX0ohGKRMq0U/2YAUmcndfPFKuxEwZW4+jr4sy40ySrn9/7s4YjEC6vpCDUw7d57LTXJn0yCUSeDANb207amqjiJjnB3a+tw+6VBNASavj0m48jpUuqzqilQLgDGSo9G1sKXhCEd5kxyFWo= 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=sfsw6RfJ; arc=none smtp.client-ip=74.125.82.170 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="sfsw6RfJ" Received: by mail-dy1-f170.google.com with SMTP id 5a478bee46e88-2b4520f6b32so3651078eec.0 for ; Sun, 12 Apr 2026 04:51:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775994713; x=1776599513; 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=1tOwW/2mLhuCZ0MwZ+NWU4MqJz68sFCRDG3EiwrP8u0=; b=sfsw6RfJhwGTDK5vBg8I+WbL8dzjQLILUv5Ie1MVUInqzNgiPRnEhSEQ/dX1M5TFpE En3Dcu4J+AwtvvRfF2xlUOXEZQ+YbcYiVSZ9jM5IdQdUVDyNodyLWg73ugBytZbbkbuO CC3jGaZnffNaRf/xoZ1zavOQkVnnfJpjieGy/HtI2EAowuZpQ/jf/KzgbM2w/8jTWGbq xnF3AfVcndW76JpsOCIPS3nZvS0pC4lhhqGxT7FNguoGBUSpEFFJNnQxYvcXWBV0oFBO iqgTyVSLx6jPUlsEZXt9rapoUfJoP+32THXysQwfnLnOuKHvISA9VWHZpsu+hfCdFJWZ YT/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775994713; x=1776599513; 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=1tOwW/2mLhuCZ0MwZ+NWU4MqJz68sFCRDG3EiwrP8u0=; b=XUerEZ7dIl2tXzU6uifMGOdoCu44Gh588xOiOYEyJN6Kr59Qp1a7Kp46BulXLzdGS7 OafJlPrQlo66ajraKSO+U0mwo20kXuCtBRq8gepojwgwntgHTHboEunbkUxXahVPiI6q bt2tetXmaHJqc+iaQ6Ue0Xgrz2+qAEcgyWG4k3FKcdfGOvSeaNYlw6MsjGrD7kBXWCOH UXN8mvQjTKz/65uyEhfZ3Guhg5eUGm3CTc+q9J3t1jpicuSr1GOePXE5vrVM6HVn7dQp GFz3ijBFJ1GV5W0QyDYQr5WGUiJ7XD+Dd1XOIc2kWzvBIeMKfbTv5SXZfMdwRfsu26Ot dtew== X-Gm-Message-State: AOJu0Yw5f2nFk4mMJIkmN6FLtmE4Q58ieW+AT9uJeajvzJc1ouGpq0ky MdLPgzy+u6ZcyBZym6qs7Pdk+T9HFHfbNb4QrJ1qLi7DRMgYvPH+CkUiRE0UAw== X-Gm-Gg: AeBDievNrqOFqIhy0uhRL0RD30xpURweu4i7yNY+5ZjMyFsPvA52l5m0Pp00Tx0WKX/ G8YovS1dFJ+LzummSOIbjzQB8gEJZSfJf2oWL/pvSuyjQEd7Z5WpmgnerddLS3V+AAhxpfbIUa7 XZ4F77MdQZBXeETdrD8LigY1YoKraJdB6FA/KbAvWZweF+OKQgbdMW+ZuKeE9WVqIQZr+b1RdGu cSR+25cB/Z24AwBNhl/duBU+xJ7XxF3eDhxGtDVuh0tE6TLYmEpmXBIiOtjdU7t+iRyd2rcOC0k 2bnbDF6AubXjkz2plQPZM+tYIhgc/z/cuTtGqSv8M67xkpnStxuzBjFfpJ2EzJM3oobFfwFnCbc PvsyhI4LRYAXX96arYEI91Mc3ichZbeFGE9kPbWSOAcfvGGcMz9rdlSV6ossdS8VYNsODc9YmsY r0FnNzNh8ZZ9Dg1GKbmVffY4bgJHhQ X-Received: by 2002:a05:7301:1003:b0:2c3:d51b:91c4 with SMTP id 5a478bee46e88-2d5876a4991mr5065436eec.7.1775994713341; Sun, 12 Apr 2026 04:51:53 -0700 (PDT) Received: from [127.0.0.1] ([172.182.226.129]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-2d562db64c4sm12079443eec.27.2026.04.12.04.51.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Apr 2026 04:51:51 -0700 (PDT) Message-Id: In-Reply-To: References: From: "Harald Nordgren via GitGitGadget" Date: Sun, 12 Apr 2026 11:51:45 +0000 Subject: [PATCH v10 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 | 138 ++++++++++------------ sequencer.c | 18 ++- t/t3420-rebase-autostash.sh | 24 +++- t/t7201-co.sh | 195 ++++++++++++++++++++++++++++++++ t/t7600-merge.sh | 2 +- xdiff-interface.c | 12 ++ xdiff-interface.h | 1 + 9 files changed, 346 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=