]> git.ipfire.org Git - thirdparty/git.git/blob - Documentation/gitcli.txt
path.c: clarify trie_find()'s in-code comment
[thirdparty/git.git] / Documentation / gitcli.txt
1 gitcli(7)
2 =========
3
4 NAME
5 ----
6 gitcli - Git command-line interface and conventions
7
8 SYNOPSIS
9 --------
10 gitcli
11
12
13 DESCRIPTION
14 -----------
15
16 This manual describes the convention used throughout Git CLI.
17
18 Many commands take revisions (most often "commits", but sometimes
19 "tree-ish", depending on the context and command) and paths as their
20 arguments. Here are the rules:
21
22 * Revisions come first and then paths.
23 E.g. in `git diff v1.0 v2.0 arch/x86 include/asm-x86`,
24 `v1.0` and `v2.0` are revisions and `arch/x86` and `include/asm-x86`
25 are paths.
26
27 * When an argument can be misunderstood as either a revision or a path,
28 they can be disambiguated by placing `--` between them.
29 E.g. `git diff -- HEAD` is, "I have a file called HEAD in my work
30 tree. Please show changes between the version I staged in the index
31 and what I have in the work tree for that file", not "show difference
32 between the HEAD commit and the work tree as a whole". You can say
33 `git diff HEAD --` to ask for the latter.
34
35 * Without disambiguating `--`, Git makes a reasonable guess, but errors
36 out and asking you to disambiguate when ambiguous. E.g. if you have a
37 file called HEAD in your work tree, `git diff HEAD` is ambiguous, and
38 you have to say either `git diff HEAD --` or `git diff -- HEAD` to
39 disambiguate.
40 +
41 When writing a script that is expected to handle random user-input, it is
42 a good practice to make it explicit which arguments are which by placing
43 disambiguating `--` at appropriate places.
44
45 * Many commands allow wildcards in paths, but you need to protect
46 them from getting globbed by the shell. These two mean different
47 things:
48 +
49 --------------------------------
50 $ git restore *.c
51 $ git restore \*.c
52 --------------------------------
53 +
54 The former lets your shell expand the fileglob, and you are asking
55 the dot-C files in your working tree to be overwritten with the version
56 in the index. The latter passes the `*.c` to Git, and you are asking
57 the paths in the index that match the pattern to be checked out to your
58 working tree. After running `git add hello.c; rm hello.c`, you will _not_
59 see `hello.c` in your working tree with the former, but with the latter
60 you will.
61
62 * Just as the filesystem '.' (period) refers to the current directory,
63 using a '.' as a repository name in Git (a dot-repository) is a relative
64 path and means your current repository.
65
66 Here are the rules regarding the "flags" that you should follow when you are
67 scripting Git:
68
69 * it's preferred to use the non-dashed form of Git commands, which means that
70 you should prefer `git foo` to `git-foo`.
71
72 * splitting short options to separate words (prefer `git foo -a -b`
73 to `git foo -ab`, the latter may not even work).
74
75 * when a command-line option takes an argument, use the 'stuck' form. In
76 other words, write `git foo -oArg` instead of `git foo -o Arg` for short
77 options, and `git foo --long-opt=Arg` instead of `git foo --long-opt Arg`
78 for long options. An option that takes optional option-argument must be
79 written in the 'stuck' form.
80
81 * when you give a revision parameter to a command, make sure the parameter is
82 not ambiguous with a name of a file in the work tree. E.g. do not write
83 `git log -1 HEAD` but write `git log -1 HEAD --`; the former will not work
84 if you happen to have a file called `HEAD` in the work tree.
85
86 * many commands allow a long option `--option` to be abbreviated
87 only to their unique prefix (e.g. if there is no other option
88 whose name begins with `opt`, you may be able to spell `--opt` to
89 invoke the `--option` flag), but you should fully spell them out
90 when writing your scripts; later versions of Git may introduce a
91 new option whose name shares the same prefix, e.g. `--optimize`,
92 to make a short prefix that used to be unique no longer unique.
93
94
95 ENHANCED OPTION PARSER
96 ----------------------
97 From the Git 1.5.4 series and further, many Git commands (not all of them at the
98 time of the writing though) come with an enhanced option parser.
99
100 Here is a list of the facilities provided by this option parser.
101
102
103 Magic Options
104 ~~~~~~~~~~~~~
105 Commands which have the enhanced option parser activated all understand a
106 couple of magic command-line options:
107
108 -h::
109 gives a pretty printed usage of the command.
110 +
111 ---------------------------------------------
112 $ git describe -h
113 usage: git describe [<options>] <commit-ish>*
114 or: git describe [<options>] --dirty
115
116 --contains find the tag that comes after the commit
117 --debug debug search strategy on stderr
118 --all use any ref
119 --tags use any tag, even unannotated
120 --long always use long format
121 --abbrev[=<n>] use <n> digits to display SHA-1s
122 ---------------------------------------------
123
124 --help-all::
125 Some Git commands take options that are only used for plumbing or that
126 are deprecated, and such options are hidden from the default usage. This
127 option gives the full list of options.
128
129
130 Negating options
131 ~~~~~~~~~~~~~~~~
132 Options with long option names can be negated by prefixing `--no-`. For
133 example, `git branch` has the option `--track` which is 'on' by default. You
134 can use `--no-track` to override that behaviour. The same goes for `--color`
135 and `--no-color`.
136
137
138 Aggregating short options
139 ~~~~~~~~~~~~~~~~~~~~~~~~~
140 Commands that support the enhanced option parser allow you to aggregate short
141 options. This means that you can for example use `git rm -rf` or
142 `git clean -fdx`.
143
144
145 Abbreviating long options
146 ~~~~~~~~~~~~~~~~~~~~~~~~~
147 Commands that support the enhanced option parser accepts unique
148 prefix of a long option as if it is fully spelled out, but use this
149 with a caution. For example, `git commit --amen` behaves as if you
150 typed `git commit --amend`, but that is true only until a later version
151 of Git introduces another option that shares the same prefix,
152 e.g. `git commit --amenity` option.
153
154
155 Separating argument from the option
156 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
157 You can write the mandatory option parameter to an option as a separate
158 word on the command line. That means that all the following uses work:
159
160 ----------------------------
161 $ git foo --long-opt=Arg
162 $ git foo --long-opt Arg
163 $ git foo -oArg
164 $ git foo -o Arg
165 ----------------------------
166
167 However, this is *NOT* allowed for switches with an optional value, where the
168 'stuck' form must be used:
169 ----------------------------
170 $ git describe --abbrev HEAD # correct
171 $ git describe --abbrev=10 HEAD # correct
172 $ git describe --abbrev 10 HEAD # NOT WHAT YOU MEANT
173 ----------------------------
174
175
176 NOTES ON FREQUENTLY CONFUSED OPTIONS
177 ------------------------------------
178
179 Many commands that can work on files in the working tree
180 and/or in the index can take `--cached` and/or `--index`
181 options. Sometimes people incorrectly think that, because
182 the index was originally called cache, these two are
183 synonyms. They are *not* -- these two options mean very
184 different things.
185
186 * The `--cached` option is used to ask a command that
187 usually works on files in the working tree to *only* work
188 with the index. For example, `git grep`, when used
189 without a commit to specify from which commit to look for
190 strings in, usually works on files in the working tree,
191 but with the `--cached` option, it looks for strings in
192 the index.
193
194 * The `--index` option is used to ask a command that
195 usually works on files in the working tree to *also*
196 affect the index. For example, `git stash apply` usually
197 merges changes recorded in a stash entry to the working tree,
198 but with the `--index` option, it also merges changes to
199 the index as well.
200
201 `git apply` command can be used with `--cached` and
202 `--index` (but not at the same time). Usually the command
203 only affects the files in the working tree, but with
204 `--index`, it patches both the files and their index
205 entries, and with `--cached`, it modifies only the index
206 entries.
207
208 See also http://marc.info/?l=git&m=116563135620359 and
209 http://marc.info/?l=git&m=119150393620273 for further
210 information.
211
212 Some other commands that also work on files in the working tree and/or
213 in the index can take `--staged` and/or `--worktree`.
214
215 * `--staged` is exactly like `--cached`, which is used to ask a
216 command to only work on the index, not the working tree.
217
218 * `--worktree` is the opposite, to ask a command to work on the
219 working tree only, not the index.
220
221 * The two options can be specified together to ask a command to work
222 on both the index and the working tree.
223
224 GIT
225 ---
226 Part of the linkgit:git[1] suite