From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oi1-f173.google.com (mail-oi1-f173.google.com [209.85.167.173]) (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 9C060314B9A for ; Wed, 15 Apr 2026 16:24:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776270272; cv=none; b=o6yiBjp4IIqX2J7g+Sk7Wy9NMoqEyVHNNDW257MFPNwRakcv+mXIZrNMcp3AHWtTS+q9L4Fnz+u99hYeY6nrZFJ24tDme+fhsdoZmxvBxHA7KYHotwg1xYuL42uwbmmHdaDs31k/WWw60jMZmSfw6LFT8EgKcvVf0phidRLbDks= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776270272; c=relaxed/simple; bh=lcJn5ca0Oci8WM6NcotNa+LQYLj1MflA5IDEqBbEUUg=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=IUK5FFDCwILi2d/cb+3r//CPH9y19zrZBEby7vJ/aZTsyadXWnSBm4Q5HyJvFD2pr/aQWBzobMI3mZ+3trics8FOq3TF8zdIdIBgl6NkNoOuogT0w6BHvf6rEyGdoqHLNOMDPn8f6aivI1bITlYg3rfteNm/XuXmlQs8KnRCiWE= 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=ZFcu2Fuo; arc=none smtp.client-ip=209.85.167.173 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="ZFcu2Fuo" Received: by mail-oi1-f173.google.com with SMTP id 5614622812f47-467e8aaa865so3995593b6e.2 for ; Wed, 15 Apr 2026 09:24:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776270269; x=1776875069; 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=tFQ6zyUhtkrTIUcKXMtPvvM6bBx1zltsummrVNyjCb0=; b=ZFcu2FuoQeybEhQu/SX7Zp+OtqC9vfOhc+e9vVereheYe1K/0TIfQccrVenk99g2hG hIwkRP12UQqNQKVnwP8dLBWhmKb5aBWrhkhQMQmDkQUrhE46fwb4RFFtQNxyLpM/NSTs S3Sp/jq702AOnIt19fFULOM0loWj0cSg0QMrqfLZxf/yK4nLKpq7E59SDosGn1tmezsz mkJHJB21kn64Z+lFOz0cZcVbPsShmtX1lOVCFRN79yiBAgJjhNjCgj7GM8alkPv1G0qJ erNlHUnL6POY0VK3C4M1J9B2z+dNhU3wPxU9dVpa1NRVEgrjTlKQr1zjDOccIY+x2Mv+ xsxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776270269; x=1776875069; 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=tFQ6zyUhtkrTIUcKXMtPvvM6bBx1zltsummrVNyjCb0=; b=oB4LRat+0Vc+q5DthVZQlmo7+dk3hZTiu3hPNbzx0CuK6Lk2VX8AUoqmmkHLq+vxc8 HhhP+nGneRqTtN0VHr5H5kCV/HkUwxB8huyoBXnERpflBCJGlxU40ktOQcXBewqCw+sM 3wPcZkhMq4rBLq2Ed0DfYn089wiLwo+FsdETB433Xu2JSpahKZ9jVYct15nSFutJBuOM GGuq+Un/vi6x+WZ7uHRFeB48DDPvtYhWI5jjxdiEbsn3YzVh0W1CKS85kIr42bimwKxp 5pa6FZUS6FDEaVYk7sbeUab9BPgKfiEnqlhsEo9BE45FQ0SW8ner1UIMhncpEbLh15ka M3Nw== X-Gm-Message-State: AOJu0YyM3dCPGB+30oozQMPh0FaMAgmAK3wWIeNEpGt9PFqNfnrhSFeG 79rNco3R06fbJzvErTJZJnIUTE1xJXe0eV7DQIHvBWJhHEYkqsWLPK3gDuanjA== X-Gm-Gg: AeBDiev3toWX207rwgYnsBRZ7CmN5gSSlcaMbqRfqpmpHwd2YkjL2KcNCj4QSxOV2JU MgQuaReb2fFFONbTRjAQOzbV3C4Cp7DII6Q6RzshuOCTrfvLbHrHsUyyWD0JhR0bckFzMoSUVrZ nMQDulkKzx1kx2Ut2gjTAOL/5914R7Xq7rljrOr0GcfV4PUk23/XU6gw1W95hfNitwt53E5b03d PnHKA7W+C90k2UFdKieh/ie4JErUjnHEDkWVfyvMRIFIoM4u4lHdK1Cx64h3gx/e6DmsoGukcHY +u8xb06ahOnQkLA8xf/o/GoA9O35mJEXJlmRpERBluzdT5d4eNCEFrUnlpflN9oaxu2y1Z/+wYE f5Yv75pEfUN3r+Fl/CjTSl6G0tpNTKNyl1fh1ROOgDOyS++abAsQnPvSqNKbju6eU9gjpEOeWqY LX4TyqfganRyqRtSOZR6Q96nyg X-Received: by 2002:a05:6808:2383:b0:463:faa3:8dda with SMTP id 5614622812f47-4789f00c84dmr12231716b6e.13.1776270269006; Wed, 15 Apr 2026 09:24:29 -0700 (PDT) Received: from [127.0.0.1] ([52.154.21.53]) by smtp.gmail.com with ESMTPSA id 5614622812f47-4797a515a27sm1149680b6e.14.2026.04.15.09.24.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Apr 2026 09:24:28 -0700 (PDT) Message-Id: <86f33df1eb043d92cc626092d512aec670c89bdb.1776270259.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "Harald Nordgren via GitGitGadget" Date: Wed, 15 Apr 2026 16:24:19 +0000 Subject: [PATCH v14 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 | 144 +++++++++------------- 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, 359 insertions(+), 141 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=