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