From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f178.google.com (mail-qk1-f178.google.com [209.85.222.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 6D8D5289E17 for ; Thu, 9 Apr 2026 13:27:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775741278; cv=none; b=nV3W1zRlM59OM1SyZbuOs0597jBo860PWI7lmESswYBic/3jQXbpMOKfiJkh/azK2RQkZ73d5iaGVLLoA3X3XOA4VoKycKkZSPHDMeiCeEJcGVyEFQch4hzjIZqI3/6G6pZjGnBRACtXlKY8qtvnX5txdZ/S9uLVhkBKpmq6vek= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775741278; c=relaxed/simple; bh=HqetkyX0oSvFA5FEf8KUKRei8l286DG+Zhh+LpdVMkY=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=DbpvyNm5qCGW0JpiWcNbbYsU8eMQt7NCFqUavmP4i6jazoQ6Iv0YJYJhWT/2+O6q5t2MUl41jZKhmHUX0DBnRdX8KNcS86MVvx39vQhkA1oMrT8jJsUuMU+97aPWfyDPyXWsD6bR2jB67+mDcSWDz1BWI7EqEM6GW7quMyeZ2vA= 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=LxRBB8Go; arc=none smtp.client-ip=209.85.222.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="LxRBB8Go" Received: by mail-qk1-f178.google.com with SMTP id af79cd13be357-8cb40149037so79888785a.2 for ; Thu, 09 Apr 2026 06:27:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775741275; x=1776346075; 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=WJ+kjEbg5xAOF7ucV/OlPTK01WVlBeuLKjyEjSrTSck=; b=LxRBB8Gov5TmlLigUaTk4WHPLl0gZp1pGSzYNhSZ8Rd0V8Lq2/LJTofvJzqow2amiK 8tyzyYJRHlxmzodLCnhcfnahabhbLE12glWJiWKAXG486Nld3KONGQX5qTdejtvretPS orgnMhU9sVNarV9iJqriTwEmxfUCft6PeUmciz9QyAq88ZiTT0cu2RlXCmLBeN45t4xd vczCO/jCbR7wt7aoN+32Do+YujeTlkkn1LaLJRaiaxg5tLOzmlLlSq/frpQ0GadaeM2t n4pmS37D9rNbL59iNTke8R4LsQWi18PtlfKLXqvpiqfKIhYKgiow6FDbNpDfCbsj1U5b Q22w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775741275; x=1776346075; 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=WJ+kjEbg5xAOF7ucV/OlPTK01WVlBeuLKjyEjSrTSck=; b=TvUtwxyIaNBfBoUt68oltgeYW2puXWgrFTxbwsnhV94D1QJ3A3rQwxQDpL1WIAmvXi dh8EgzBo6i2GCYP6eHxkHEE3LLygg9jAs8oFmttoBCwyTeguPm/W+79IBZMFbERKujz9 WrAoFtgWmWyemC/8SNdUgbk4mn5diRmfLqY4jNf9xxvfmHx0A9s5KPZmLdWgXX8eyqvu zokfx9TmIuOZgTHRMjgA2CyFqKvxAJn44Uux1ywdWED6gxCgf3kdmn2QgtXC/LbS0JQh kSvOARGfnuZLYVbcpJlXvnaNtuigmXrDhK0tdn0ZtK5SHrPUpazbKqBaNjF3RFZef7xZ 0Jaw== X-Gm-Message-State: AOJu0YzWLdGZAV+rD05Uum0APhaNn4EPp35c6Y+5HY75Hqs5UEiE1mG8 5BA30krp7i/EaK04mz9y3EUCe6GVD2S6Qq1iVeDLAm9rSHo364vlwSn9/bgCrw== X-Gm-Gg: AeBDiesWhoXDTO199R/BB88Y7QmEJEA98oDVTYM6XwODe3kk4Rxrs5TubKkVoAs+eRV CoCaCDZH14RTlX9er5QAgCkv8hOTKAKPElltvLMQCzqzecicJ8j+TQYm8EcaRy3mXgKEr4K9EHn 2iVXkInFEP7gj9V6h1LZPM0Xrz0s01vXqes4Eb/GvqMzhthryb7bJN8IwSuu6rQtwVrLYlM57wA vYZf/ccirPJAAdpzDo+CYgZuTCdkfhe2mdhXRhAaUEJmKNFuomISrbgrD/aJ+O5RyZClyrPoAaf 1pBFyVxW3pV+ancWIh7JDG+jhhrMi2WaNJ1OGnoEtWj+axpqC+pQwxZkGNfXDmTuXoRx1KuFSd7 wTehy3T4OItnW+KqDXtQkOY2PxTRvYtLY54pXddgehWKF8YKKEmzha3S/43u4CBTrTsKUfYEsGA wFcriQ5qk/OoFheL8j3EqjuXCQAM4= X-Received: by 2002:a05:620a:112d:b0:8d7:a2b3:3ab1 with SMTP id af79cd13be357-8d7a2b33c00mr1793288685a.11.1775741274315; Thu, 09 Apr 2026 06:27:54 -0700 (PDT) Received: from [127.0.0.1] ([64.236.200.119]) by smtp.gmail.com with ESMTPSA id af79cd13be357-8d5ce92c4d3sm1275711785a.44.2026.04.09.06.27.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Apr 2026 06:27:53 -0700 (PDT) Message-Id: <1c29e1997132273d58831c470f581fc9ae53198a.1775741265.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "Harald Nordgren via GitGitGadget" Date: Thu, 09 Apr 2026 13:27:45 +0000 Subject: [PATCH v7 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 | 27 ++--- builtin/checkout.c | 138 ++++++++++------------- builtin/stash.c | 30 ++++- sequencer.c | 18 ++- t/t3420-rebase-autostash.sh | 24 +++- t/t7201-co.sh | 188 ++++++++++++++++++++++++++++++++ t/t7600-merge.sh | 2 +- xdiff-interface.c | 12 ++ xdiff-interface.h | 1 + 10 files changed, 360 insertions(+), 138 deletions(-) diff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc index 43ccf47cf6..18022e610e 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=