gitcli.txt 9.0 KB

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