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