0

I couldn't find a difference but could there be one? Between:

  • option A: git pull origin develop --rebase
  • option B: git pull --rebase origin develop

Git docs state :

git pull [<options>] [<repository> [<refspec>…​]] 

So you would think of option B being the correct one, but than..

  • why does option A also works
  • and is there maybe an order by which the options are handled?
7
  • 1
    There is no difference AFAIK. As to why, Git syntax is often very lax, that's why. Commented Oct 22, 2020 at 8:57
  • 2
    No, the position of options is irrelevant in git. Just beware of -- in some commands, because everything after it is considered filepaths. git show --name-only abcd1234 -- path/to/file.txt will not be equivalent to git show abcd1234 -- path/to/file.txt --name-only Commented Oct 22, 2020 at 8:58
  • 1
    @MRedant No. If you want to prevent unexpected mistakes, prefer option B like it is described in the documentation. It is a shared convention between most of the unix command line tools... Commented Oct 22, 2020 at 9:24
  • 2
    @MRedant I'm quite surprised by your comment, since I did not propose any answer nor solution in my first comment. I was merely pointing out that no option should be put after -- which is the symbol for "everything past this are filepaths". And here you do not use -- in your command, sorry if I somehow added confusion. Also, I have no clue why you got that my comment prompted you towards what you call "option A". Commented Oct 22, 2020 at 10:38
  • 1
    @RomainValeri .. sorry for the confusion.. I was refering to the 'No' in your first comment and the other interesting background-info you provided. Commented Oct 22, 2020 at 11:25

1 Answer 1

1

There are competing theories on how rigid one should be with placement of options vs arguments. POSIX utility guidelines prescribe what you called Option B. Git, however, tries to be quite flexible about option placement, even when there are optional or required positional arguments.

These are the general rules for Git options:

  • Options placed between git and the sub-command verb, such as git --no-replace-objects log, are handled by the Git front end (the git program itself). The front end transforms these into environment variable settings, which means that there's always an environment variable you could set instead. For instance:

     git --git-dir=<path> ... 

    and

     GIT_DIR=<path> git ... 

    do the same thing.

    Options placed after the sub-command verb are handled by the sub-command.

  • Single-letter options, such as -v, are preceded by a single hyphen, and can be grouped together. Multi-letter options, such as --name-status, are preceded by a double hyphen. Hence in git diff, there is a -c option ("combined diffs") but also a --cc option ("dense combined diffs"); the single-c option takes a single dash and the double-c option takes a double dash.

  • Anything starting with a hyphen is an option, anywhere, except after a double-hyphen.

The git filter-branch command breaks the last rule a bit because it has to parse its own options, then pass some options to git rev-list. So with filter-branch, you run:

git filter-branch <filter-options> -- <rev-list-options> 

and the <rev-list-options> itself can have options, then a second stand-alone double hyphen --, then path names.1

This all adds up to what you called Option A. But note that if you write your commands with Option B in mind, they all work under Option A.

and is there maybe an order by which the options are handled?

There were, in the past, for some Git sub-commands, some places where order was particularly important. That's fixed now but you will see this remark in the git checkout-index documentation:

The order of the flags used to matter, but not anymore.

In general, however, options are processed in the order they appear, and often, a repeated option will overwrite a previous one. For instance:

git --git-dir=/tmp --git-dir=.git status 

uses .git as the Git directory, not /tmp.


1By can here, I mean is physically able to. You shouldn't actually use this capacity with git filter-branch as it is a form of aiming a gun at your own foot.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.