git checkout 标签,git pull 在分支中失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10147475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:27:03  来源:igfitidea点击:

git checkout tag, git pull fails in branch

gitgit-pullgit-checkout

提问by alesko

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

我已经克隆了一个 git 存储库,然后签出了一个标签:

# git checkout 2.4.33 -b my_branch

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

这没问题,但是当我尝试git pull在我的分支中运行时,git 吐出这个错误:

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>

当前分支没有跟踪信息。请指定您要合并的分支。详见 git-pull(1)

git pull <remote> <branch>

如果您希望为此分支设置跟踪信息,您可以这样做:

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

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

我只想git pull更新主分支而不管我当前的分支(无论如何它都是一个标签)。这样的事情可能吗?

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

我需要这个的原因是我有一个自动脚本,它总是 git pull 存储库,当然由于上面的错误而失败..

采纳答案by Vincent Wen

Edit:For newer versions of Git, --set-upstream masterhas been deprecated, you should use --set-upstream-toinstead:

编辑:对于较新版本的 Git,--set-upstream master已被弃用,您应该--set-upstream-to改用:

git branch --set-upstream-to=origin/master master


As it prompted, you can just run:

根据提示,您可以运行:

git branch --set-upstream master origin/master

After that, you can simply run git pullto update your code.

之后,您可以简单地运行git pull以更新您的代码。

回答by Simon Raik-Allen

I had the same problem and fixed it with this command:

我遇到了同样的问题并使用以下命令修复了它:

$ git push -u origin master

From the help file the -u basically sets the default for pulls:

从帮助文件中 -u 基本上设置了拉取的默认值:

-u, --set-upstream`

  For every branch that is up to date or successfully pushed, add 
  upstream (tracking) reference, used by argument-less git-pull(1) and
  other commands. For more information, see branch.<name>.merge in 
  git-config(1).

回答by navins

Try these commands:

试试这些命令:

git pull origin master
git push -u origin master

回答by cfedermann

Switch back to the master branch using

使用切换回主分支

$ git checkout master

and then run the git pulloperation

然后运行git pull操作

$ git pull origin/master

Afterwards, you can switch back to your my_branchagain.

之后,您可以my_branch再次切换回您的。

回答by Casey

@alesko: it is not possible to only do only git pullafter checkout my_branchto update masterbranch only.
Because git pullwill also merge to the currentbranch -> in your scenario to the my_branch

@alesko:不可能只git pull在结帐后才my_branch更新master分支。
因为git pull也会合并到当前分支 -> 在你的场景中my_branch

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

@Simon:这也会推动。这是为什么?

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

回答by Eric Wang

You might have multiple branch. And your current branch didn't set its upstream in remote.

您可能有多个分支。并且您当前的分支没有将其上游设置为远程。

Steps to fix this:

解决此问题的步骤:

git checkout branch_name
git branch --set-upstream-to=origin/remote_branch_name local_branch_name

e.g.

例如

// this set upstream of local branch develop to remote branch  origin/develop,
git branch --set-upstream-to=origin/develop develop

After doing this, when you do git pull, it pull from specified branch.

执行此操作后,当您执行此操作时git pull,它会从指定的分支中拉出。

回答by Akash Kandpal

You could specify what branch you want to pull:

您可以指定要拉取的分支:

git pull origin master

Or you could set it up so that your local master branch tracks github master branch as an upstream:

或者您可以设置它,以便您的本地 master 分支将 github master 分支作为上游跟踪:

git branch --set-upstream-to=origin/master master
git pull

This branch tracking is set up for you automatically when you clone a repository (for the default branch only), but if you add a remote to an existing repository you have to set up the tracking yourself. Thankfully, the advice given by git makes that pretty easy to remember how to do.

当您克隆存储库(仅适用于默认分支)时,会自动为您设置此分支跟踪,但是如果您将远程添加到现有存储库,则必须自己设置跟踪。值得庆幸的是,git 给出的建议让你很容易记住该怎么做。

--set-upstream is deprecated in git 1.9.x, apparently. Going forward you'd want to use something like

--set-upstream 显然在 git 1.9.x 中已弃用。展望未来,你会想要使用类似的东西

git branch -u origin/master

assuming you've checked out master already. If not, git branch -u origin/master masterwill work

假设您已经签出 master 。如果没有, git branch -u origin/master master将工作

回答by drzymala

First, make sure you are on the right branch.
Then (one time only):

首先,确保你在正确的分支上。
然后(仅一次):

git branch --track

After that this works again:

之后,这再次起作用:

git pull

回答by vanarajcs

Try this

尝试这个

git checkout master

git pull origin master

回答by rjmunro

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 .gitconfigfile:

如果像我一样您需要一直这样做,您可以通过将以下内容添加到您的.gitconfig文件中来设置别名以自动执行此操作:

[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 pushagain.

当您看到消息时There is no tracking information...,只需运行git set-upstream,然后git push再次运行。

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

感谢https://zarino.co.uk/post/git-set-upstream/