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 When writing a script that is expected to handle random user-input,
37 it is a good practice to make it explicit which arguments are which
38 by placing disambiguating -- at appropriate places.
39
40 · Many commands allow wildcards in paths, but you need to protect
41 them from getting globbed by the shell. These two mean different
42 things:
43
44 $ git checkout -- *.c
45 $ git checkout -- \*.c
46
47 The former lets your shell expand the fileglob, and you are asking
48 the dot-C files in your working tree to be overwritten with the
49 version in the index. The latter passes the *.c to Git, and you are
50 asking the paths in the index that match the pattern to be checked
51 out to your working tree. After running git add hello.c; rm
52 hello.c, you will not see hello.c in your working tree with the
53 former, but with the latter you will.
54
55 Here are the rules regarding the "flags" that you should follow when
56 you are scripting Git:
57
58 · it’s preferred to use the non dashed form of Git commands, which
59 means that you should prefer git foo to git-foo.
60
61 · splitting short options to separate words (prefer git foo -a -b to
62 git foo -ab, the latter may not even work).
63
64 · when a command line option takes an argument, use the sticked form.
65 In other words, write git foo -oArg instead of git foo -o Arg for
66 short options, and git foo --long-opt=Arg instead of git foo
67 --long-opt Arg for long options. An option that takes optional
68 option-argument must be written in the sticked form.
69
70 · when you give a revision parameter to a command, make sure the
71 parameter is not ambiguous with a name of a file in the work tree.
72 E.g. do not write git log -1 HEAD but write git log -1 HEAD --; the
73 former will not work if you happen to have a file called HEAD in
74 the work tree.
75
76 · many commands allow a long option "--option" to be abbreviated only
77 to their unique prefix (e.g. if there is no other option whose name
78 begins with "opt", you may be able to spell "--opt" to invoke the
79 "--option" flag), but you should fully spell them out when writing
80 your scripts; later versions of Git may introduce a new option
81 whose name shares the same prefix, e.g. "--optimize", to make a
82 short prefix that used to be unique no longer unique.
83
85 From the Git 1.5.4 series and further, many Git commands (not all of
86 them at the time of the writing though) come with an enhanced option
87 parser.
88
89 Here is a list of the facilities provided by this option parser.
90
91 Magic Options
92 Commands which have the enhanced option parser activated all understand
93 a couple of magic command line options:
94
95 -h
96 gives a pretty printed usage of the command.
97
98 $ git describe -h
99 usage: git describe [options] <committish>*
100 or: git describe [options] --dirty
101
102 --contains find the tag that comes after the commit
103 --debug debug search strategy on stderr
104 --all use any ref
105 --tags use any tag, even unannotated
106 --long always use long format
107 --abbrev[=<n>] use <n> digits to display SHA-1s
108
109
110 --help-all
111 Some Git commands take options that are only used for plumbing or
112 that are deprecated, and such options are hidden from the default
113 usage. This option gives the full list of options.
114
115 Negating options
116 Options with long option names can be negated by prefixing --no-. For
117 example, git branch has the option --track which is on by default. You
118 can use --no-track to override that behaviour. The same goes for
119 --color and --no-color.
120
121 Aggregating short options
122 Commands that support the enhanced option parser allow you to aggregate
123 short options. This means that you can for example use git rm -rf or
124 git clean -fdx.
125
126 Abbreviating long options
127 Commands that support the enhanced option parser accepts unique prefix
128 of a long option as if it is fully spelled out, but use this with a
129 caution. For example, git commit --amen behaves as if you typed git
130 commit --amend, but that is true only until a later version of Git
131 introduces another option that shares the same prefix, e.g `git commit
132 --amenity" option.
133
134 Separating argument from the option
135 You can write the mandatory option parameter to an option as a separate
136 word on the command line. That means that all the following uses work:
137
138 $ git foo --long-opt=Arg
139 $ git foo --long-opt Arg
140 $ git foo -oArg
141 $ git foo -o Arg
142
143
144 However, this is NOT allowed for switches with an optional value, where
145 the sticked form must be used:
146
147 $ git describe --abbrev HEAD # correct
148 $ git describe --abbrev=10 HEAD # correct
149 $ git describe --abbrev 10 HEAD # NOT WHAT YOU MEANT
150
151
153 Many commands that can work on files in the working tree and/or in the
154 index can take --cached and/or --index options. Sometimes people
155 incorrectly think that, because the index was originally called cache,
156 these two are synonyms. They are not — these two options mean very
157 different things.
158
159 · The --cached option is used to ask a command that usually works on
160 files in the working tree to only work with the index. For example,
161 git grep, when used without a commit to specify from which commit
162 to look for strings in, usually works on files in the working tree,
163 but with the --cached option, it looks for strings in the index.
164
165 · The --index option is used to ask a command that usually works on
166 files in the working tree to also affect the index. For example,
167 git stash apply usually merges changes recorded in a stash to the
168 working tree, but with the --index option, it also merges changes
169 to the index as well.
170
171 git apply command can be used with --cached and --index (but not at the
172 same time). Usually the command only affects the files in the working
173 tree, but with --index, it patches both the files and their index
174 entries, and with --cached, it modifies only the index entries.
175
176 See also http://marc.info/?l=git&m=116563135620359 and
177 http://marc.info/?l=git&m=119150393620273 for further information.
178
180 Part of the git(1) suite
181
182
183
184Git 1.8.3.1 11/19/2018 GITCLI(7)