无法通过git pull提取代码

无法通过git pull提取代码,git,github,git-pull,Git,Github,Git Pull,我的远程GitHub存储库中有以下两个分支: master test-branch 我在我的测试目录中做了git拉取,但在我的本地目录中没有看到任何代码拉取。另外,当我执行git分支时,我在这里看不到任何本地分支列表。不知道为什么?但一旦我执行了git branch-a,请参见下面以红色显示的远程分支: remotes/origin/master remotes/origin/test-branch 当我执行特定分支拉取时,即git-pull-origin测试分支我看到代码在我的测试目录

我的远程GitHub存储库中有以下两个分支:

master
test-branch
我在我的测试目录中做了
git拉取
,但在我的本地目录中没有看到任何代码拉取。另外,当我执行
git分支
时,我在这里看不到任何本地分支列表。不知道为什么?但一旦我执行了git branch-a,请参见下面以红色显示的远程分支:

 remotes/origin/master
 remotes/origin/test-branch
当我执行特定分支拉取时,即
git-pull-origin测试分支
我看到代码在我的测试目录中被拉取,但当我执行
git-branch
时,我看到下面的列表:

* master
  remotes/origin/test-branch [displayed in red]

不知道为什么在我提取测试分支代码时它会在这里显示master。还有,我如何才能看到此主机指向哪个远程分支?

当您执行
git pull origin test branch
时,它将实际从
origin
远程
master
分支中提取更改,并将它们合并到当前签出的分支中

本地签出远程分支并设置跟踪 这将基本上做到以下几点:

  • git fetch
    将更新为远程
  • 创建名为
    测试分支的本地分支
  • 设置源/测试分支作为远程跟踪分支
  • 将本地分支设置为源/测试分支
  • 从git手册:

    -t, --track
    When creating a new branch, set up "upstream" configuration. See "--track" in git-branch(1) for details.
    
    If no -b option is given, the name of the new branch will be derived from the remote-tracking branch, by looking at the local part of the refspec configured for the corresponding remote, and then stripping the
    initial part up to the "*". This would tell us to use hack as the local branch when branching off of origin/hack (or remotes/origin/hack, or even refs/remotes/origin/hack). If the given name has no slash, or the
    above guessing results in an empty name, the guessing is aborted. You can explicitly give a name with -b in such a case.
    
    因此,这基本上意味着,如果您的远程分支被称为
    origin/test branch
    ,它将调用您本地创建的分支
    test branch

    显示跟踪的分支的步骤 将在第二行显示它正在跟踪的远程设备(如果正在跟踪)

    git branch -vv
    
    将向您显示本地分支的列表以及它们正在跟踪的远程分支



    查看此项以获得更详细的答案。

    默认情况下,
    git pull origin xyz
    运行
    git fetch origin xyz
    ,然后运行
    git merge fetch\u HEAD
    。请务必注意,指定的远程分支未签出,而是集成到本地分支中

    它显示
    *master
    ,因为您实际上没有通过运行
    git pull origin xyz
    切换到其他分支。但是,分支
    xyz
    的更改已集成到本地
    master
    分支中,因为在获取后执行了合并。您可能希望首先切换到所需的分支(
    git checkout xyz
    ),然后拉动更改(
    git pull

    您可以使用
    git branch-a-vv
    显示远程和本地分支,包括跟踪信息以及当前签出的分支:

    * xyz                   b7b2f7c [origin/xyz] Commit Message A
      main                  8c4124b [origin/main] Commit Message B
      remotes/origin/HEAD   -> origin/main
      remotes/origin/xyz    b7b2f7c Commit Message A
      remotes/origin/main   8c4124b Commit Message B
    

    如果您没有对本地存储库进行任何更改,我更希望您克隆新的repo。克隆后,您可以使用
    git branch-a
    显示所有分支,然后您可以
    git checkout
    您想要的特定分支,并在此分支中执行
    git pull
    。要查看主分支所指向的分支,您可以在终端
    git log--graph--oneline--all
    上执行这个命令,或者下载一个git客户端,在那里您可以很好地看到这个指向(我建议)当我克隆
    git clone repo_url
    并执行
    git branch-a
    时,我看到输出为
    *主遥控器/原点/头部->原点/主遥控器/原点/主遥控器/原点/测试分支
    现在我想切换到测试分支,我做了
    git checkout-remotes/origin/test-2May-21
    它说你处于分离的头部状态?当您执行git pull origin test branch时,它实际上会从origin remote master branch中提取更改,并将它们合并到当前签出的分支中。那么,
    test branch
    在这里有什么用呢?此外,当前没有签出的文件branch@emilly我不知道你所说的“这里的测试分支有什么用”是什么意思。在命令
    git pull origin test branch
    中,它用于指定要拉哪个
    origin
    分支。而且git总是有一个分支被签出。默认情况下,它是
    master
    分支(或者从那时起,有时是
    main
    。您可以通过运行
    git status
    来检查当前已签出的分支。正如您在命令git pull origin test branch中所说,它用于指定您要提取的源分支。因此,我在这里指定了
    test branch
    ,因此它将从远程tes中提取更改t分支不是主分支。不是吗?@Emily是的,这是正确的。它将从
    origin/test分支
    中提取更改,并将它们与当前签出的分支合并,该分支可能是
    master
    。一旦我在
    git init
    之后执行
    git pull
    操作,为什么我在本地目录中看不到任何代码,我会看到它是空的已尝试
    git-checkout-test-branch
    它不显示任何分支?
    可能您想先切换到所需的分支
    然后再拉我尝试使用
    git-checkout-test-branch签出到测试分支
    但它说
    错误:pathspec“test-branch”与任何文件都不匹配git
    ?@Emily对此可能有一些解释:a)远程配置不正确b)远程存储库上不存在分支c)有多个远程存储库具有匹配的分支名称(在这种情况下通常会显示提示)。您能否运行
    git fetch--all
    ,然后将
    git branch-vv-a
    的输出添加到您的问题中(提交消息可以删除)?
    git branch -vv
    
    * xyz                   b7b2f7c [origin/xyz] Commit Message A
      main                  8c4124b [origin/main] Commit Message B
      remotes/origin/HEAD   -> origin/main
      remotes/origin/xyz    b7b2f7c Commit Message A
      remotes/origin/main   8c4124b Commit Message B