From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dl1-f49.google.com (mail-dl1-f49.google.com [74.125.82.49]) (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 9AC9D29BDB4 for ; Thu, 9 Apr 2026 19:17:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775762246; cv=none; b=d8VpMpOoWRYaVl/91mkhI5h5ZC9d1MtxW/ZRxfOpt2B1mlJbplpBspFj7rj8D2Xkbps7q54S9Lf9Jk6xWhsiuIrD5z/t93u+HeaCB+UCvBoEXD0UnSQs+P+6N8lHpMDm+3sBJNy/SST9igtESPTIDeUtMC9penv8oZYxJs1X7aY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775762246; c=relaxed/simple; bh=bNr5D/bZgh+oty+AaE/K/1v+Llob08fq3yI3osGPFJ8=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=MtJ1ja2dyxiXg3N0i5Nov8BcdffdARLi9x5FRjHEy1JFaamB0cIl+kHzbmqlory5WDNIylDOvW4gYqTqPR1xT6i/d6pkx8HEWtVTp0Eir4ad9OiU5r9huTNqfoUDXDglBhMIOLBqwzudAkscgx72iQYvSx8MtAiOA6CvldlDSZo= 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=bPT3dfF1; arc=none smtp.client-ip=74.125.82.49 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="bPT3dfF1" Received: by mail-dl1-f49.google.com with SMTP id a92af1059eb24-128e4d0cc48so8476430c88.1 for ; Thu, 09 Apr 2026 12:17:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775762243; x=1776367043; 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=Gv2T6uohiu4ljGS3uWyZRJukg7BOF0IMqjKhhcjfZSI=; b=bPT3dfF1cnG0EoclKIAqxzmWwAix20tRZNUI+mZRSM9zOV9Cmas3PumvntlfnSvmSk VV8hbTK+TO73xAuMn1KIRpj5zk9CtgfCtl2D0HcvyiWyMUqzAj2voNr2m/kkLulZdsiz EefBy1pwMES+ICQJlfUYZJMVZX63n7INbbn3leut7dEdJ9i5LdyTBLN5jynzJZMKzPPU dcUd6vtCW7YWmvBZ2B4yi0Ze+ddiqxVSG8+kPNb94xCstF559GFfprGDW6vcY2uvYewp +sbsTP91CChMITGsZ7jQIKm9YzXO1tSYhnyeKydEOATSFLTJswO9t9wUHUk9X3qDKinx g+wA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775762243; x=1776367043; 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=Gv2T6uohiu4ljGS3uWyZRJukg7BOF0IMqjKhhcjfZSI=; b=o5XonHq1fc2OkBYL4xsH/zmjaAO4+pi88MxQd7gYqOJcjDM7NeKuVR2/swf9B1VJ0o +crXerVA4RH4N/sPKlDcJczsVB3mc6STTCM87hr724479DvKEVPcNIK8fkwMQa2CDII+ Ko6cej744ArK8YU2tLyLEH2b6lsTAJiKdrHnKFORSu9dFrgiSFg6kbl3UfLxXFuS81a8 oRkUi+sNZl3T59znARZjttiBrYw5J/UuUlsm18k7ihFms+GfI6cya16LsluE9hYZbEpo 9SaBIwsz2JWXG2LClxzuc06qW2n26sA+S+snL7p/2arRfq6EXpOnTRvT870lONoXOmAt 0dtQ== X-Gm-Message-State: AOJu0YxfheCHzg5JHHozrrbR9dMuOEnHc/AE44lvv4gtiNN9rZpRKrnY g3wvfRuhmBnJMUueoDwH2UE2UhYfnSkdpHbUoP0nqkqIGSlGotCqTq7r0W95UA== X-Gm-Gg: AeBDies1VYXto2ZRLESWxyNZrfXIxo9i40akWOAb3KnFcyscppAWDBtT3ffDmIj+Alh QGDIXugpLS8jIsXqFNR4qwSqysJzPZhtpcwM4yjBcsf95Prv+73jkXeMhK+9gfWlwafyQdXGghu KacI1sANnZgXKl0Iagg4IfDphA9e0LOVSavf06iouDQpoGnanjO3Uc8GHv/kEYFx+haQZ6TGG+k jz8Lu2HTDIeOVdHV+NQ8AYkip7HVgfkVlf1UNXhMBCdRcEtwDODW27ss7q7G8qlpqKZ+BPV3aNd Uv2FIYsmeH026CaP3D7tYbWH3M5FHsaB/IGII+erqDNuVi0mScjO0EhzhbLkP4Zo5ZNPB+NjsWi 0LNK44dyQJvdk5xCOVhW0tiwNKQMrCWq7wQGqLnSmXyjGXeTj7DcR2UzC6xuYQh8BZIHx+BU4HG yStXayfXXW/3Q70XW5K9YGbJ6w5xL9 X-Received: by 2002:a05:7022:609d:b0:12a:6f8b:36a3 with SMTP id a92af1059eb24-12c34e71d4bmr161604c88.6.1775762243249; Thu, 09 Apr 2026 12:17:23 -0700 (PDT) Received: from [127.0.0.1] ([172.182.195.132]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-12c3459fa73sm900402c88.1.2026.04.09.12.17.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Apr 2026 12:17:22 -0700 (PDT) Message-Id: In-Reply-To: References: From: "Harald Nordgren via GitGitGadget" Date: Thu, 09 Apr 2026 19:17:15 +0000 Subject: [PATCH v8 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 , 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 ++++++++++------------ builtin/stash.c | 30 ++++- 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 + 10 files changed, 370 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=