无法通过 git pull 拉取代码

时间:2021-05-02 08:02:12

标签: git github git-pull

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

master
test-branch

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

 remotes/origin/master
 remotes/origin/test-branch

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

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

不知道为什么在我拉测试分支代码时它会在这里显示 master。另外我怎么能看到这个master指向哪个远程分支?

2 个答案:

答案 0 :(得分:0)

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

在本地签出远程分支并设置跟踪

git fetch
git checkout --track origin/test-branch

这基本上会执行以下操作:

  1. git fetch 将更新为远程
  2. 创建一个名为 test-branch 的本地分支
  3. origin/test-branch 设置为远程跟踪分支
  4. 将本地分支设置为 origin/test-branch

来自 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 status

如果正在跟踪,将在第二行显示它正在跟踪的远程

git branch -vv

将向您显示本地分支机构的列表以及它们正在跟踪的远程


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

答案 1 :(得分:0)

默认情况下,git pull origin xyz 运行 git fetch origin xyz,后跟 git merge FETCH_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