From: Harald Nordgren via GitGitGadget Date: Fri, 13 Mar 2026 09:23:38 GMT Subject: [PATCH v3] checkout: add --autostash option for branch switching Message-ID: In-Reply-To: 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=