From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-vk1-f182.google.com (mail-vk1-f182.google.com [209.85.221.182]) (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 D60163A63EC for ; Fri, 10 Apr 2026 21:01:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775854886; cv=none; b=nCVW/DABE4t73jU/WY5sE/IYRpMDC1QMX9IVHWBBVORjA0kpOBAzSdrz+LfeDj6G+7BpeKNYeIZHp9zl9GBYmzULHUBlP70aBg7dUcsmmNiEMrA25CUqcEAMnu5m6AhBXaUGhMaEJmA5pii3dR8MDcTOgtUa4g1fcUg48t2RyRY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775854886; c=relaxed/simple; bh=DowXi/3E22xhFvx1n/CLdBIzo5ubTOOKY3GKDBXLo7A=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=txOeE9HXWsjhJV2201F0io1kNcJ0BWM1Y6LqdWMvvIyRpBRGZwamzT+Hu5dU7lpHzgh8eSHc3gU8eFVFPjgCfcjel4hbYBUS2OmPCRHX7uiec+HOSH/3zV51tm0Jabzhk0wnyrnfDm0G3fK/3LJtFo+2S2UffyFUPzjC+Ey0Gdo= 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=f4q/kEdR; arc=none smtp.client-ip=209.85.221.182 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="f4q/kEdR" Received: by mail-vk1-f182.google.com with SMTP id 71dfb90a1353d-5637886c92aso1082997e0c.0 for ; Fri, 10 Apr 2026 14:01:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775854882; x=1776459682; 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=I8rYER8XINRJx2coGLNG/i9Bb9xb3QV9NkhPmSJXdUI=; b=f4q/kEdRqbst3aEnZ2SSLLtdegnzNkYXjIitxbFKNmk69qjluAQRzuTUpbZGk9as23 6ZziYG+LBVzEEewcwIP6jdC2GiLO5UMZd+JyszxJKKdlBmpD0+1Vgsg60H2Bft/MO13I Y38Wb76t97VEGfPUV0mt7i48N7hGICSzgyLuvq7CgvYF8OOzFy2u+eeM5ISYFFI3/cyC cejK9DUlawjeulAtrEXaMvgcWkvU5SYpGLRED/uVL0nMfF79XeKN4rNOoaR1gT+MAAIO alMdjLFELop8bvXQquagfqjKHlgxt8OlJpirGtmbGBvbWnf0qD+xvjiPtjbcVy9wE6DE t9kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775854882; x=1776459682; 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=I8rYER8XINRJx2coGLNG/i9Bb9xb3QV9NkhPmSJXdUI=; b=OjGYyYZOT37ZqpI+ceKmueb8ulNyfhsXSiNvIsHRL9RpkacvZE9+vcXT9tk91m2bEi 6n2gSKMOIWwUO5SOcatDzv0Bl9yed93V2zCXumg8Ph3MiM1GNLWc/hSKVTbjALsJOfD4 jOTnnuGHBkW/yih48aoABIaADhJNerHAVRx8Zo5LHHY4lP37yphlYAVG6ZKi6qo73C8N Y5ibs0pIr/MPIt2SbmEufOHFw9u19jwsOTz4glCvS+X8W/dX9JrXyseCupBIydqIuls2 b/vGflH2iqQbHoEbJt8+/kcqB5+y+r7wthRVN353OAjYssQ4/O9rKxPzHmp2wzyLCCzf zpgg== X-Gm-Message-State: AOJu0YzzwVysByEcBO0hoqmeQkke8vdmFJWUKMRxOvd+OY7BYoZOMxU3 Au3qRlqiEpwq1MZVlmDOlYOzlrAK/MTT2xaFewZnfuuOsd6UH03jAIctHX2VOQ== X-Gm-Gg: AeBDies4e8hZvSc1YSy1T6oWLuxpUrKeyCt92Fv10LogJON8vRsK1y4TD/laLcTro/A 2uoBrrOwQ23rVt4YqacbjZgT7VLVTr/7GnXTkSjOysPTIBs12EySVlW07y79aFyWsClWSdMn5gh jJfZpOfUMBA0ehvn31PwJb5qwbthf/vMUR5HTdito+Nhr7/O2tvQcNiUIIQRhKtqNuHmom9yHrV OWFcdMxxPVsmjdAokBa5O4rVUnmX3sx226P85Lc9ybcMfeGeq2IaEY5/YMmRzzxYLp6FaggVqUH W47fb5RmRN+esPSaSzyRXfY7XcwrEtazmISRv8ajvZosjgrmQ4DjMTPfDaQGz2yMLgnT4/DKM8f VQP0zKP/77fNA9G1Zuy02MrZOJngQsx6TmyOxtyzzKPKK8QYVgs9O0OcXs7r7oy1Vb3nljkvMQV laeMv26o9jINYqIO/n0U5AxvEjkA== X-Received: by 2002:a05:6102:f8e:b0:608:94e3:bd89 with SMTP id ada2fe7eead31-609fe9b15d9mr2245272137.1.1775854882164; Fri, 10 Apr 2026 14:01:22 -0700 (PDT) Received: from [127.0.0.1] ([52.150.30.136]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-8ac84cffe76sm39000166d6.47.2026.04.10.14.01.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Apr 2026 14:01:21 -0700 (PDT) Message-Id: <00e0b3196cd158244cb15bfd7edb7d9d3f0954a2.1775854874.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "Harald Nordgren via GitGitGadget" Date: Fri, 10 Apr 2026 21:01:13 +0000 Subject: [PATCH v9 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 , 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=