From: Lei Zhu via GitGitGadget Date: Wed, 17 Jun 2026 10:57:08 GMT Subject: [PATCH v2] checkout/switch: add --create-if-missing option Message-ID: In-Reply-To: From: Korov Add a new `--create-if-missing` option to `git switch` and `git checkout` that behaves like an idempotent form of branch switching. Users who often switch between topic branches may not know whether the local branch already exists. Without this option, they need to check for the branch first and then choose between switching to it or creating it. The new option folds that workflow into a single command. When the target branch does not exist, `--create-if-missing ` behaves like `git switch -c ` or `git checkout -b `, including existing `--track` and `--no-track` handling. When the target branch already exists, `--create-if-missing ` switches to it without resetting the branch tip. If `--track` is given, update the branch's upstream configuration using the explicit start-point, or the current branch when no start-point is provided. Fail in detached HEAD state when no start-point is available for tracking setup. For `git checkout`, keep this as a branch operation and reject pathspec usage with `--create-if-missing` to avoid mixing branch switching with path checkout semantics. Document the new option and add tests covering branch creation, existing-branch switching, tracking updates, pathspec rejection, and detached-HEAD failure cases. Signed-off-by: Korov --- checkout/switch: add --create-if-missing option Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2324%2FKorov%2Fdev3-v2 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2324/Korov/dev3-v2 Pull-Request: https://github.com/git/git/pull/2324 Range-diff vs v1: 1: 64a6947ad1 ! 1: 0070592d49 switch: add --ensure option @@ Metadata Author: Korov ## Commit message ## - switch: add --ensure option + checkout/switch: add --create-if-missing option - Add a new `git switch --ensure` (`-e`) option that behaves like an - idempotent form of branch switching. + Add a new `--create-if-missing` option to `git switch` and `git + checkout` that behaves like an idempotent form of branch switching. Users who often switch between topic branches may not know whether the - local branch already exists. Without this option, they need to check - for the branch first and then choose between `git switch ` and - `git switch -c `. The new option folds that workflow into a - single command. + local branch already exists. Without this option, they need to check for + the branch first and then choose between switching to it or creating it. + The new option folds that workflow into a single command. - When the target branch does not exist, `git switch -e ` - behaves like `git switch -c `, including existing `--track` - and `--no-track` handling. + When the target branch does not exist, `--create-if-missing ` + behaves like `git switch -c ` or `git checkout -b `, + including existing `--track` and `--no-track` handling. - When the target branch already exists, `git switch -e ` - switches to it without resetting the branch tip. If `--track` is - given, update the branch's upstream configuration using the explicit - start-point, or the current branch when no start-point is provided. - Fail in detached HEAD state when no start-point is available for - tracking setup. + When the target branch already exists, `--create-if-missing ` + switches to it without resetting the branch tip. If `--track` is given, + update the branch's upstream configuration using the explicit + start-point, or the current branch when no start-point is provided. Fail + in detached HEAD state when no start-point is available for tracking + setup. - Document the new option and add tests covering create-branch tracking, - existing-branch tracking updates, and detached-HEAD failure cases. + For `git checkout`, keep this as a branch operation and reject pathspec + usage with `--create-if-missing` to avoid mixing branch switching with + path checkout semantics. + + Document the new option and add tests covering branch creation, + existing-branch switching, tracking updates, pathspec rejection, and + detached-HEAD failure cases. Signed-off-by: Korov + ## Documentation/git-checkout.adoc ## +@@ Documentation/git-checkout.adoc: git checkout [-q] [-f] [-m] [] + git checkout [-q] [-f] [-m] --detach [] + git checkout [-q] [-f] [-m] [--detach] + git checkout [-q] [-f] [-m] [[-b|-B|--orphan] ] [] ++git checkout [-q] [-f] [-m] --create-if-missing [] + git checkout [--] ... + git checkout --pathspec-from-file= [--pathspec-file-nul] + git checkout [-f|--ours|--theirs|-m|--conflict=