Git: which is the default configured remote for branch?

匿名 (未验证) 提交于 2019-12-03 08:33:39

问题:

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 out which is the "default configured remote for your current branch"? And how do I set it?

[myserver]~/progs $ git remote -v hub     ~/sitehub/progs.git/ (fetch) hub     ~/sitehub/progs.git/ (push) [myserver]~/progs $ git branch -r   hub/master [myserver]~/progs $ git remote -v hub     ~/sitehub/progs.git (fetch) hub     ~/sitehub/progs.git (push) [myserver]~/progs $ cat .git/HEAD ref: refs/heads/master [myserver]~/progs $ git pull hub You asked to pull from the remote 'hub', but did not specify a branch. Because this is not the default configured remote for your current branch, you must specify a branch on the command line. 

回答1:

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]



回答2:

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



回答3:

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:



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!