doc: git-checkout: clarify -b and -B

From user feedback: several users reported having trouble understanding
the difference between `-b` and `-B` ("I think it's because my brain
expects it to contrast with `-b`, but instead it starts off explaining
how they're the same").

Also, in `-B`, 2 users can't tell what the branch is reset *to*.

Simplify the sentence structure in the explanations of `-b` and `-B` and
add a little extra information (what `<start-point>` is, what the branch
is reset to).

Splitting up `-b` and `-B` into separate items helps simplify the
sentence structure since there's less "In this case...".

Replace the long "the branch is not reset/created unless "git checkout"
is successful..." with just "will fail", since we should generally
assume that Git will fail operations in a clean way and not leave
operations half-finished, and that cases where it does not fail cleanly
are the exceptions that the documentation should flag.

Signed-off-by: Julia Evans <julia@jvns.ca>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Julia Evans
2025-09-10 19:14:26 +00:00
committed by Junio C Hamano
parent ab215e4a8d
commit 042d6f3402

View File

@@ -47,27 +47,21 @@ $ git checkout -b <branch> --track <remote>/<branch>
Running `git checkout` without specifying a branch has no effect except Running `git checkout` without specifying a branch has no effect except
to print out the tracking information for the current branch. to print out the tracking information for the current branch.
`git checkout (-b|-B) <new-branch> [<start-point>]`:: `git checkout -b <new-branch> [<start-point>]`::
Specifying `-b` causes a new branch to be created as if Create a new branch named _<new-branch>_, start it at _<start-point>_
linkgit:git-branch[1] were called and then checked out. In (defaults to the current commit), and check out the new branch.
this case you can use the `--track` or `--no-track` options, You can use the `--track` or `--no-track` options to set the branch's
which will be passed to `git branch`. As a convenience, upstream tracking information.
`--track` without `-b` implies branch creation; see the
description of `--track` below.
+ +
If `-B` is given, _<new-branch>_ is created if it doesn't exist; otherwise, it This will fail if there's an error checking out _<new-branch>_, for
is reset. This is the transactional equivalent of example if checking out the `<start-point>` commit would overwrite your
+ uncommitted changes.
------------
$ git branch -f <branch> [<start-point>] `git checkout -B <branch> [<start-point>]`::
$ git checkout <branch>
------------ The same as `-b`, except that if the branch already exists it
+ resets `_<branch>_` to the start point instead of failing.
that is to say, the branch is not reset/created unless "git checkout" is
successful (e.g., when the branch is in use in another worktree, not
just the current branch stays the same, but the branch is not reset to
the start-point, either).
`git checkout --detach [<branch>]`:: `git checkout --detach [<branch>]`::
`git checkout [--detach] <commit>`:: `git checkout [--detach] <commit>`::
@@ -157,16 +151,14 @@ of it").
see linkgit:git-branch[1] for details. see linkgit:git-branch[1] for details.
`-B <new-branch>`:: `-B <new-branch>`::
Creates the branch _<new-branch>_, start it at _<start-point>_; The same as `-b`, except that if the branch already exists it
if it already exists, then reset it to _<start-point>_. And then resets `_<branch>_` to the start point instead of failing.
check the resulting branch out. This is equivalent to running
`git branch` with `-f` followed by `git checkout` of that branch;
see linkgit:git-branch[1] for details.
`-t`:: `-t`::
`--track[=(direct|inherit)]`:: `--track[=(direct|inherit)]`::
When creating a new branch, set up "upstream" configuration. See When creating a new branch, set up "upstream" configuration. See
`--track` in linkgit:git-branch[1] for details. `--track` in linkgit:git-branch[1] for details. As a convenience,
--track without -b implies branch creation.
+ +
If no `-b` option is given, the name of the new branch will be If no `-b` option is given, the name of the new branch will be
derived from the remote-tracking branch, by looking at the local part of derived from the remote-tracking branch, by looking at the local part of