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
120 --help-all
121 Some Git commands take options that are only used for plumbing or
122 that are deprecated, and such options are hidden from the default
123 usage. This option gives the full list of options.
124
125 Negating options
126 Options with long option names can be negated by prefixing --no-. For
127 example, git branch has the option --track which is on by default. You
128 can use --no-track to override that behaviour. The same goes for
129 --color and --no-color.
130
131 Aggregating short options
132 Commands that support the enhanced option parser allow you to aggregate
133 short options. This means that you can for example use git rm -rf or
134 git clean -fdx.
135
136 Abbreviating long options
137 Commands that support the enhanced option parser accepts unique prefix
138 of a long option as if it is fully spelled out, but use this with a
139 caution. For example, git commit --amen behaves as if you typed git
140 commit --amend, but that is true only until a later version of Git
141 introduces another option that shares the same prefix, e.g. git commit
142 --amenity option.
143
144 Separating argument from the option
145 You can write the mandatory option parameter to an option as a separate
146 word on the command line. That means that all the following uses work:
147
148 $ git foo --long-opt=Arg
149 $ git foo --long-opt Arg
150 $ git foo -oArg
151 $ git foo -o Arg
152
153
154 However, this is NOT allowed for switches with an optional value, where
155 the stuck form must be used:
156
157 $ git describe --abbrev HEAD # correct
158 $ git describe --abbrev=10 HEAD # correct
159 $ git describe --abbrev 10 HEAD # NOT WHAT YOU MEANT
160
161
163 Many commands that can work on files in the working tree and/or in the
164 index can take --cached and/or --index options. Sometimes people
165 incorrectly think that, because the index was originally called cache,
166 these two are synonyms. They are not — these two options mean very
167 different things.
168
169 · The --cached option is used to ask a command that usually works on
170 files in the working tree to only work with the index. For example,
171 git grep, when used without a commit to specify from which commit
172 to look for strings in, usually works on files in the working tree,
173 but with the --cached option, it looks for strings in the index.
174
175 · The --index option is used to ask a command that usually works on
176 files in the working tree to also affect the index. For example,
177 git stash apply usually merges changes recorded in a stash entry to
178 the working tree, but with the --index option, it also merges
179 changes to the index as well.
180
181 git apply command can be used with --cached and --index (but not at the
182 same time). Usually the command only affects the files in the working
183 tree, but with --index, it patches both the files and their index
184 entries, and with --cached, it modifies only the index entries.
185
186 See also http://marc.info/?l=git&m=116563135620359 and
187 http://marc.info/?l=git&m=119150393620273 for further information.
188
189 Some other commands that also work on files in the working tree and/or
190 in the index can take --staged and/or --worktree.
191
192 · --staged is exactly like --cached, which is used to ask a command
193 to only work on the index, not the working tree.
194
195 · --worktree is the opposite, to ask a command to work on the working
196 tree only, not the index.
197
198 · The two options can be specified together to ask a command to work
199 on both the index and the working tree.
200
202 Part of the git(1) suite
203
204
205
206Git 2.24.1 12/10/2019 GITCLI(7)