From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dl1-f41.google.com (mail-dl1-f41.google.com [74.125.82.41]) (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 5FCCB32692B for ; Tue, 28 Apr 2026 18:39:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777401566; cv=none; b=r6REppc5Ja72qc6sxx/K8F8FNwSpNKl7gZBEXSxFA1uI1btp1Fxei/VcfRwrNc2I2FvYoXjLYtS3DxKcwHYdVHyvy5zvIhilYzlMUKddSF+Ym/RNXLi+h/wGscawbzzjI2lNhI8ayCMVOrO1Giop3Abl6wYytP+afo60YFB1Uk8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777401566; c=relaxed/simple; bh=Jm+Jy+VZbEdgRncRZFMF8FV4QRTG5swdIazjOALEJVk=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=N6+YwFRTFdlav43DXkm9tj6rXH6LeWRyq3AZP3CUCh1GlJt0wYiedJV/MMI74muhFcKU2yPxuKFLXAhUe4VjLgchKdH3jh8e3aY1Qnx8RZtFKhitXiIlurqteUcmI8USvGjX1k1JqY2z5IkCo1w8Q99QVXHpKcTSs4Rc3fSY7v8= 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=Q5gNH7cQ; arc=none smtp.client-ip=74.125.82.41 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="Q5gNH7cQ" Received: by mail-dl1-f41.google.com with SMTP id a92af1059eb24-12c19d23b19so15375558c88.0 for ; Tue, 28 Apr 2026 11:39:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777401563; x=1778006363; 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=rdc13SioscLVSpbe6EqUjgeUMHmGlh1vPjjavadQaHM=; b=Q5gNH7cQ6Kj1+6+Xio23kwJzRceHMVl1fPzUM1c4yZM4F36S+w8XxMwB3cLu9Q9iBR ubTLwmI6offexbQUo0SKhsXeJ1k8V6T3Vqdq7P6t6ex9295jhL3sfGYdYYiEK++PeW62 EBqbpsfkQtburJjnt2jsVk6VSf9sSCQYFpqxb7N77IYQhduBh+4AGJGmaU+mhzrAkX7Q FniuOixP4vcACpP0r33eP8yA4N6Fmnw3hyGc1arEU3LAPsbl7q4NNgv/tw2S5/VZ6ILm 9aLBVQvG3Apd2dcL/yaPBb6gWyhKL4CG8P8cHVmPN9T/dGfU/ncsBuu6MY7+bZHN2nP/ AGFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777401563; x=1778006363; 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=rdc13SioscLVSpbe6EqUjgeUMHmGlh1vPjjavadQaHM=; b=LEDYbvk2Ej0qCJ82FOiGq7k/mbIMySocDXSDXzE2Tbm3CoejT9ECaqAy7oKK4NJCfo sEL8KhplkFFR+Je3MdKy2i7lATlZ50PhklOHtbzoBhbeY+YYSAQQFNdjYWeqZ+iNqbVu /EcWbN/QL5v8MpqprjviKogZuX6IZe/uvlqzYjy2jeWS6OeKUh0dV1WXqk4xdUYFAGoO 1zxoR6NCWzS/V04IYYxWl+g4W1sjzgO/uCf8wcwYVbW5b84o8LxZK6d6iOV67kDh3FnO IePzTWeaRXbq7/6XKXJFOwH6Lyly4FcWeIz7HwhzbagW/9JEXGmqlJOGG9nX83atfF7l iB5Q== X-Gm-Message-State: AOJu0YzwZmeXuj+lAu8VSX31VJ+ABsGIJKqYpsm6kJJVacBvr+Nfi2u8 PRa1IUe2Aed4VQq1y3ABE5jWPgmpx7HXWLiECT7TCLu1KsiAumthJNZDocuJ1Jj/ X-Gm-Gg: AeBDievIE8L/D11QN+S0Y7k/BMD55jmFKCk0w+ZFw3DjDWh7eNYT/72wvkHIHTU9BAH 01/6QhFLCp+4UpwIw5FNI+UCL7WXCcEVi/Hu1dSJAQOwysOXkoKEbWJqxXbhNVTFfR6P7DJIZ6A heL8DamK4zemS/NfqkKMQfLkosZA4yqVxTrKHPJ7NCxRadKAWyL9RBnZ5mRZPaakyigvux6fQpE YQGGckVRD9KpRbRfNjnQd79atq1uhGa6nsDSydh/r3Q7jER7h54STvF2JYcUFja6JStJHUWsUAp RBnxhsc6XejgT4oreEBLLJNX2AoPt2GNGKaPYE775eaSz7RdYC9n+UIaTRBYSebEG3M9ShepVWQ Ya48zv/L8tjJrsfpqkz9tuGyqWwerYV71LFWb6BGIoL39MwakE2s1PRxX4f0tghBKsqQRQke7jY FDo+WbIqyRVx24EV2q7neNV3rwaZWYR7DyK+nHWHv+I5Nddug= X-Received: by 2002:a05:7022:6b98:b0:12d:ce34:3f97 with SMTP id a92af1059eb24-12de2a69acfmr165428c88.31.1777401562984; Tue, 28 Apr 2026 11:39:22 -0700 (PDT) Received: from [127.0.0.1] ([52.161.83.199]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-12ddd9b1bfcsm2588587c88.12.2026.04.28.11.39.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Apr 2026 11:39:22 -0700 (PDT) Message-Id: <07d25fda91a1bbb3e13005d5462d1879303b15c0.1777401552.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "Harald Nordgren via GitGitGadget" Date: Tue, 28 Apr 2026 18:39:12 +0000 Subject: [PATCH v16 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 | 55 ++++++----- Documentation/git-switch.adoc | 36 +++---- builtin/checkout.c | 161 ++++++++++++++------------------ sequencer.c | 14 ++- t/t3420-rebase-autostash.sh | 16 ++-- t/t7201-co.sh | 71 +++++++++++++- t/t7600-merge.sh | 3 +- xdiff-interface.c | 12 +++ xdiff-interface.h | 1 + 9 files changed, 219 insertions(+), 150 deletions(-) diff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc index 43ccf47cf6..a8b3b8c2e2 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=