interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / Documentation / gitcli.adoc

278 lines · 11285 bytes Raw Blame History
1gitcli(7)
2=========
3
4NAME
5----
6gitcli - Git command-line interface and conventions
7
8DESCRIPTION
9-----------
10
11This manual describes the convention used throughout Git CLI.
12
13Many commands take revisions (most often "commits", but sometimes
14"tree-ish", depending on the context and command) and paths as their
15arguments. Here are the rules:
16
17 * Options come first and then args.
18 A subcommand may take dashed options (which may take their own
19 arguments, e.g. "--max-parents 2") and arguments. You SHOULD
20 give dashed options first and then arguments. Some commands may
21 accept dashed options after you have already given non-option
22 arguments (which may make the command ambiguous), but you should
23 not rely on it (because eventually we may find a way to fix
24 these ambiguities by enforcing the "options then args" rule).
25
26 * Revisions come first and then paths.
27 E.g. in `git diff v1.0 v2.0 arch/x86 include/asm-x86`,
28 `v1.0` and `v2.0` are revisions and `arch/x86` and `include/asm-x86`
29 are paths.
30
31 * When an argument can be misunderstood as either a revision or a path,
32 they can be disambiguated by placing `--` between them.
33 E.g. `git diff -- HEAD` is, "I have a file called HEAD in my work
34 tree. Please show changes between the version I staged in the index
35 and what I have in the work tree for that file", not "show the difference
36 between the HEAD commit and the work tree as a whole". You can say
37 `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 revisions.
47 You can use `--end-of-options` for this (it also works for commands
48 that do not distinguish between revisions in paths, in which case it
49 is simply an alias for `--`).
50+
51When writing a script that is expected to handle random user-input, it is
52a good practice to make it explicit which arguments are which by placing
53disambiguating `--` 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--------------------------------
60$ git restore *.c
61$ git restore \*.c
62--------------------------------
63+
64The former lets your shell expand the fileglob, and you are asking
65the dot-C files in your working tree to be overwritten with the version
66in the index. The latter passes the `*.c` to Git, and you are asking
67the paths in the index that match the pattern to be checked out to your
68working tree. After running `git add hello.c; rm hello.c`, you will _not_
69see `hello.c` in your working tree with the former, but with the latter
70you will.
71
72 * Just as the filesystem '.' (period) refers to the current directory,
73 using a '.' as a repository name in Git (a dot-repository) is a relative
74 path and means your current repository.
75
76Here are the rules regarding the "flags" that you should follow when you are
77scripting Git:
78
79 * Splitting short options to separate words (prefer `git foo -a -b`
80 to `git foo -ab`, the latter may not even work).
81
82 * When a command-line option takes an argument, use the 'stuck' form. In
83 other words, write `git foo -oArg` instead of `git foo -o Arg` for short
84 options, and `git foo --long-opt=Arg` instead of `git foo --long-opt Arg`
85 for long options. An option that takes optional option-argument must be
86 written in the 'stuck' form.
87
88 * Despite the above suggestion, when Arg is a path relative to the
89 home directory of a user, e.g. `~/directory/file` or `~u/d/f`, you
90 may want to use the separate form, e.g. `git foo --file ~/mine`,
91 not `git foo --file=~/mine`. The shell will expand `~/` in the
92 former to your home directory, but most shells keep the tilde in
93 the latter. Some of our commands know how to tilde-expand the
94 option value even when given in the stuck form, but not all of
95 them do.
96
97 * When you give a revision parameter to a command, make sure the parameter is
98 not ambiguous with a name of a file in the work tree. E.g. do not write
99 `git log -1 HEAD` but write `git log -1 HEAD --`; the former will not work
100 if you happen to have a file called `HEAD` in the work tree.
101
102 * Many commands allow a long option `--option` to be abbreviated
103 only to their unique prefix (e.g. if there is no other option
104 whose name begins with `opt`, you may be able to spell `--opt` to
105 invoke the `--option` flag), but you should fully spell them out
106 when writing your scripts; later versions of Git may introduce a
107 new option whose name shares the same prefix, e.g. `--optimize`,
108 to make a short prefix that used to be unique no longer unique.
109
110
111ENHANCED OPTION PARSER
112----------------------
113From the Git 1.5.4 series and further, many Git commands (not all of them at the
114time of the writing though) come with an enhanced option parser.
115
116Here is a list of the facilities provided by this option parser.
117
118
119Magic Options
120~~~~~~~~~~~~~
121Commands which have the enhanced option parser activated all understand a
122couple of magic command-line options:
123
124-h::
125 gives a pretty printed usage of the command.
126+
127---------------------------------------------
128$ git describe -h
129usage: git describe [<options>] <commit-ish>*
130 or: git describe [<options>] --dirty
131
132 --contains find the tag that comes after the commit
133 --debug debug search strategy on stderr
134 --all use any ref
135 --tags use any tag, even unannotated
136 --long always use long format
137 --abbrev[=<n>] use <n> digits to display SHA-1s
138---------------------------------------------
139+
140Note that some subcommand (e.g. `git grep`) may behave differently
141when there are things on the command line other than `-h`, but `git
142subcmd -h` without anything else on the command line is meant to
143consistently give the usage.
144
145--help-all::
146 Some Git commands take options that are only used for plumbing or that
147 are deprecated, and such options are hidden from the default usage. This
148 option gives the full list of options.
149
150
151Negating options
152~~~~~~~~~~~~~~~~
153Options with long option names can be negated by prefixing `--no-`. For
154example, `git branch` has the option `--track` which is 'on' by default. You
155can use `--no-track` to override that behaviour. The same goes for `--color`
156and `--no-color`.
157
158
159Options trump configuration and environment
160~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
161
162When there is a configuration variable or an environment variable
163that tweak the behaviour of an aspect of a Git command, and also a
164command line option that tweaks the same, the command line option
165overrides what the configuration and/or environment variable say.
166
167For example, the `user.name` configuration variable is used to
168specify the human-readable name used by the `git commit` command to
169record the author and the committer name in a newly created commit.
170The `GIT_AUTHOR_NAME` environment variable, if set, takes precedence
171when deciding what author name to record. The `--author=<author>`
172command line option of the `git commit` command, when given, takes
173precedence over these two sources of information.
174
175
176Aggregating short options
177~~~~~~~~~~~~~~~~~~~~~~~~~
178Commands that support the enhanced option parser allow you to aggregate short
179options. This means that you can for example use `git rm -rf` or
180`git clean -fdx`.
181
182
183Abbreviating long options
184~~~~~~~~~~~~~~~~~~~~~~~~~
185Commands that support the enhanced option parser accepts unique
186prefix of a long option as if it is fully spelled out, but use this
187with a caution. For example, `git commit --amen` behaves as if you
188typed `git commit --amend`, but that is true only until a later version
189of Git introduces another option that shares the same prefix,
190e.g. `git commit --amenity` option.
191
192
193Separating argument from the option
194~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
195You can write the mandatory option parameter to an option as a separate
196word on the command line. That means that all the following uses work:
197
198----------------------------
199$ git foo --long-opt=Arg
200$ git foo --long-opt Arg
201$ git foo -oArg
202$ git foo -o Arg
203----------------------------
204
205However, this is *NOT* allowed for switches with an optional value, where the
206'stuck' form must be used:
207
208----------------------------
209$ git describe --abbrev HEAD # correct
210$ git describe --abbrev=10 HEAD # correct
211$ git describe --abbrev 10 HEAD # NOT WHAT YOU MEANT
212----------------------------
213
214
215Magic filename options
216~~~~~~~~~~~~~~~~~~~~~~
217Options that take a filename allow a prefix `:(optional)`. For example:
218
219----------------------------
220git commit -F :(optional)COMMIT_EDITMSG
221# if COMMIT_EDITMSG does not exist, the above is equivalent to
222git commit
223----------------------------
224
225Like with configuration values, if the named file is missing Git behaves as if
226the option was not given at all. See "Values" in linkgit:git-config[1].
227
228NOTES ON FREQUENTLY CONFUSED OPTIONS
229------------------------------------
230
231Many commands that can work on files in the working tree
232and/or in the index can take `--cached` and/or `--index`
233options. Sometimes people incorrectly think that, because
234the index was originally called cache, these two are
235synonyms. They are *not* -- these two options mean very
236different things.
237
238 * The `--cached` option is used to ask a command that
239 usually works on files in the working tree to *only* work
240 with the index. For example, `git grep`, when used
241 without a commit to specify from which commit to look for
242 strings in, usually works on files in the working tree,
243 but with the `--cached` option, it looks for strings in
244 the index.
245
246 * The `--index` option is used to ask a command that
247 usually works on files in the working tree to *also*
248 affect the index. For example, `git stash apply` usually
249 merges changes recorded in a stash entry to the working tree,
250 but with the `--index` option, it also merges changes to
251 the index as well.
252
253`git apply` command can be used with `--cached` and
254`--index` (but not at the same time). Usually the command
255only affects the files in the working tree, but with
256`--index`, it patches both the files and their index
257entries, and with `--cached`, it modifies only the index
258entries.
259
260See also https://lore.kernel.org/git/7v64clg5u9.fsf@assigned-by-dhcp.cox.net/ and
261https://lore.kernel.org/git/7vy7ej9g38.fsf@gitster.siamese.dyndns.org/ for further
262information.
263
264Some other commands that also work on files in the working tree and/or
265in the index can take `--staged` and/or `--worktree`.
266
267* `--staged` is exactly like `--cached`, which is used to ask a
268 command to only work on the index, not the working tree.
269
270* `--worktree` is the opposite, to ask a command to work on the
271 working tree only, not the index.
272
273* The two options can be specified together to ask a command to work
274 on both the index and the working tree.
275
276GIT
277---
278Part of the linkgit:git[1] suite