Git: 默认配置的远程分支是什么?
Git: 默认配置的远程分支是什么?
我有一个远程的裸仓库hub
。我只在master
分支上工作。
下面错误信息的最后一句让我想知道:我如何找出"当前分支的默认配置远程仓库"是哪个?我如何设置它?
[myserver]~/progs $ git remote -v
hub ~/sitehub/progs.git/ (fetch)
hub ~/sitehub/progs.git/ (push)
[myserver]~/progs $ git branch -r
hub/master
[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.
问题出现的原因是之前的回答告诉了如何设置上游分支,但没有告诉如何查看它。解决方法有几种:
- 使用git branch -vv
命令可以显示所有分支的相关信息(在大多数终端中以蓝色显示)。
- 使用cat .git/config
命令也可以查看这个信息。
参考链接:
- [how do I get git to show me which branches are tracking what?](https://stackoverflow.com/questions/4950725)
- [What is this branch tracking (if anything) in git?](https://stackoverflow.com/questions/3631706/)
问题的出现原因是当创建本地主分支时没有指定默认的远程仓库,导致无法使用git push和git pull命令。
解决方法是手动更新.git/config文件,将默认的远程仓库配置为origin。然后就可以使用git push和git pull命令了。
如果使用git pull hub master
命令,也不会自动设置默认远程仓库,因为这样会导致配置文件被搞乱。
有人问为什么要手动编辑配置文件,而不使用git命令来设置默认的远程仓库。另一个人回答说命令行的方法可以保证.gitconfig文件保持有意义的状态。
有人选择手动编辑配置文件的原因是因为他有很多分支,这样做比为每个分支应用单独的命令节省了时间。
来源:https://gist.github.com/569530
Git提供了一种命令来设置当前配置的远程分支,即set-upstream[-to]
命令。但是,原始问题询问的是默认配置的远程分支。两者并不完全相同。每个分支都有一个当前配置的远程分支,指定该远程分支上哪个分支对应于本地分支。而默认配置的远程分支则确定了在不明确指定远程分支的情况下要推送或拉取的分支。原始问题没有提到如何找出默认的远程分支。
解决方法:
- 使用Git版本v1.8.0及以上:在推送时使用git push -u hub master
命令,或者使用git branch -u hub/master
命令将当前分支配置为hub/master
的远程分支。
- 如果使用的是v1.7.x或更早版本,则需要使用--set-upstream
选项:使用git branch --set-upstream master hub/master
命令将远程分支hub/master
配置为当前分支master
的上游分支。
以上是关于如何设置当前配置的远程分支的解决方法,但没有提到如何找出默认的远程分支。