Skip to content

Commit 48e7f23

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 48e7f23

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

Documentation/git-checkout.adoc

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ 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: it can
24+
**switch branches**, for example with `git checkout <branch>`, and
25+
**restore files from a different version**, for example with
26+
`git checkout <commit> <filename>` or `git checkout <filename>`
27+
28+
See ARGUMENT DISAMBIGUATION below for how Git decides which one to do.
2729
2830
`git checkout [<branch>]`::
2931
To prepare for working on _<branch>_, switch to it by updating
@@ -511,14 +513,17 @@ $ git log -g -2 HEAD
511513
ARGUMENT DISAMBIGUATION
512514
-----------------------
513515
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.
516+
When you run `git checkout <something>`, Git tries to guess whether
517+
`<something>` is intended to be a branch, a commit, or a set of file(s),
518+
and then switches branches, switches commits, or restores the files.
519+
520+
If there's any ambiguity, Git will treat `<something>` as a branch or
521+
commit, but you can use the double dash `--` to force Git to treat the
522+
parameter as a list of files and/or directories, like this:
523+
524+
----------
525+
git checkout -- file.txt
526+
----------
522527
523528
EXAMPLES
524529
--------

0 commit comments

Comments
 (0)