'git pull' 的默认行为

Default behaviour of 'git pull'

为什么 git pull 会提取所有内容,包括新创建的远程分支,而 git pull origin master 不会?

我正在使用 git version 2.9.3.windows.2

当您不指定任何分支时,将使用默认设置。默认意味着获取和更新 all 个存在于远程存储库中的分支。

有关详细信息,请参阅文档:

git pull [options] [<repository> [<refspec>…​]]

<refspec> specifies which refs to fetch and which local refs to update. When no <refspec>s appear on the command line, the refs to fetch are read from remote.<repository>.fetch variables instead (see git-fetch[1]).

Source: https://git-scm.com/docs/git-pull

参考文档解释:

You often interact with the same remote repository by regularly and repeatedly fetching from it. In order to keep track of the progress of such a remote repository, git fetch allows you to configure remote.<repository>.fetch configuration variables.

Typically such a variable may look like this:

[remote "origin"]
  fetch = +refs/heads/*:refs/remotes/origin/*

The example above will fetch all branches that exist in the origin (i.e. any ref that matches the left-hand side of the value, refs/heads/*) and update the corresponding remote-tracking branches in the refs/remotes/origin/* hierarchy.

Source: https://git-scm.com/docs/git-fetch#CRTB

该行为是默认行为,因为它允许您一次同步整个存储库。如果您不想一次更新所有本地分支,请使用 git fetch 同步存储库并使用 git merge origin/<branch> 更新每个本地分支。

其实很简单

当您说 git pull 时,无论任何挂钩和过滤器,所有内容都会添加到您的本地。简而言之,您可以从遥控器获取所有内容并更新 .git 文件夹。您可以转到文件夹 .git/logs/refs/remotes/origin/ 你会看到你本地的所有分支。

所以,现在我在命令中输入 git pull

幕后发生的事情与您拥有的当地分支机构无关。它连接了原点并从那里到你的本地。

但是,当我键入 git pull origin master 时。在这里你给出了一个来自 origin 的路径规范,你需要一个最新的 master 分支负责人。然后只有 master brach 在 remote

上被拉取和刷新

所以,origin master 是他们用 git 语言调用的路径规范。

在外行语言中git pull 从您的远程获取所有内容(所有新分支并更新旧分支),默认情况下,它会为 origin.如果您有任何其他遥控器,例如 upstream,您必须指定它,例如 git pull upstream,它将更新上游的所有内容。