ChatGPT解决这个技术问题 Extra ChatGPT

当前分支没有跟踪信息

我从相对较短的时间内开始使用 github,并且一直使用客户端来执行提交和拉取操作。我昨天决定从 git bash 尝试一下,我成功地创建了一个新的 repo 并提交了文件。

今天我从另一台计算机对存储库进行了更改,我已经提交了更改,现在我回到家并执行了 git pull 来更新我的本地版本,我得到了这个:

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 develop origin/<branch>

这个 repo 的唯一贡献者是我,没有分支(只有一个大师)。我在 Windows 上,我已经从 git bash 执行了拉取操作:

https://i.stack.imgur.com/AeyH8.png

混帐状态:

$ git status
# On branch master
nothing to commit, working directory clean

git 分支:

$ git branch
* master

我究竟做错了什么?

好的,git remote -v 怎么样?那说明什么?
如果您在执行 git pull 时没有 cd 进入克隆的 repo 目录,也可能会出现该错误。
使用 Git 2.24,2019 年第四季度,git pull --set-upstream-to=origin/master master 是可能的。请参阅my answer below
使用 git pull origin master --allow-unrelated-histories educative.io/edpresso/…

C
ComputerDruid

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

git pull origin master

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

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

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


完美的!有效。那么发生了什么事是默认的“git pull”没有“默认分支”?这就是为什么有错误?
--set-upstream 显然在 git 1.9.x 中已被弃用。假设您已经签出 master ,那么接下来您可能想要使用 git branch -u origin/master 之类的东西。如果没有,git branch -u origin/master master 将起作用。
@BartRead 更新它以使用新的更清晰的 --set-upstream-to= 语法。 (这是 --set-upstream 的替代品)
这似乎是一种令人遗憾的情况——我们真的需要为每个分支手动设置吗?为什么我们不能将 origin 设置为默认远程,然后将每个 push/pull 默认设置为具有相同名称的 origin 分支?这有那么难吗?
啊,对我来说关键部分是,如果您删除并重新创建一个遥控器,那么原始 fetch 设置的跟踪就会丢失。因此需要重新设置。 :)
R
Rob Bednark

请参阅:git checkout tag, git pull fails in branch

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

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

当您看到消息 There is no tracking information... 时,运行:

 git set-upstream
 git push

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


这是一种在一行中将其添加到全局 .gitconfig 的方法(可以相应地针对本地或其他配置进行修改):git config --global alias.set-upstream '!git branch --set-upstream-to=origin/$(git symbolic-ref --short HEAD)'
a
aerin

ComputerDruid 的回答很好,但我认为没有必要手动设置上游,除非你愿意。我添加这个答案是因为人们可能认为这是一个必要的步骤。

如果您指定要拉取的遥控器,则此错误将消失,如下所示:

git pull origin master

请注意,origin 是远程的名称,master 是分支名称。

1) 如何查看遥控器名称

git remote -v

2)如何查看存储库中可用的分支。

git branch -r

C
Coconut

步骤1

$ git pull
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-to=<remote>/<branch> master

第2步

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

第 3 步

$ git pull
Already up to date.

I
IMSoP

对于任何想了解为什么会发生这种情况的人,有一些相关的概念:

一个 git 存储库可以有零个或多个“远程”,它们被命名为指向其他存储库的链接,通常在某个中央服务器上。您可以使用“git remote -v”列出它们

如果您从另一台服务器克隆存储库,将为您设置一个名为“origin”的默认远程。

git push 和 git pull 的完整语法是指定远程名称和该远程上的分支。

本地分支可以与远程分支相关联,因此您不必在每次拉取或推送时都键入它。

如果您使用“git switch branchname”或“git switch -u origin/branchname”从远程存储库签出分支,将为您设置关联。 (在这种情况下,“git switch”与“git checkout”相同)

如果你在本地创建一个分支,然后用“git push -u origin”推送它,它会设置关联。

但是如果你在初始拉或推中没有包含“-u”,则不会存储关联,所以你必须每次都具体。

正如其他答案指出的那样,解决方案是设置与“git branch --set-upstream-to=origin/branchname”的关联,其中“origin”是远程的名称,“branchname”是分支的名称在远程服务器上调用。这可能与它在本地的名称相同,但不一定如此。


