Skip to content

pr-git-2324/Korov/dev3-v2

tagged this 17 Jun 10:57
From: Korov <korov9.c@gmail.com>

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 <branch>`
behaves like `git switch -c <branch>` or `git checkout -b <branch>`,
including existing `--track` and `--no-track` handling.

When the target branch already exists, `--create-if-missing <branch>`
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 <korov9.c@gmail.com>

Submitted-As: https://lore.kernel.org/git/pull.2324.v2.git.git.1781693828456.gitgitgadget@gmail.com
In-Reply-To: https://lore.kernel.org/git/pull.2324.git.git.1780997009796.gitgitgadget@gmail.com
Assets 2
Loading