user7110415
user7110415

Reputation:

What is a "switch" in a Git command?

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?

Upvotes: 3

Views: 350

Answers (2)

VonC
VonC

Reputation: 1326994

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

Upvotes: 3

j6t
j6t

Reputation: 13507

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.

Upvotes: 0

Related Questions