Skip to content

Commit 9b28903

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 863d01d commit 9b28903

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

0 commit comments

Comments
 (0)