执行git pull或者git push的时,有时候会出现如下报错:
$ git pull You asked me to pull without telling me which branch you want to merge with, and 'branch.linux_c++.merge' in your configuration file does not tell me, either. Please specify which branch you want to use on the command line and try again (e.g. 'git pull <repository> <refspec>'). See git-pull(1) for details. If you often merge with the same branch, you may want to use something like the following in your configuration file: [branch "linux_c++"] remote = <nickname> merge = <remote-ref> [remote "<nickname>"] url = <url> fetch = <refspec> See git-config(1) for details.
我们先来看看当前分支状态:
$ git branch -av * linux_c++ 584efea add cscope and fix fileencoding problam master ee9d037 v1.0.1: add install.sh remotes/origin/HEAD -> origin/master remotes/origin/linux_c++ 584efea add cscope and fix fileencoding problam remotes/origin/master ee9d037 v1.0.1: add install.sh
当前所在的linux_c++分支虽然与远程linux_c++同名,但实际上,这个分支并不是origin/linux_c++分支的追踪分支,所以当直接用git pull去请求拉新分支的时候,git并不知道应该拉取哪个分支。
因此,解决此问题有两个方案,一个是git pull或者git push的时候,指定相应的远程分支名,如:
$ git pull origin linux_c++
另一个方案则是,设置当前分支追踪某个远程分支。设置现有分支追踪远程分支的方式有两种:
git branch -u remote-name/branch_name branch_name
或者
git branch --set-upstream-to=remote_name/branch_name branch_name
当然,还可以再创建本地分支的时候,直接使其追踪到远程分支:
git checkout -b local_branch remote_name/remote_branch
当前我们的分支是已有分支,那么可以输入:
$ git branch -u origin/linux_c++ linux_c++ Branch linux_c++ set up to track remote branch linux_c++ from origin.
应当注意的一点是:git branch -u 和git branch --set-upstream-to 的两个选项都是要在较高的git版本才有,至少博主在1.7.1上发现没有这两个选项,而1.8.3.1上是有的。