1GITCLI(7)                         Git Manual                         GITCLI(7)


6       gitcli - Git command-line interface and conventions


9       gitcli


12       This manual describes the convention used throughout Git CLI.
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:
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.
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.
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.
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.
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:
44               $ git checkout -- *.c
45               $ git checkout -- \*.c
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.
55       ·   Just as the filesystem .  (period) refers to the current directory,
56           using a .  as a repository name in Git (a dot-repository) is a
57           relative path and means your current repository.
59       Here are the rules regarding the "flags" that you should follow when
60       you are scripting Git:
62       ·   it’s preferred to use the non-dashed form of Git commands, which
63           means that you should prefer git foo to git-foo.
65       ·   splitting short options to separate words (prefer git foo -a -b to
66           git foo -ab, the latter may not even work).
68       ·   when a command-line option takes an argument, use the stuck form.
69           In other words, write git foo -oArg instead of git foo -o Arg for
70           short options, and git foo --long-opt=Arg instead of git foo
71           --long-opt Arg for long options. An option that takes optional
72           option-argument must be written in the stuck form.
74       ·   when you give a revision parameter to a command, make sure the
75           parameter is not ambiguous with a name of a file in the work tree.
76           E.g. do not write git log -1 HEAD but write git log -1 HEAD --; the
77           former will not work if you happen to have a file called HEAD in
78           the work tree.
80       ·   many commands allow a long option --option to be abbreviated only
81           to their unique prefix (e.g. if there is no other option whose name
82           begins with opt, you may be able to spell --opt to invoke the
83           --option flag), but you should fully spell them out when writing
84           your scripts; later versions of Git may introduce a new option
85           whose name shares the same prefix, e.g.  --optimize, to make a
86           short prefix that used to be unique no longer unique.


89       From the Git 1.5.4 series and further, many Git commands (not all of
90       them at the time of the writing though) come with an enhanced option
91       parser.
93       Here is a list of the facilities provided by this option parser.
95   Magic Options
96       Commands which have the enhanced option parser activated all understand
97       a couple of magic command-line options:
99       -h
100           gives a pretty printed usage of the command.
102               $ git describe -h
103               usage: git describe [<options>] <commit-ish>*
104                  or: git describe [<options>] --dirty
106                   --contains            find the tag that comes after the commit
107                   --debug               debug search strategy on stderr
108                   --all                 use any ref
109                   --tags                use any tag, even unannotated
110                   --long                always use long format
111                   --abbrev[=<n>]        use <n> digits to display SHA-1s
114       --help-all
115           Some Git commands take options that are only used for plumbing or
116           that are deprecated, and such options are hidden from the default
117           usage. This option gives the full list of options.
119   Negating options
120       Options with long option names can be negated by prefixing --no-. For
121       example, git branch has the option --track which is on by default. You
122       can use --no-track to override that behaviour. The same goes for
123       --color and --no-color.
125   Aggregating short options
126       Commands that support the enhanced option parser allow you to aggregate
127       short options. This means that you can for example use git rm -rf or
128       git clean -fdx.
130   Abbreviating long options
131       Commands that support the enhanced option parser accepts unique prefix
132       of a long option as if it is fully spelled out, but use this with a
133       caution. For example, git commit --amen behaves as if you typed git
134       commit --amend, but that is true only until a later version of Git
135       introduces another option that shares the same prefix, e.g. git commit
136       --amenity option.
138   Separating argument from the option
139       You can write the mandatory option parameter to an option as a separate
140       word on the command line. That means that all the following uses work:
142           $ git foo --long-opt=Arg
143           $ git foo --long-opt Arg
144           $ git foo -oArg
145           $ git foo -o Arg
148       However, this is NOT allowed for switches with an optional value, where
149       the stuck form must be used:
151           $ git describe --abbrev HEAD     # correct
152           $ git describe --abbrev=10 HEAD  # correct
153           $ git describe --abbrev 10 HEAD  # NOT WHAT YOU MEANT


157       Many commands that can work on files in the working tree and/or in the
158       index can take --cached and/or --index options. Sometimes people
159       incorrectly think that, because the index was originally called cache,
160       these two are synonyms. They are not — these two options mean very
161       different things.
163       ·   The --cached option is used to ask a command that usually works on
164           files in the working tree to only work with the index. For example,
165           git grep, when used without a commit to specify from which commit
166           to look for strings in, usually works on files in the working tree,
167           but with the --cached option, it looks for strings in the index.
169       ·   The --index option is used to ask a command that usually works on
170           files in the working tree to also affect the index. For example,
171           git stash apply usually merges changes recorded in a stash entry to
172           the working tree, but with the --index option, it also merges
173           changes to the index as well.
175       git apply command can be used with --cached and --index (but not at the
176       same time). Usually the command only affects the files in the working
177       tree, but with --index, it patches both the files and their index
178       entries, and with --cached, it modifies only the index entries.
180       See also http://marc.info/?l=git&m=116563135620359 and
181       http://marc.info/?l=git&m=119150393620273 for further information.


184       Part of the git(1) suite
188Git 2.18.1                        05/14/2019                         GITCLI(7)