Skip to content
  • Ævar Arnfjörð Bjarmason's avatar
    checkout & worktree: introduce checkout.defaultRemote · 8d7b558b
    Ævar Arnfjörð Bjarmason authored and Junio C Hamano's avatar Junio C Hamano committed
    Introduce a checkout.defaultRemote setting which can be used to
    designate a remote to prefer (via checkout.defaultRemote=origin) when
    running e.g. "git checkout master" to mean origin/master, even though
    there's other remotes that have the "master" branch.
    
    I want this because it's very handy to use this workflow to checkout a
    repository and create a topic branch, then get back to a "master" as
    retrieved from upstream:
    
        (
            cd /tmp &&
            rm -rf tbdiff &&
            git clone git@github.com:trast/tbdiff.git &&
            cd tbdiff &&
            git branch -m topic &&
            git checkout master
        )
    
    That will output:
    
        Branch 'master' set up to track remote branch 'master' from 'origin'.
        Switched to a new branch 'master'
    
    But as soon as a new remote is added (e.g. just to inspect something
    from someone else) the DWIMery goes away:
    
        (
            cd /tmp &&
            rm -rf tbdiff &&
            git clone git@github.com:trast/tbdiff.git &&
            cd tbdiff &&
            git branch -m topic &&
            git remote add avar git@github.com:avar/tbdiff.git &&
            git fetch avar &&
            git checkout master
        )
    
    Will output (without the advice output added earlier in this series):
    
        error: pathspec 'master' did not match any file(s) known to git.
    
    The new checkout.defaultRemote config allows me to say that whenever
    that ambiguity comes up I'd like to prefer "origin", and it'll still
    work as though the only remote I had was "origin".
    
    Also adjust the advice.checkoutAmbiguousRemoteBranchName message to
    mention this new config setting to the user, the full output on my
    git.git is now (the last paragraph is new):
    
        $ ./git --exec-path=$PWD checkout master
        error: pathspec 'master' did not match any file(s) known to git.
        hint: 'master' matched more than one remote tracking branch.
        hint: We found 26 remotes with a reference that matched. So we fell back
        hint: on trying to resolve the argument as a path, but failed there too!
        hint:
        hint: If you meant to check out a remote tracking branch on, e.g. 'origin',
        hint: you can do so by fully qualifying the name with the --track option:
        hint:
        hint:     git checkout --track origin/<name>
        hint:
        hint: If you'd like to always have checkouts of an ambiguous <name> prefer
        hint: one remote, e.g. the 'origin' remote, consider setting
        hint: checkout.defaultRemote=origin in your config.
    
    I considered splitting this into checkout.defaultRemote and
    worktree.defaultRemote, but it's probably less confusing to break our
    own rules that anything shared between config should live in core.*
    than have two config settings, and I couldn't come up with a short
    name under core.* that made sense (core.defaultRemoteForCheckout?).
    
    See also 70c9ac2f ("DWIM "git checkout frotz" to "git checkout -b
    frotz origin/frotz"", 2009-10-18) which introduced this DWIM feature
    to begin with, and 4e853331
    
     ("worktree: make add <path> <branch>
    dwim", 2017-11-26) which added it to git-worktree.
    
    Signed-off-by: default avatarÆvar Arnfjörð Bjarmason <avarab@gmail.com>
    Signed-off-by: default avatarJunio C Hamano <gitster@pobox.com>
    8d7b558b