我不明白,当您将新分支推送到远程时,始终设置上游分支的默认行为有什么缺点?您没有解释为什么每次都需要明确地管理它。为什么没有为此添加配置选项?
@warvariuc 那是另一种“为什么”,我没有资格回答,因为我从未参与过 git 的开发。部分答案可能是 git 被设计为完全去中心化的,因此在像 Github 这样的地方拥有一个单一的“中央”副本并不是他们最初优化的目标。如果您有三个不同的遥控器,您可能会将同一个分支推送到,将跟踪设置为您首先推送到的任何一个都会非常混乱。
感谢你的回答。在我与 Git 和不同团队合作的 10 年中,我很少有几个遥控器。也没有配置行为的选项很奇怪。所以我试图找出说服开发人员添加配置选项的原因和方法......
M
Maddu Swaroop

发生这种情况是因为当前分支没有跟踪远程分支。所以你可以用两种方法来做。

使用特定分支名称拉取 git pull origin master 或者您可以将特定分支跟踪到本地分支。 git branch --set-upstream-to=origin/


为什么我关心“当前分支在远程分支上没有跟踪”
i
imapotatoe123

我正在尝试上述示例,但无法让它们与我在另一台计算机上创建的(非主)分支同步。作为背景,我在计算机 A(git v 1.8)上创建了这个存储库,然后将存储库克隆到计算机 B(git 2.14)上。我在 comp B 上进行了所有更改,但是当我尝试将更改拉到计算机上时,AI 无法这样做,得到与上述相同的错误。与上述解决方案类似,我必须这样做:

git branch --set-upstream-to=origin/<my_branch_name> 
git pull

略有不同,但希望对某人有所帮助


这对我有用;我还使用了在另一台计算机上设置的非主分支。然后在 Git Bash 中使用这个命令,我就可以得到我的更改了 :)
? -> 你的意思是 branch_name
很有魅力,谢谢!
R
Ron Reynolds

我经常遇到这个确切的消息,因为我通过 git checkout -b <feature-branch-name> 创建了本地分支,而没有先创建远程分支。

在所有工作完成并在本地提交后,修复是 git push -u,它创建了远程分支,推送了我的所有工作,然后是合并请求 URL。


为了它的价值,我必须运行 git push -u origin <my-feature-branch-name> 才能创建远程分支并推送我的工作
M
Mahesh Jamdade

尝试

   git pull --rebase

希望这个答案有助于最初在这里回答https://stackoverflow.com/a/55015370/8253662


G
Gopalakrishnan T

尝试使用

git push --set-upstream origin <branch_name>

否则

利用

git push -u 

会告诉你需要做什么。


git branch --set-upstream origin BRANCH_NAME_HERE 致命:不再支持“--set-upstream”选项。请改用“--track”或“--set-upstream-to”。
V
VonC

使用 Git 2.24(2019 年第四季度),您无需再做

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

您将能够:

git pull --set-upstream-to=origin/main main

在“default remote and branch using -u option - works with push but not pull”查看更多信息。

请注意,对于 Git 2.37(2022 年第三季度),您有一个 new option push.autoSetupRemote

git config --global push.autoSetupRemote true

然后一个简单的 git push 将与 git push --set-upstream-to=origin/main main 相同,使下一个 git pull 已经 设置为从 origin/main 检索提交。


M
MERLIN THOMAS

1) git branch --set-upstream-to=origin/<master_branch> 特征/<your_current_branch>

2)git拉


N
Newt

同样的事情发生在我之前,当我创建一个新的 git 分支而不将其推送到原点时。

尝试先执行这两行:

git checkout -b name_of_new_branch # create the new branch
git push origin name_of_new_branch # push the branch to github

然后:

git pull origin name_of_new_branch

现在应该好了!


M
Maddu Swaroop

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


为您的答案添加一些解释总是有帮助的,以使其更加清晰易懂。请阅读stackoverflow.com/help/how-to-answer
除了@32cupo 的观点,这与五年前接受的答案基本相同,但解释较少。
如果我在原点上的分支上工作,这会破坏什么吗?
M
Maya

$ git branch --set-upstream-to=heroku/master master

$ git pull

为我工作!


A
Arnav Singh

这个答案最适合

谁希望将他们的 repo 链接到他们的项目并提交更改

根据椰子的回答

步骤1

git pull
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-to=<remote>/<branch> master

第2步

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

第 3 步

git pull
Already up to date.

问题

我在提交更改时花了几个小时我按照上面的步骤和git add . git commit .然后我执行了下面的步骤并且我的推送成功了

推送更改

执行

git push -f origin master