Git: which is the default configured remote for branch?

前端 未结 4 718
半阙折子戏
半阙折子戏 2020-12-02 04:02

I have a remote bare repository hub. I work only in the master branch. The last sentence of this error message below makes me wonder: How do I find

相关标签:
4条回答
  • 2020-12-02 04:10

    You can do it more simply, guaranteeing that your .gitconfig is left in a meaningful state:

    Using Git version v1.8.0 and above

    git push -u hub master when pushing, or:
    git branch -u hub/master

    OR

    (This will set the remote for the currently checked-out branch to hub/master)
    git branch --set-upstream-to hub/master

    OR

    (This will set the remote for the branch named branch_name to hub/master)
    git branch branch_name --set-upstream-to hub/master

    If you're using v1.7.x or earlier

    you must use --set-upstream:
    git branch --set-upstream master hub/master

    0 讨论(0)
  • 2020-12-02 04:18

    Track the remote branch

    You can specify the default remote repository for pushing and pulling using git-branch’s track option. You’d normally do this by specifying the --track option when creating your local master branch, but as it already exists we’ll just update the config manually like so:

    Edit your .git/config

    [branch "master"]
      remote = origin
      merge = refs/heads/master
    

    Now you can simply git push and git pull.

    [source]

    0 讨论(0)
  • 2020-12-02 04:20

    the command to get the effective push remote for the branch, e.g., master, is:

    git config branch.master.pushRemote || git config remote.pushDefault || git config branch.master.remote

    Here's why (from the "man git config" output):

    branch.name.remote [...] tells git fetch and git push which remote to fetch from/push to [...] [for push] may be overridden with remote.pushDefault (for all branches) [and] for the current branch [..] further overridden by branch.name.pushRemote [...]

    For some reason, "man git push" only tells about branch.name.remote (even though it has the least precedence of the three) + erroneously states that if it is not set, push defaults to origin - it does not, it's just that when you clone a repo, branch.name.remote is set to origin, but if you remove this setting, git push will fail, even though you still have the origin remote

    0 讨论(0)
  • 2020-12-02 04:30

    For the sake of completeness: the previous answers tell how to set the upstream branch, but not how to see it.

    There are a few ways to do this:

    git branch -vv shows that info for all branches. (formatted in blue in most terminals)

    cat .git/config shows this also.

    For reference:

    • how do I get git to show me which branches are tracking what?
    • What is this branch tracking (if anything) in git?
    0 讨论(0)
提交回复
热议问题