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