From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dl1-f51.google.com (mail-dl1-f51.google.com [74.125.82.51]) (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 A7AA23AD501 for ; Wed, 17 Jun 2026 10:57:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781693835; cv=none; b=I20daVMe1fcU7b1g1tYSiToTlN7a3kIv+aXriqOsTy/OCvfnf0O60qHBYIuDi6SeBd67hdeec6tsQk7yxiY6VNFExczTYmzVNBUD+HsvcclEHJ+JfRB2oyucl9QXR0Todjjk2uUisom6rVUSRZTOEGbUoVfc3k5lGjeGdtX/+no= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781693835; c=relaxed/simple; bh=L8Pv6Uxfd/JE+w4ogzgoM2HKOP+8KsDTIWxKWHqSJ4w=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=st6b1ZFWhpptfY8Fr6KxFRF1yukFp0jJIlalWftB1NgLYagLw4Mjtskyjb+UiAej6RH3EUfB49HICvtiKvNnF+2oYF14ghfZ7enZJobVg/QougxyUieBaQ4wbd8w43+k5Fgj8pZGWDxqp13qIGkqmEgVG+v07zDwmf1hVBHKGWw= 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=fX4ihsXk; arc=none smtp.client-ip=74.125.82.51 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="fX4ihsXk" Received: by mail-dl1-f51.google.com with SMTP id a92af1059eb24-13809223fd4so6073826c88.1 for ; Wed, 17 Jun 2026 03:57:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1781693831; x=1782298631; 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=/M63VKDXThXTFulyEGzH3bUA/B7lZ2YMYyk8s9ttUO4=; b=fX4ihsXk85geFoUs9UvWb1QPs58tnkTV4d9UjVLSLLu3X6MhUC7VLSB2hfZara76mP ql4VcU8jBZ+D0eiYMrOTjDcjROZeiswNQvANWZkruYIPZBLWIbIfSqEYEVXrMIkOVI6T ND0PBSj8YUn3NpjjhU/28NKEv2sHeKdI59UjoDFoOBKZo0gWmHUHpusp5TUlb8yEnnz1 t986aH778/8Z78BLJTFYKIiQGOl62hzRYUaF6mzmya3FAGEOpE4jNzljm4B5UIYY08KV SB6ix3cPbXK6a5RvjxF1jrVrFUxRrKj2773xjHGa++XkEsRCxLoj2ID/mrY/unSDto+/ TsQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781693831; x=1782298631; 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=/M63VKDXThXTFulyEGzH3bUA/B7lZ2YMYyk8s9ttUO4=; b=QqQKXCczaBe2NA24caXVglTBN8JPzxswUQCruKMw8SdPlxZ/O1etmI3rDRnLUTh9S0 R/FCNgK6KSsZKgajIY1UrxTHE19EmbpELGZ58kG1/FBYyFMsTUym1oPS2zIHxw+eyJCH a7u7nyUVxwdVmZ8L3BhXPq/c/PkHwQGwOxanC+tTX74WRJz/7WTWijQAuv30awyRsTcd lg7o2GI4m+0TgBhVakjjgtvmV8m/iTi3LWPnHmt2Mobag9j2sFWrU76WP3g2YJh056EL arFAa9qYOyJbFoStRHWlHSlUJC1b9eWbHPfBljzNOHShS7spqGufLLFQ4NQ4rqzd77BZ Tlzw== X-Gm-Message-State: AOJu0YyXbV2z1AuHQ1pxehfUZ1VpvT3nNmPz9K0nrYBFJ9xFAavkGhR7 IdNSMAT2KldyG115u046r/KBl9kPozTTLceJrfgd+8VtjkJOOM3hUcWclSKeXw== X-Gm-Gg: AfdE7clkBLCEqgyxGQAZ0KZjBSsvW4AGnlc1o4buMCHjVBnS1qs+AzTds13mMlr7Rk3 Mj+0a93xYmHaGaRbC0Nn5hIk8DhhAkcYKHukA0hY4F0ajP2/FqBPnI9uMUeUdCpOCAEAl7s2Fmw RNLOYTbM7GrOxuIZMwjw5ZVgxgtfpGreeGzHsJ9p+g2uu3W6bYo76zs/NdOMH4d5iL+Iyt5vZ8D pYFAzbkvtXGzxUnwMtXEWBrNbZID1Zs4ZXZKh6FmTgujF51KbG2EPEdVVIJt28x6TWS2KN2hT7V y5TFEKyD28uoRoXUNMiQYGZ97CDbtEQYcbbo+bSmvqU7S2op0g+MjeEL0eiRwJ9Zah/r8Y1/UF6 Ryad7V64GiD4lqb84YpJSOxkSZGuh8HuUEOH7on65W1SXu7cZqcsWB+DWJ31dRJQkI/kJElIxs4 SdcBipHb6XpMDnwT2V X-Received: by 2002:a05:7300:6146:b0:2ed:e14:e954 with SMTP id 5a478bee46e88-30bca0a6ea3mr1602158eec.30.1781693830460; Wed, 17 Jun 2026 03:57:10 -0700 (PDT) Received: from [127.0.0.1] ([172.185.167.42]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3081e5d0849sm25067775eec.7.2026.06.17.03.57.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Jun 2026 03:57:09 -0700 (PDT) Message-Id: In-Reply-To: References: From: "Lei Zhu via GitGitGadget" Date: Wed, 17 Jun 2026 10:57:08 +0000 Subject: [PATCH v2] checkout/switch: add --create-if-missing option 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: Lei Zhu , Korov 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=