Default remote for git fetch

后端 未结 4 2100
闹比i
闹比i 2021-02-07 07:22

If I am on a local branch that is not tracking any remote branch and I give the command

git fetch

Given I have several remotes defined

4条回答
  •  失恋的感觉
    2021-02-07 07:43

    Here are some aliases which will give strings which can be used programatically:

    branch-name = "symbolic-ref --short HEAD"  # https://stackoverflow.com/a/19585361/5353461
    branch-remote-fetch = !"branch=$(git branch-name \"$1\") && git config branch.\"$branch\".remote || echo origin #"
    branch-remote-push  = !"branch=$(git branch-name \"$1\") && git config branch.\"$branch\".pushRemote || git config remote.pushDefault || git branch-remote-fetch #"
    branch-url-fetch = !"remote=$(git branch-remote-fetch \"$1\") && git remote get-url        \"$remote\" #"  # cognizant of insteadOf
    branch-url-push  = !"remote=$(git branch-remote-push  \"$1\") && git remote get-url --push \"$remote\" #"  # cognizant of pushInsteadOf
    

    If you want to find the remote for another branch, then replace branch-name above with the following with allow for a single argument to be passed:

    branch-current = "symbolic-ref --short HEAD"  # https://stackoverflow.com/a/19585361/5353461
    branch-names = !"[ -z \"$1\" ] && git branch-current 2>/dev/null || git branch --format='%(refname:short)' --contains \"${1:-HEAD}\" #"  # https://stackoverflow.com/a/19585361/5353461
    branch-name = !"br=$(git branch-names \"$1\") && case \"$br\" in *$'\\n'*) printf \"Multiple branches:\\n%s\" \"$br\">&2; exit 1;; esac; echo \"$br\" #"
    

提交回复
热议问题