没有参数的Git获取和拉取

Mat*_*all 16 git git-pull git-fetch

我有一个名为git branch的签出foo.

> git status
# On branch foo
nothing to commit (working directory clean)
Run Code Online (Sandbox Code Playgroud)

它最初使用此命令检出:

> git checkout origin/foo -b foo --track
Run Code Online (Sandbox Code Playgroud)

我想从远程存储库获取此分支的更新.我知道这些命令中的任何一个都足够了:

> git fetch origin foo # ignore the lack of merging
> git pull origin foo
Run Code Online (Sandbox Code Playgroud)

如果我省略了fetchor 的参数pull,git默认会获取(或拉动)我当前检出的分支?也就是说,以下两对命令是否等效?

> git checkout foo
> git pull
Run Code Online (Sandbox Code Playgroud)

> git checkout foo
> git pull origin foo
Run Code Online (Sandbox Code Playgroud)

Mar*_*air 22

不幸的是,它们是否相同或一般取决于你所在的分支,你的配置,月相等.

你可以从git pull手册页中看到这一点,正如我在下面描述的那样,但我通常会尽量避免通过这样做来解决这个问题:git fetch origin然后git merge origin/foo.(我写了一篇关于此事的博客文章.)

但是,您的问题实际上是关于git pull未指定远程或refspec时的默认行为.我们可以从git pull手册页中找出这一点,特别是该DEFAULT BEHAVIOUR部分.这有点难以弄清楚,所以我给出了大胆的唯一真正适用于你的问题的部分给出了(a)你在分支上foo,(b)你创建了如你在问题中描述的那个分支, (c)您没有更改配置.

通常人们使用git pull而不给出任何参数.传统上,这相当于说git pull origin.但是,当branch.<name>.remote在分支上存在配置时<name>,将使用该值而不是origin.

为了确定要从中获取的URL,请remote.<origin>.url参考配置的值,如果没有任何此类变量,则使用文件中的URL:行值$GIT_DIR/remotes/<origin>.

为了确定在命令行上没有任何refspec参数的情况下运行命令时要获取(并且可选地存储在远程跟踪分支中)的远程分支,请参阅配置变量的值remote.<origin>.fetch,如果没有,则$GIT_DIR/remotes/<origin>查询文件并使用其Pull:行.除了OPTIONS部分中描述的refspec格式之外,您还可以使用如下所示的globbing refspec:

refs/heads/*:refs/remotes/origin/*

globbing refspec必须具有非空RHS(即必须存储在远程跟踪分支中获取的内容),并且其LHS和RHS必须以/*.以上规定了使用refs/remotes/origin/相同名称的层次结构中的远程跟踪分支跟踪所有远程分支.

在获取之后确定要合并哪个远程分支的规则有点涉及,以便不破坏向后兼容性.

如果在git pull的命令行上给出了明确的refspecs,它们都被合并了.

当在命令行上没有给出refspec时,git pull使用配置中的refspec或$GIT_DIR/remotes/<origin>.在这种情况下,以下规则适用:

  1. If branch.<name>.merge 存在当前分支的配置,即合并的远程站点上的分支的名称.

  2. 如果refspec是一个全局的,则不会合并任何内容.

  3. 否则,合并第一个refspec的远程分支.

当您使用以下内容创建分支foo时:

git checkout origin/foo -b foo --track
Run Code Online (Sandbox Code Playgroud)

...这将已经设置以下配置选项,您的分支相关联foorefs/heads/fooorigin库:

branch.foo.remote=origin
branch.foo.merge=refs/heads/foo
Run Code Online (Sandbox Code Playgroud)

所以,如果你把连同底气上面的句子,答案是"是的,在这种情况下,你形容,当你在分行foo,命令git pullgit pull origin foo是等价的."


Rus*_*vis 5

即使在Mark描述的条件下,当它们看起来相同时,仍然存在细微的差异- git pull origin foo不会更新远程跟踪分支,而是git pull会更新。这在git-pull的手册页中有记录:

A parameter <ref> without a colon is equivalent to <ref>: when pulling/fetching, so it merges <ref> into the current branch without storing the remote branch anywhere locally

因此,在您的情况下,对于等于的真实值git pull,您需要git pull origin foo:refs/remotes/origin/foo