Skip to content

Commit 79d2d03

Browse files
committed
doc: git-checkout: clarify intro
- Reduce use of jargon ("index", "pathspec", "tree") by using an example, and by mirroring the "switch" and "restore" language from the first line of the man page. - Reference and clarify the ARGUMENT DISAMBIGUATION section, as well as fixing a small error (in "`git checkout abc`, `abc` is a commit, not a `_<tree-ish>_`) Signed-off-by: Julia Evans <[email protected]>
1 parent c44beea commit 79d2d03

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

Documentation/git-checkout.adoc

Lines changed: 18 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,18 @@ $ 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 either switches to that branch or commit or restores the
519+
specified files.
520+
521+
If there's any ambiguity, Git will treat `<something>` as a branch or
522+
commit, but you can use the double dash `--` to force Git to treat the
523+
parameter as a list of files and/or directories, like this:
524+
525+
----------
526+
git checkout -- file.txt
527+
----------
522528
523529
EXAMPLES
524530
--------

0 commit comments

Comments
 (0)