From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dy1-f178.google.com (mail-dy1-f178.google.com [74.125.82.178]) (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 2189331E84E for ; Fri, 24 Apr 2026 21:10:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777065025; cv=none; b=U30U5AidO0xcNUKn/EXp3Xg8x7qb4GFV7iGrswG5dsVFtCyS5Mpl9+BFVI9TxoIXcB6kyxcAgqctSf+1YHTJAuYm2paufR5QcUOPWHH4hxydG96FcBB8LMX+qAdNlAK5pR6dBFoVEpREaLFKxxbCa2CySncWPCcPjU4+5fdjgCg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777065025; c=relaxed/simple; bh=BgcKVl0LKboCITzyO/sFHlcQG+XLZ/YLp0z/+DznvJ4=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=sTw2CtXrWi41FJZi8m43yLowgm8YLA14ip0MCftrreQ2iLO3Xa1v0Fst12ebZhrPOj6MXNTOEkV6H/GSFh0Lx7NZTsHp40cZ9WLU7t60x0SO0IjcMj3YfkMN34wrY4mACpp9PmOZL19zOm8wqkmvPg04ZKJTv/Az9/uqOtaKUAU= 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=KND+KF4l; arc=none smtp.client-ip=74.125.82.178 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="KND+KF4l" Received: by mail-dy1-f178.google.com with SMTP id 5a478bee46e88-2de831d2b20so1602322eec.1 for ; Fri, 24 Apr 2026 14:10:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777065022; x=1777669822; 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=nqYbhiLPdvNlrbdNIHuSXnNX/EmFBKqVJIpOcnZfOt8=; b=KND+KF4lelpzIgZJbLh377d4k03TXb69akPo3wPhSGy1s9a/uejrWCugPz4svIUyk/ nNOuePBqD9Xmjj/JqxueUFQGGNCfi7tanK2O2I0B4Zu7Hfb5+2JtjDJFKLDfIvahlsFM gNsqWjpg6SqR7zvmZl5JvpZff6XQEOkZJgb8ykocg7+BLDtd1N67S124XZQlIVckwTe2 ZT9IpM4aaHZ8RF/oL7ax4EV+22l5w2m/KqOAlTHxp0jNsMV8GKNfiIg0a7b1a9trj3VL d8GQgojDyjfSesyOmzRccfIZcXqGPO1t0KnFlo/d0e+SypCoW9/QDY4KlYivx1nOj72b V94g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777065022; x=1777669822; 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=nqYbhiLPdvNlrbdNIHuSXnNX/EmFBKqVJIpOcnZfOt8=; b=ZABbCQ8/zop429C8I4papnvDnTA5XJvhqWX+fi/NmRw0Rg2PRf1LIuK4DYws3GiCey X6CO0Lb0ncbKaAUaTruaaWcIGThmcD7CEUluFj2UtpaDSVzfnrApWaZnorOeknz6jUE1 EY/67hxxep9prLmGPelMvob8S7mfFyQnDvVM1ecMcw9uz7rANaBuYlx7SlEwXYXZXK9T MRrgGOcAmZ9txwfLOckCp6Uj4PsM53GT9iMER3l1L0WA0OwVWpnz/03njyQUyU8kkA2I CM8Gn+A95XWsvRwLtW4rIawuPUZszKGqlJQPX5hdMZJUDoxG6ScWmZeDhUc5UnWpiWM1 8QGg== X-Gm-Message-State: AOJu0YxtSRWH1q3JYSJkugJl5ajxBDwqL6AvT6WBYom0TUiuGd82Pqa3 SZnhjMmm/j/Wijdlx3FJ285jwl/BoZuEtz+s0cq6hMUETNub0xJQsA29lqUyPQ== X-Gm-Gg: AeBDieuxL7iJPGwWe4DskKKiQCsanoW08IyP4FTnCvpHPlPZ8qPwXBRtQ1YRQEhHBLD XgFKQ3skNpJUFWW0I2Lvg2iDGD+a+yftpmtlsCSsRlyoK681dToWWq7gob8BmbBke0LxtMeTn6N OVw853oa/LiVPKvg77RkjRlK5UziBJL0hPiCM/4wr6Z57YoEffrLkyD2ASnuV8foX4usk2ATpim g0zOyX+Wcv6DDRgWds89Kwy2fu1C+36Mjerr8wW/q4kIOuYDdQagrbOyJli13lhYO85w0KnKKhP aVeR9GHCRzepsO/EN5zUMbVABFI+GzvJ40cnPC4Ouig7hQYAB84hr7we1mW8AzNJA8jk1WqUgr9 KBn3c2Q8V4Ab+M8I6oP9kHAwD0swbQ57qKtjR+sOit/vbmlinIT9NC/h/HIKVDIlYWErMDOP/KS zWSAGhYxueCTKw+v2XBu2ryuX0Lr3h/cXrBlzIrpc= X-Received: by 2002:a05:693c:300c:b0:2dd:c066:bfe with SMTP id 5a478bee46e88-2e478a331bfmr21637844eec.21.1777065021707; Fri, 24 Apr 2026 14:10:21 -0700 (PDT) Received: from [127.0.0.1] ([172.184.211.117]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-2e53d8b944bsm33782329eec.28.2026.04.24.14.10.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Apr 2026 14:10:21 -0700 (PDT) Message-Id: <96b14db827e51966aceb6513f7d16ed526274ec2.1777065012.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "Harald Nordgren via GitGitGadget" Date: Fri, 24 Apr 2026 21:10:12 +0000 Subject: [PATCH v15 5/5] checkout -m: 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", the attempted merge of local modifications may cause conflicts with the changes made on the other branch, which the user may not want to (or may not be able to) resolve right now. Because there is no easy way to recover from this situation, we discouraged users from using "checkout -m" unless they are certain their changes are trivial and within their ability to resolve conflicts. Teach the -m flow to create a temporary stash before switching and reapply it after. On success, the stash is silently applied and the list of locally modified paths is shown, same as a successful "git checkout" without "-m". 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 | 160 ++++++++++++++------------------ sequencer.c | 14 ++- t/t3420-rebase-autostash.sh | 16 ++-- t/t7201-co.sh | 61 +++++++++++- t/t7600-merge.sh | 3 +- xdiff-interface.c | 12 +++ xdiff-interface.h | 1 + 9 files changed, 211 insertions(+), 147 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=