Skip to content

Commit 92eec55

Browse files
committed
doc: git-checkout: clarify intro
- Many users do not understand the terms "index" or "pathspec". Clarify in the intro by using an example, so that users can understand the basic idea without learning the full definition of "pathspec". - Use the terminology "Switch" and "Restore" to mirror `git switch` and `git restore` - Reference (and clarify) the ARGUMENT DISAMBIGUATION section Signed-off-by: Julia Evans <[email protected]>
1 parent c44beea commit 92eec55

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

Documentation/git-checkout.adoc

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ git checkout (-p|--patch) [<tree-ish>] [--] [<pathspec>...]
2020
2121
DESCRIPTION
2222
-----------
23-
Updates files in the working tree to match the version in the index
24-
or the specified tree. If no pathspec was given, `git checkout` will
25-
also update `HEAD` to set the specified branch as the current
26-
branch.
23+
`git checkout` has two main modes:
24+
25+
1. **Switch branches**, with `git checkout <branch>`
26+
2. **Restore a different version of a file**, for example with `git
27+
checkout <commit> <filename>` or `git checkout <filename>`
28+
29+
See ARGUMENT DISAMBIGUATION below for how Git decides which one to do.
30+
Here's a description of all of the modes:
2731
2832
`git checkout [<branch>]`::
2933
To prepare for working on _<branch>_, switch to it by updating
@@ -511,14 +515,17 @@ $ git log -g -2 HEAD
511515
ARGUMENT DISAMBIGUATION
512516
-----------------------
513517
514-
When there is only one argument given and it is not `--` (e.g. `git
515-
checkout abc`), and when the argument is both a valid _<tree-ish>_
516-
(e.g. a branch `abc` exists) and a valid _<pathspec>_ (e.g. a file
517-
or a directory whose name is "abc" exists), Git would usually ask
518-
you to disambiguate. Because checking out a branch is so common an
519-
operation, however, `git checkout abc` takes "abc" as a _<tree-ish>_
520-
in such a situation. Use `git checkout -- <pathspec>` if you want
521-
to checkout these paths out of the index.
518+
When you run `git checkout <something>`, Git tries to guess whether
519+
`<something>` is intended to be a branch, a commit, or a set of file(s),
520+
and then switches branches, switches commits, or restores the files.
521+
522+
If there's a conflict, you can use the double dash `--` to distinguish
523+
between branches and files:
524+
525+
* `git checkout <branch> --` will force Git to treat the parameter as a
526+
branch name or commit
527+
* `git checkout -- <pathspec>` will force Git to treat the parameter as
528+
a set of file(s)
522529
523530
EXAMPLES
524531
--------

0 commit comments

Comments
 (0)