Skip to content

Commit 49f400b

Browse files
committed
doc: git-checkout: clarify restoring files section
From user feedback on this section: 3 users don't know what "tree-ish" means and 3 users don't know what "pathspec" means. One user also says that the section is very confusing and that they don't understand what the "index" is. From conversations on Mastodon, several users said that their impression is that "the index" means the same thing as "HEAD". It would be good to give users a hint that `git checkout file.txt` is not always the same as `git checkout HEAD file.txt`: it's different if there are staged changes to `file.txt`. Replace "pathspec" in the description with "the specified files and/or directories", so that hopefully users can infer that "pathspec" is a way to specify files/directories, say "commit or tree" instead of "tree-ish", and add an example to give a common use case for `git checkout file.txt` (discarding unstaged changes) to hopefully make the section more accessible to users who do not know what the term "index" means. Signed-off-by: Julia Evans <[email protected]>
1 parent e5eb4ef commit 49f400b

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
@@ -73,23 +73,29 @@ Omitting _<branch>_ detaches `HEAD` at the tip of the current branch.
7373
`git checkout <tree-ish> [--] <pathspec>...`::
7474
`git checkout <tree-ish> --pathspec-from-file=<file> [--pathspec-file-nul]`::
7575
76-
Overwrite both the index and the working tree with the
77-
contents at the _<tree-ish>_ for the files that match the pathspec.
76+
Replace the specified files and/or directories with the version from
77+
the given commit or tree and add them to the index
78+
(also known as "staging area").
79+
+
80+
For example, `git checkout main file.txt` will replace `file.txt`
81+
with the version from `main`.
7882
7983
`git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [--] <pathspec>...`::
8084
`git checkout [-f|--ours|--theirs|-m|--conflict=<style>] --pathspec-from-file=<file> [--pathspec-file-nul]`::
8185
82-
Overwrite working tree with the contents in the index for the files
83-
that match the pathspec.
86+
Replace the specified files and/or directories with the version from
87+
the index.
8488
+
85-
The index may contain unmerged entries because of a previous failed merge.
86-
By default, if you try to check out such an entry from the index, the
87-
checkout operation will fail and nothing will be checked out.
88-
Using `-f` will ignore these unmerged entries. The contents from a
89-
specific side of the merge can be checked out of the index by
90-
using `--ours` or `--theirs`. With `-m`, changes made to the working tree
91-
file can be discarded to re-create the original conflicted merge result.
92-
89+
For example, if you check out a commit, edit `file.txt`, and then
90+
decide those changes were a mistake, `git checkout file.txt` will
91+
discard any unstaged changes to `file.txt`.
92+
+
93+
This will fail if the file has a merge conflict and you haven't yet run
94+
`git add file.txt` (or something equivalent) to mark it as resolved.
95+
You can use `-f` to ignore the unmerged files instead of failing, use
96+
`--ours` or `--theirs` to replace them with the version from a specific
97+
side of the merge, or use `-m` to replace them with the original
98+
conflicted merge result.
9399
`git checkout (-p|--patch) [<tree-ish>] [--] [<pathspec>...]`::
94100
This is similar to the previous two modes, but lets you use the
95101
interactive interface to show the "diff" output and choose which

0 commit comments

Comments
 (0)