Christian Couder | a5af0e2 | 2008-05-02 05:30:47 +0200 | [diff] [blame] | 1 | gitcli(7) |
Pierre Habouzit | 2f7ee08 | 2007-12-13 11:20:01 +0100 | [diff] [blame] | 2 | ========= |
| 3 | |
| 4 | NAME |
| 5 | ---- |
Jason St. John | 06ab60c | 2014-05-21 14:52:26 -0400 | [diff] [blame] | 6 | gitcli - Git command-line interface and conventions |
Pierre Habouzit | 2f7ee08 | 2007-12-13 11:20:01 +0100 | [diff] [blame] | 7 | |
| 8 | SYNOPSIS |
| 9 | -------- |
| 10 | gitcli |
| 11 | |
| 12 | |
| 13 | DESCRIPTION |
| 14 | ----------- |
| 15 | |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 16 | This manual describes the convention used throughout Git CLI. |
Junio C Hamano | d0658ec | 2008-06-25 22:16:37 -0700 | [diff] [blame] | 17 | |
| 18 | Many commands take revisions (most often "commits", but sometimes |
| 19 | "tree-ish", depending on the context and command) and paths as their |
| 20 | arguments. Here are the rules: |
| 21 | |
| 22 | * Revisions come first and then paths. |
| 23 | E.g. in `git diff v1.0 v2.0 arch/x86 include/asm-x86`, |
| 24 | `v1.0` and `v2.0` are revisions and `arch/x86` and `include/asm-x86` |
| 25 | are paths. |
| 26 | |
| 27 | * When an argument can be misunderstood as either a revision or a path, |
Jeff King | 6cf378f | 2012-04-26 04:51:57 -0400 | [diff] [blame] | 28 | they can be disambiguated by placing `--` between them. |
| 29 | E.g. `git diff -- HEAD` is, "I have a file called HEAD in my work |
Junio C Hamano | d0658ec | 2008-06-25 22:16:37 -0700 | [diff] [blame] | 30 | tree. Please show changes between the version I staged in the index |
Øystein Walle | 5fe8f49 | 2014-02-05 23:19:43 +0100 | [diff] [blame] | 31 | and what I have in the work tree for that file", not "show difference |
Junio C Hamano | d0658ec | 2008-06-25 22:16:37 -0700 | [diff] [blame] | 32 | between the HEAD commit and the work tree as a whole". You can say |
Jeff King | 6cf378f | 2012-04-26 04:51:57 -0400 | [diff] [blame] | 33 | `git diff HEAD --` to ask for the latter. |
Junio C Hamano | d0658ec | 2008-06-25 22:16:37 -0700 | [diff] [blame] | 34 | |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 35 | * Without disambiguating `--`, Git makes a reasonable guess, but errors |
Junio C Hamano | d0658ec | 2008-06-25 22:16:37 -0700 | [diff] [blame] | 36 | out and asking you to disambiguate when ambiguous. E.g. if you have a |
| 37 | file called HEAD in your work tree, `git diff HEAD` is ambiguous, and |
Jeff King | 6cf378f | 2012-04-26 04:51:57 -0400 | [diff] [blame] | 38 | you have to say either `git diff HEAD --` or `git diff -- HEAD` to |
Junio C Hamano | d0658ec | 2008-06-25 22:16:37 -0700 | [diff] [blame] | 39 | disambiguate. |
Jeff King | 67feca3 | 2019-08-06 10:40:30 -0400 | [diff] [blame] | 40 | |
| 41 | * Because `--` disambiguates revisions and paths in some commands, it |
| 42 | cannot be used for those commands to separate options and revisions. |
| 43 | You can use `--end-of-options` for this (it also works for commands |
| 44 | that do not distinguish between revisions in paths, in which case it |
| 45 | is simply an alias for `--`). |
Junio C Hamano | 008566e | 2012-09-10 12:47:38 -0700 | [diff] [blame] | 46 | + |
Junio C Hamano | d0658ec | 2008-06-25 22:16:37 -0700 | [diff] [blame] | 47 | When writing a script that is expected to handle random user-input, it is |
| 48 | a good practice to make it explicit which arguments are which by placing |
Jeff King | 6cf378f | 2012-04-26 04:51:57 -0400 | [diff] [blame] | 49 | disambiguating `--` at appropriate places. |
Junio C Hamano | d0658ec | 2008-06-25 22:16:37 -0700 | [diff] [blame] | 50 | |
Junio C Hamano | 8300016 | 2012-09-07 13:49:15 -0700 | [diff] [blame] | 51 | * Many commands allow wildcards in paths, but you need to protect |
| 52 | them from getting globbed by the shell. These two mean different |
| 53 | things: |
| 54 | + |
| 55 | -------------------------------- |
Nguyễn Thái Ngọc Duy | 80f537f | 2019-04-25 16:45:58 +0700 | [diff] [blame] | 56 | $ git restore *.c |
| 57 | $ git restore \*.c |
Junio C Hamano | 8300016 | 2012-09-07 13:49:15 -0700 | [diff] [blame] | 58 | -------------------------------- |
| 59 | + |
| 60 | The former lets your shell expand the fileglob, and you are asking |
| 61 | the dot-C files in your working tree to be overwritten with the version |
| 62 | in the index. The latter passes the `*.c` to Git, and you are asking |
| 63 | the paths in the index that match the pattern to be checked out to your |
| 64 | working tree. After running `git add hello.c; rm hello.c`, you will _not_ |
| 65 | see `hello.c` in your working tree with the former, but with the latter |
| 66 | you will. |
| 67 | |
Philip Oakley | 08f8d5d | 2013-10-15 14:57:42 -0700 | [diff] [blame] | 68 | * Just as the filesystem '.' (period) refers to the current directory, |
| 69 | using a '.' as a repository name in Git (a dot-repository) is a relative |
| 70 | path and means your current repository. |
Pierre Habouzit | 2f7ee08 | 2007-12-13 11:20:01 +0100 | [diff] [blame] | 71 | |
Chris Johnsen | dcb1126 | 2009-03-15 06:30:52 -0500 | [diff] [blame] | 72 | Here are the rules regarding the "flags" that you should follow when you are |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 73 | scripting Git: |
Pierre Habouzit | 2f7ee08 | 2007-12-13 11:20:01 +0100 | [diff] [blame] | 74 | |
Jason St. John | 06ab60c | 2014-05-21 14:52:26 -0400 | [diff] [blame] | 75 | * it's preferred to use the non-dashed form of Git commands, which means that |
Pierre Habouzit | 2f7ee08 | 2007-12-13 11:20:01 +0100 | [diff] [blame] | 76 | you should prefer `git foo` to `git-foo`. |
| 77 | |
| 78 | * splitting short options to separate words (prefer `git foo -a -b` |
| 79 | to `git foo -ab`, the latter may not even work). |
| 80 | |
Jason St. John | 06ab60c | 2014-05-21 14:52:26 -0400 | [diff] [blame] | 81 | * when a command-line option takes an argument, use the 'stuck' form. In |
Pierre Habouzit | 2f7ee08 | 2007-12-13 11:20:01 +0100 | [diff] [blame] | 82 | other words, write `git foo -oArg` instead of `git foo -o Arg` for short |
| 83 | options, and `git foo --long-opt=Arg` instead of `git foo --long-opt Arg` |
| 84 | for long options. An option that takes optional option-argument must be |
Nicolas Vigier | b0d12fc | 2013-10-31 12:08:28 +0100 | [diff] [blame] | 85 | written in the 'stuck' form. |
Pierre Habouzit | 2f7ee08 | 2007-12-13 11:20:01 +0100 | [diff] [blame] | 86 | |
| 87 | * when you give a revision parameter to a command, make sure the parameter is |
| 88 | not ambiguous with a name of a file in the work tree. E.g. do not write |
| 89 | `git log -1 HEAD` but write `git log -1 HEAD --`; the former will not work |
| 90 | if you happen to have a file called `HEAD` in the work tree. |
| 91 | |
Jason St. John | 0b7e4e0 | 2013-11-19 20:34:40 -0500 | [diff] [blame] | 92 | * many commands allow a long option `--option` to be abbreviated |
Junio C Hamano | 9c81990 | 2012-08-16 23:16:22 -0700 | [diff] [blame] | 93 | only to their unique prefix (e.g. if there is no other option |
Jason St. John | 0b7e4e0 | 2013-11-19 20:34:40 -0500 | [diff] [blame] | 94 | whose name begins with `opt`, you may be able to spell `--opt` to |
| 95 | invoke the `--option` flag), but you should fully spell them out |
Junio C Hamano | 9c81990 | 2012-08-16 23:16:22 -0700 | [diff] [blame] | 96 | when writing your scripts; later versions of Git may introduce a |
Jason St. John | 0b7e4e0 | 2013-11-19 20:34:40 -0500 | [diff] [blame] | 97 | new option whose name shares the same prefix, e.g. `--optimize`, |
Junio C Hamano | 9c81990 | 2012-08-16 23:16:22 -0700 | [diff] [blame] | 98 | to make a short prefix that used to be unique no longer unique. |
| 99 | |
Pierre Habouzit | 2f7ee08 | 2007-12-13 11:20:01 +0100 | [diff] [blame] | 100 | |
Junio C Hamano | d0658ec | 2008-06-25 22:16:37 -0700 | [diff] [blame] | 101 | ENHANCED OPTION PARSER |
| 102 | ---------------------- |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 103 | From the Git 1.5.4 series and further, many Git commands (not all of them at the |
Pierre Habouzit | 2f7ee08 | 2007-12-13 11:20:01 +0100 | [diff] [blame] | 104 | time of the writing though) come with an enhanced option parser. |
| 105 | |
Junio C Hamano | 30462a7 | 2012-10-04 10:13:49 -0700 | [diff] [blame] | 106 | Here is a list of the facilities provided by this option parser. |
Pierre Habouzit | 2f7ee08 | 2007-12-13 11:20:01 +0100 | [diff] [blame] | 107 | |
| 108 | |
| 109 | Magic Options |
| 110 | ~~~~~~~~~~~~~ |
| 111 | Commands which have the enhanced option parser activated all understand a |
Jason St. John | 06ab60c | 2014-05-21 14:52:26 -0400 | [diff] [blame] | 112 | couple of magic command-line options: |
Pierre Habouzit | 2f7ee08 | 2007-12-13 11:20:01 +0100 | [diff] [blame] | 113 | |
| 114 | -h:: |
| 115 | gives a pretty printed usage of the command. |
| 116 | + |
| 117 | --------------------------------------------- |
| 118 | $ git describe -h |
Robert P. J. Day | de61305 | 2018-05-24 16:11:39 -0400 | [diff] [blame] | 119 | usage: git describe [<options>] <commit-ish>* |
| 120 | or: git describe [<options>] --dirty |
Pierre Habouzit | 2f7ee08 | 2007-12-13 11:20:01 +0100 | [diff] [blame] | 121 | |
| 122 | --contains find the tag that comes after the commit |
| 123 | --debug debug search strategy on stderr |
Greg Price | 48dfe96 | 2013-02-25 00:34:14 -0500 | [diff] [blame] | 124 | --all use any ref |
| 125 | --tags use any tag, even unannotated |
| 126 | --long always use long format |
| 127 | --abbrev[=<n>] use <n> digits to display SHA-1s |
Pierre Habouzit | 2f7ee08 | 2007-12-13 11:20:01 +0100 | [diff] [blame] | 128 | --------------------------------------------- |
Junio C Hamano | 1ff466c | 2020-02-27 08:10:20 -0800 | [diff] [blame] | 129 | + |
| 130 | Note that some subcommand (e.g. `git grep`) may behave differently |
| 131 | when there are things on the command line other than `-h`, but `git |
| 132 | subcmd -h` without anything else on the command line is meant to |
| 133 | consistently give the usage. |
Pierre Habouzit | 2f7ee08 | 2007-12-13 11:20:01 +0100 | [diff] [blame] | 134 | |
| 135 | --help-all:: |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 136 | Some Git commands take options that are only used for plumbing or that |
Pierre Habouzit | 2f7ee08 | 2007-12-13 11:20:01 +0100 | [diff] [blame] | 137 | are deprecated, and such options are hidden from the default usage. This |
| 138 | option gives the full list of options. |
| 139 | |
| 140 | |
| 141 | Negating options |
| 142 | ~~~~~~~~~~~~~~~~ |
Chris Johnsen | dcb1126 | 2009-03-15 06:30:52 -0500 | [diff] [blame] | 143 | Options with long option names can be negated by prefixing `--no-`. For |
| 144 | example, `git branch` has the option `--track` which is 'on' by default. You |
| 145 | can use `--no-track` to override that behaviour. The same goes for `--color` |
| 146 | and `--no-color`. |
Pierre Habouzit | 2f7ee08 | 2007-12-13 11:20:01 +0100 | [diff] [blame] | 147 | |
| 148 | |
| 149 | Aggregating short options |
| 150 | ~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 151 | Commands that support the enhanced option parser allow you to aggregate short |
Chris Johnsen | dcb1126 | 2009-03-15 06:30:52 -0500 | [diff] [blame] | 152 | options. This means that you can for example use `git rm -rf` or |
| 153 | `git clean -fdx`. |
Pierre Habouzit | 2f7ee08 | 2007-12-13 11:20:01 +0100 | [diff] [blame] | 154 | |
| 155 | |
Junio C Hamano | 30462a7 | 2012-10-04 10:13:49 -0700 | [diff] [blame] | 156 | Abbreviating long options |
| 157 | ~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 158 | Commands that support the enhanced option parser accepts unique |
| 159 | prefix of a long option as if it is fully spelled out, but use this |
| 160 | with a caution. For example, `git commit --amen` behaves as if you |
| 161 | typed `git commit --amend`, but that is true only until a later version |
| 162 | of Git introduces another option that shares the same prefix, |
Jason St. John | 0b7e4e0 | 2013-11-19 20:34:40 -0500 | [diff] [blame] | 163 | e.g. `git commit --amenity` option. |
Junio C Hamano | 30462a7 | 2012-10-04 10:13:49 -0700 | [diff] [blame] | 164 | |
| 165 | |
Pierre Habouzit | 2f7ee08 | 2007-12-13 11:20:01 +0100 | [diff] [blame] | 166 | Separating argument from the option |
| 167 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 168 | You can write the mandatory option parameter to an option as a separate |
| 169 | word on the command line. That means that all the following uses work: |
| 170 | |
| 171 | ---------------------------- |
| 172 | $ git foo --long-opt=Arg |
| 173 | $ git foo --long-opt Arg |
| 174 | $ git foo -oArg |
| 175 | $ git foo -o Arg |
| 176 | ---------------------------- |
| 177 | |
Ralf Wildenhues | f1cdcc7 | 2008-01-07 22:43:27 +0100 | [diff] [blame] | 178 | However, this is *NOT* allowed for switches with an optional value, where the |
Nicolas Vigier | b0d12fc | 2013-10-31 12:08:28 +0100 | [diff] [blame] | 179 | 'stuck' form must be used: |
Pierre Habouzit | 2f7ee08 | 2007-12-13 11:20:01 +0100 | [diff] [blame] | 180 | ---------------------------- |
| 181 | $ git describe --abbrev HEAD # correct |
| 182 | $ git describe --abbrev=10 HEAD # correct |
| 183 | $ git describe --abbrev 10 HEAD # NOT WHAT YOU MEANT |
| 184 | ---------------------------- |
| 185 | |
| 186 | |
Nanako Shiraishi | aa0c1f2 | 2008-07-01 23:02:40 +0900 | [diff] [blame] | 187 | NOTES ON FREQUENTLY CONFUSED OPTIONS |
| 188 | ------------------------------------ |
| 189 | |
| 190 | Many commands that can work on files in the working tree |
| 191 | and/or in the index can take `--cached` and/or `--index` |
| 192 | options. Sometimes people incorrectly think that, because |
| 193 | the index was originally called cache, these two are |
| 194 | synonyms. They are *not* -- these two options mean very |
| 195 | different things. |
| 196 | |
| 197 | * The `--cached` option is used to ask a command that |
| 198 | usually works on files in the working tree to *only* work |
| 199 | with the index. For example, `git grep`, when used |
| 200 | without a commit to specify from which commit to look for |
| 201 | strings in, usually works on files in the working tree, |
| 202 | but with the `--cached` option, it looks for strings in |
| 203 | the index. |
| 204 | |
| 205 | * The `--index` option is used to ask a command that |
| 206 | usually works on files in the working tree to *also* |
| 207 | affect the index. For example, `git stash apply` usually |
Liam Beguin | e01db91 | 2017-06-17 18:30:50 -0400 | [diff] [blame] | 208 | merges changes recorded in a stash entry to the working tree, |
Nanako Shiraishi | aa0c1f2 | 2008-07-01 23:02:40 +0900 | [diff] [blame] | 209 | but with the `--index` option, it also merges changes to |
| 210 | the index as well. |
| 211 | |
| 212 | `git apply` command can be used with `--cached` and |
| 213 | `--index` (but not at the same time). Usually the command |
| 214 | only affects the files in the working tree, but with |
| 215 | `--index`, it patches both the files and their index |
| 216 | entries, and with `--cached`, it modifies only the index |
| 217 | entries. |
| 218 | |
Denton Liu | dcee037 | 2019-12-02 11:26:18 -0800 | [diff] [blame] | 219 | See also https://lore.kernel.org/git/7v64clg5u9.fsf@assigned-by-dhcp.cox.net/ and |
| 220 | https://lore.kernel.org/git/7vy7ej9g38.fsf@gitster.siamese.dyndns.org/ for further |
Nanako Shiraishi | aa0c1f2 | 2008-07-01 23:02:40 +0900 | [diff] [blame] | 221 | information. |
| 222 | |
Nguyễn Thái Ngọc Duy | 46e91b6 | 2019-04-25 16:45:45 +0700 | [diff] [blame] | 223 | Some other commands that also work on files in the working tree and/or |
| 224 | in the index can take `--staged` and/or `--worktree`. |
| 225 | |
| 226 | * `--staged` is exactly like `--cached`, which is used to ask a |
| 227 | command to only work on the index, not the working tree. |
| 228 | |
| 229 | * `--worktree` is the opposite, to ask a command to work on the |
| 230 | working tree only, not the index. |
| 231 | |
| 232 | * The two options can be specified together to ask a command to work |
| 233 | on both the index and the working tree. |
| 234 | |
Pierre Habouzit | 2f7ee08 | 2007-12-13 11:20:01 +0100 | [diff] [blame] | 235 | GIT |
| 236 | --- |
Christian Couder | 9e1f0a8 | 2008-06-06 09:07:32 +0200 | [diff] [blame] | 237 | Part of the linkgit:git[1] suite |