git checkout tag, git pull fails in branch

2020-05-11 04:34发布

I have cloned a git repository and then checked out a tag:

# git checkout 2.4.33 -b my_branch

This is OK, but when I try to run git pull in my branch, git spits out this error:

There is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details

git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

git branch --set-upstream new origin/<branch>

I want git pull to only update the master branch and leave my current branch alone (it's a tag anyway). Is something like this possible?

The reason I need this is that I have a automatic script which always git pulls the repository and of course fails because of the error above..

14条回答
唯我独甜
2楼-- · 2020-05-11 04:55

You need to set up your tracking (upstream) for the current branch

git branch --set-upstream master origin/master

Is already deprecated instead of that you can use --track flag

git branch --track master origin/master

I also like the doc reference that @casey notice:

-u <upstream>
  Set up <branchname>'s tracking information so <upstream> is considered  
  <branchname>'s upstream branch. If no <branchname> is specified,  
  then it defaults to the current branch.
查看更多
戒情不戒烟
3楼-- · 2020-05-11 04:55

In order to just download updates:

git fetch origin master

However, this just updates a reference called origin/master. The best way to update your local master would be the checkout/merge mentioned in another comment. If you can guarantee that your local master has not diverged from the main trunk that origin/master is on, you could use git update-ref to map your current master to the new point, but that's probably not the best solution to be using on a regular basis...

查看更多
地球回转人心会变
4楼-- · 2020-05-11 04:55

This command is deprecated: git branch --set-upstream master origin/master

So, when trying to set up tracking, this is the command that worked for me:

git branch --set-upstream-to=origin/master master
查看更多
趁早两清
5楼-- · 2020-05-11 04:56

Switch back to the master branch using

$ git checkout master

and then run the git pull operation

$ git pull origin/master

Afterwards, you can switch back to your my_branch again.

查看更多
\"骚年 ilove
6楼-- · 2020-05-11 05:04

@alesko : it is not possible to only do only git pull after checkout my_branch to update master branch only.
Because git pull will also merge to the current branch -> in your scenario to the my_branch

@Simon: that will do also the push. why is that?

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

and acording to docs:

-u <upstream>
  Set up <branchname>'s tracking information so <upstream> is considered  
  <branchname>'s upstream branch. If no <branchname> is specified,  
  then it defaults to the current branch.
查看更多
forever°为你锁心
7楼-- · 2020-05-11 05:09

If like me you need to do this all the time, you can set up an alias to do it automatically by adding the following to your .gitconfig file:

[alias]
    set-upstream = !git branch --set-upstream-to=origin/`git symbolic-ref --short HEAD`

When you see the message There is no tracking information..., just run git set-upstream, then git push again.

Thanks to https://zarino.co.uk/post/git-set-upstream/

查看更多
登录 后发表回答