3

I was reading a book which shows the syntax of git commands as: enter image description here

enter image description here

so my question is, is --global also a switch? Can a switch also a argument?

Updated:

the book said --global is a switch, so I assume -a is also a switch and we can use it as git help -a, but we can't use it as git -a help, which is supposed to be valid according to the syntax?

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • it's an argument, you're telling git what is the target config that will be updated (unlike, for example, when you install a dependency globally) – ffflabs Feb 07 '20 at 04:38
  • @ffflabs so why this argument needs to prefixed as `--global`, but other arguments don't? for example, `--user.name`? –  Feb 07 '20 at 04:44
  • 1
    Cross-site duplicate: https://superuser.com/q/1074694 – tripleee Feb 07 '20 at 05:01
  • Git recently added a `git switch` command, used to switch branches. This is *not* what the OP is asking about. – Keith Thompson Feb 07 '20 at 05:26
  • I think it is more common to call these *options* rather than *switches*, but both terms are in use. – torek Feb 07 '20 at 07:09
  • @slowjams Did my answer clarified your question? `user.name` is an argument (no `--`), not an option or switch `--xx`. SO there is no `--user.name`, because `user.name` is not an option. – VonC Feb 07 '20 at 07:59

2 Answers2

3

The switches are all the parameters passed before any Git actual command: see docs/git

git [--version] [--help] [-C <path>] [-c <name>=<value>]
    [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
    [-p|--paginate|-P|--no-pager] [--no-replace-objects] [--bare]
    [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
    [--super-prefix=<path>]
    <command> [<args>]

You can see all the possible switches before <command> [<args>]

--global is a switch for the git config command as seen here, not for "git" alone.

The term "switch" was introduced in commit 0a8365a; May 2005, Git v0.99

diff-tree: fix and extend argument parsing

We use "--" to mark end of command line switches, not "-".

This is inline with the double-hyphen command-line convention, which is, as I explained here, useful if a non-option argument starts with a hyphen.

                       -- optional separator, followed by arguments
                       v
git -p config --global -- user.name
    ^^        ^^^^^^^^
     |            |_ switch for the git config subcommand.
     |
  switch for the git command
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you for your answer. But in my case, `--global` is passed after the actual command(`config`), isn't that `--global` should be passed before any Git actual command as you mentioned? –  Feb 07 '20 at 05:41
  • @slowjams No it should not: it is the `--global` of `git config` https://git-scm.com/docs/git-config#Documentation/git-config.txt---global. `git --global` does not exist. – VonC Feb 07 '20 at 05:42
  • But isn't that a switch should appear before the command? but for `git config --global`, the switch is after the command? –  Feb 07 '20 at 05:46
  • @slowjams no: it is a switch for the `git config` command, not for `git` itself. `git` itself how no `--global` switch. `git config` does, as per the link I have put: https://git-scm.com/docs/git-config#Documentation/git-config.txt---global – VonC Feb 07 '20 at 05:47
  • you mean `git config` is a command? isn't that a command is only a single word(e.g `config` is a command)? Even though `git config` is a command, the `--global switch` is still after the command, not before the command. –  Feb 07 '20 at 05:51
  • @slowjams yes git config is a command (a sub command of git itself): read https://git-scm.com/docs/git-config#Documentation/git-config.txt – VonC Feb 07 '20 at 06:01
  • OK, so for `git config`, the switch `--global` can be after the actual command? As `git config --global`, clearly the switch is after the `git config` command. I am really confused –  Feb 07 '20 at 06:07
  • Each command has its own set of switch. Git itself has some, git config has some. – VonC Feb 07 '20 at 06:12
  • @slowjams Why did you select an answer which was just a cheap copy of mine? – VonC Feb 08 '20 at 20:51
  • I think you edited your answer later which is more clear to me, thanks –  Feb 11 '20 at 22:41
  • I have selected your answer. Another question is that isn't that single hyphen sometimes the same as single hyphen? like `-g` is the same as `--global`? –  Feb 11 '20 at 22:44
  • @slowjams yes, it is a convention where a single hyphen designates a single letter option, alias for a word option. A word option (like 'global') is prefixed by a double-hypen. See the answers of https://serverfault.com/q/387935/783 or https://askubuntu.com/q/813303/5470 – VonC Feb 12 '20 at 06:54
0

Generally, a command line argument that begins with a hyphen is called a switch. (But even with this "definition", I would consider the word "switch" jargon in this context rather than a technical term.)

A "switch" typically changes a minor aspect of a command or its mode of operation.

Since the git command has many sub-commands, you can have switches that apply to the command in general (these are the switches between git and the subcommand) and switches that apply to the sub-command (these occur after the sub-command).

Given your example

git -p config --global user.name "Rick"
  • -p is a switch that applies to the command in general;

  • --global is a switch that applies to the sub-command.

j6t
  • 9,150
  • 1
  • 15
  • 35