From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qv1-f41.google.com (mail-qv1-f41.google.com [209.85.219.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 45649377021 for ; Fri, 13 Mar 2026 09:23:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773393824; cv=none; b=RzzHwtPhpwHHxsLmmWHJBIVW6xmAWk66KmmYnu/oDuOWNaZfJB1Boh9ftyLECYudccR187gy2S8a/6nAvj5uzidfht9fO2r8QGLY1tBalhVWymkXETsV0W3OKmq62Tcank1UzbIWJ9K+rtxt18yXPOSKyYhBtkhfyYld9dDqzZ0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773393824; c=relaxed/simple; bh=VNWncJFblkOExtnlhGpLmiifjgtTefW1bH/Li/MOoak=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=iOZfDbz62LqJmlHr7rpkXJUkpsrT1Jvd8EtMN4aYTW98FB4iF8qFcmAVw8jL6O70gj95i/1LxYukbsRTI5KSSch20kLzEISsvpuADgBDTRTh3Xp5W34MPNipA2JjpemBCgBkFULwPG5wESuekdlMwxyjP4oFJRHPcTL66pxeVYM= 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=XhPqxPsh; arc=none smtp.client-ip=209.85.219.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="XhPqxPsh" Received: by mail-qv1-f41.google.com with SMTP id 6a1803df08f44-89a00e8dc04so19221326d6.0 for ; Fri, 13 Mar 2026 02:23:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773393820; x=1773998620; 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=EQoJJNaEc/AFMhb+4ahCfsAtpFDAD7uhKmGy5Gskfk8=; b=XhPqxPshbx10QhPIZcY/iCvA4RdEAIodh5Jfg0k2/kt5sxO76O9MCdkeoNzkcr1R28 lvY1vcOk1S/OUC0EfP9qm6mujnL64abqGG3e8mhZmKqodwQz/8xqYfwIDm9uWD1+jJvo x6R9+JjsYkDGDn8TTJKQthCUF2JTRQlHGTRPycJVslkAXe2TLJ8kmYas5lUzamhK4Tjg +DuzH/kcK9fJEg014nc8hU3Rgu8uR8Wbb753v8WbmyexCIyI/azhU3Uyy73nnyWhpM0M Tt1O+s9VpQ1V8lPeb/KhB/bGAjR8O5amZBZKe5RjOttlHTrhkHtrHmmuBlQ+NA1z0PR/ PdNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773393820; x=1773998620; 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=EQoJJNaEc/AFMhb+4ahCfsAtpFDAD7uhKmGy5Gskfk8=; b=j15nEKN/6/A5OfdmTP3YT/Yv4eX+zk61jzycCn5dMAkFDJtjHjkw0D9p2AUAIEUCxR gpuzJJjLdgndme3gOwA9wffCtmRljrI/gMdbuUz+EqS+VrAJknbP2xf1j7XQicXi3NjW d+Dlhsxixk5rSf+bO/zGTIxVapXIzKhalTd92XRVa9RmrxXbPFJMFlwv+BV+xqMIvuRO 5XwLWLdlUrQIoz/V431eHEDawjBDvkEA0mzb5R63apfrLjMdQC2HBb/OFYT/zjmvwqq3 4YGLHyVbHo9C3XIfBdHNgaDVpcvr79wwEeP+ZP44dSIYO/zxjQJWqt0jJAX/HpSPx+FE 0KUw== X-Gm-Message-State: AOJu0YyeZbai+nHkph+FeX7pGuslIME2vNNfRtmwc8yEqauUO7PCRxWX cNbfhh12LUOhxNkmcoLHslyjePO9qEo+Q3Opy0i7NhM0tHQPTfuuY/BcFNZDCw== X-Gm-Gg: ATEYQzyiS1ykbDgek1AtD2HJPj60UR8JdOqI+9jNco5R2j47F7lDNk3WUNmWhddQyaM qHjv3o/FX6OrZrclzyYYQvEsFMynP2mHazmdX3om7FvHDTT0BfTYzPF4BunVwRWIvhIX2QjFNhU rnkLktzHJ7CAqaSuIWVZEpH8g9C7R0p4G20gqPi+wBCZrwBTgzCd1R4Y+QdA36gnTnBcDKHRL4E Eobfppwioxq2a94XNoymkoFWF8jGVo4PCr2HJr/OFvlXIREoRcz604PtlHuYv+PuXi9dThj5HSW 57dO4BnJo3S4IIWS/GIoYk9j8AEIQEZfAI4aCLmGc5buA6XNnMZ9M6OdRHyW7frUm58TP6tAexB esaiNHyCtt4IlAi2Fnqfwz8CgUHVwjXT0vtUkxYnFQDyJ1WiOFIQcRh7huwI+o1I9QBTPU7ncXa mu6CltZIQLW8NQL2qEF3RFUmn6vQ== X-Received: by 2002:a05:6214:c47:b0:899:f5d3:321c with SMTP id 6a1803df08f44-89a81d321a1mr37439316d6.10.1773393819599; Fri, 13 Mar 2026 02:23:39 -0700 (PDT) Received: from [127.0.0.1] ([64.236.200.113]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-89a6947c4b8sm48661886d6.20.2026.03.13.02.23.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Mar 2026 02:23:39 -0700 (PDT) Message-Id: In-Reply-To: References: From: "Harald Nordgren via GitGitGadget" Date: Fri, 13 Mar 2026 09:23:38 +0000 Subject: [PATCH v3] checkout: add --autostash option for branch switching 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: Harald Nordgren , Harald Nordgren From: Harald Nordgren When switching branches, local modifications in the working tree can prevent the checkout from succeeding. While "git rebase" and "git merge" already support --autostash to handle this case automatically, "git checkout" and "git switch" require users to manually stash and unstash their changes. Teach "git checkout" and "git switch" to accept --autostash and --no-autostash options that automatically create a temporary stash entry before the branch switch begins and apply it after the switch completes. If the stash application results in conflicts, the stash entry is saved to the stash list so the user can resolve them later. Also add a checkout.autoStash configuration option that enables this behavior by default, which can be overridden with --no-autostash on the command line. Signed-off-by: Harald Nordgren --- checkout: 'autostash' for branch switching Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2234%2FHaraldNordgren%2Fcheckout_autostash-v3 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2234/HaraldNordgren/checkout_autostash-v3 Pull-Request: https://github.com/git/git/pull/2234 Range-diff vs v2: 1: be2b697c54 ! 1: 05f1e53163 checkout: add --autostash option for branch switching @@ Commit message Signed-off-by: Harald Nordgren - ## Documentation/config/checkout.adoc ## -@@ Documentation/config/checkout.adoc: with a small number of cores, the default sequential checkout often performs - better. The size and compression level of a repository might also influence how - well the parallel version performs. - -+`checkout.autoStash`:: -+ When set to true, enable autostash for `git checkout` and -+ `git switch` branch switching operations. When a branch -+ switch would fail because local changes (in the index or the -+ working tree) overlap with paths that differ between the -+ current and target branch, a temporary stash entry is -+ automatically created before the switch and applied after -+ it completes. If the local changes do not overlap with the -+ branch difference, the switch proceeds normally without -+ stashing. -+ This option can be overridden by the `--no-autostash` and -+ `--autostash` options of linkgit:git-checkout[1] and -+ linkgit:git-switch[1]. -+ Defaults to false. -+ - `checkout.thresholdForParallelism`:: - When running parallel checkout with a small number of files, the cost - of subprocess spawning and inter-process communication might outweigh - ## Documentation/git-checkout.adoc ## -@@ Documentation/git-checkout.adoc: When switching branches with `--merge`, staged changes may be lost. - `merge.conflictStyle` configuration variable. Possible values are - `merge` (default), `diff3`, and `zdiff3`. +@@ Documentation/git-checkout.adoc: 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 so you can use `git stash ++ pop` to recover and `git stash drop` when done. + + + 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=