執行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上是有的。
