git pull does not specify the error of branch

When executing git pull or git push, sometimes the following error will appear:

$ 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.

Let's first take a look at the current branch status:

$ 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

Although the current linux_c++ branch has the same name as the remote linux_c++ branch, in fact, this branch is not the tracking branch of the origin/linux_c++ branch, so when you directly use git pull to request a new branch, git does not know which branch to pull. .

Therefore, there are two solutions to solve this problem. One is to specify the corresponding remote branch name when git pull or git push, such as:

$ git pull origin linux_c++

Another solution is to set the current branch to track a remote branch. There are two ways to set up an existing branch to track a remote branch:

git branch -u remote-name/branch_name branch_name

or

git branch --set-upstream-to=remote_name/branch_name branch_name

Of course, when you create a local branch, you can directly track it to the remote branch:

git checkout -b local_branch remote_name/remote_branch

Currently our branch is an existing branch, so we can enter:

$ git branch -u origin/linux_c++ linux_c++
Branch linux_c++ set up to track remote branch linux_c++ from origin.

It should be noted that the two options of git branch -u and git branch --set-upstream-to are only available in higher git versions, at least the blogger found that these two options were not available on 1.7.1 , and 1.8.3.1 is there.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326605274&siteId=291194637
Recommended