3

In Git, when I am performing a commit with add, I can use

git commit -am "My commit"

When I see examples of git log or in most other cases we use

git log --online

But I do not understand when I'd use one hyphen (-) or two hyphens (--). Can anyone please explain?

u32i64
  • 2,384
  • 3
  • 22
  • 36
riju.srk
  • 209
  • 4
  • 9
  • 1
    https://superuser.com/questions/174564/why-are-there-short-and-long-alternatives-for-command-line-options – melpomene Jul 22 '17 at 07:17
  • 1
    `-am` is equivalent to `-a -m` is equivalent to `--all --message`. – Ry- Jul 22 '17 at 07:28
  • melpomene thanks, that explains a lot. – riju.srk Jul 22 '17 at 12:44
  • 1
    This question isn't related to Git in any way. `-a` is an example of a short option in unix/linux whereas `--oneline` is an example of a long option. `-am` is a combination of `-a and -m`. – Asad Moosvi Jul 22 '17 at 22:38

4 Answers4

7

There is no big difference between - and --. It is just a convention in to use:

  • - when the option is one letter. In this case you can combine then: -am is equivalent to -a -m
  • -- when the option is a word.
  • -- without a word is used to separate options from arguments, that way you can use arguments starting with hyphen without having them interpreted as an option

Some options have the two forms (one letter or one word), some have only one of the two forms. Check the help of each command to find it.

git commit -am "My commit"
# is equivalent to
git commit --all --message "My commit"

Also you can read this question on Super User to learn more about this, as suggested by melpomene.

zigarn
  • 10,892
  • 2
  • 31
  • 45
u32i64
  • 2,384
  • 3
  • 22
  • 36
  • 2
    The function of `--` in Git is a bit more complex than this, [see here](https://stackoverflow.com/questions/13321458/meaning-of-git-checkout-double-dashes). Double dash can serve to distinguish a name from referring to a branch or a file. But often your answer would seem to be correct. – Tim Biegeleisen Jul 22 '17 at 07:24
  • It's still the classic convention: `--` without a word is use to separate option from arguments, that way you can use arguments starting with hyphen without having them interpreted as an option – zigarn Jul 22 '17 at 08:24
4

I've "OBSERVED" that single hyphen is used for single character options and double hyphens are used for multiple character option in git (and several other tools).

This is an indication to the program that whatever it sees after -- should be treated a single option. For example, --online specifies that "online" is an option. But if we use -online, we specify o,n,I,i,n,e are options.

However, this also depends on the tools we use. Most tools follow this pattern, which I think makes the parsing easier. But I've seen few program that can can take in multiple character option with a single hyphen. For example, "java -version" works just fine.

Andrew Nessin
  • 1,206
  • 2
  • 15
  • 22
2

It largely depends on the command you're executing. Let's take git-add as an example.

The possible parameters to it are as follows:

git add [--verbose | -v] [--dry-run | -n] [--force | -f] [--interactive | -i] [--patch | -p]
      [--edit | -e] [--[no-]all | --[no-]ignore-removal | [--update | -u]]
      [--intent-to-add | -N] [--refresh] [--ignore-errors] [--ignore-missing]
      [--chmod=(+|-)x] [--] [<pathspec>…​]

I picked this one in particular because there's a nice mix of short and long-form flags, as well as the double-hyphen in a peculiar place, which I'll get to in a moment.

First and foremost, the key difference between a single hyphen and a double-hyphened flag largely depend on what the command supports. You'll notice in git-add, there are some flags which use double-hyphens as a longer-form command, and the single-hyphen flags as a shorter way to express the same command. In that vein, --verbose and -v accomplish the same thing with different syntaxes.

That's established by convention, and is there for convenience's sake.

Now, to the point of the double-hyphen. In Unix-based systems, a double-hyphen following the flags represents the end of the command, which allows you to specify paths which would conflict with any of the flags present or above. As an example, if you have (for whatever reason) a file named -n, you can add it to Git by:

git add -- -n

So, to summarize:

  • Use a single hyphen for shorthand flags if it's supported.
  • Use a double-hyphen for long-hand flags if you remember the command name, or if there's no alternative (like in --ignore-missing).
  • Use a double-hyphen to signify the end of a command.
Makoto
  • 104,088
  • 27
  • 192
  • 230
0

Using one hyphen - is to use shortcut parameters (one letter for each parameter) for parameters the most commonly used.

That's a Unix convention.

They are intended to be used only by the user in the command line because they are convenient.

Everywhere else (especially in scripts) , prefer the equivalent with 2 hyphens --, which is a good practice, to make it more meaningful for the future one (perhaps you) that will have to read it.

You could see parameters that have a one letter shortcut using the parameter --help on every Unix command.

Philippe
  • 28,207
  • 6
  • 54
  • 78