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.