没有参数的Git抓取和拉取 [英] Git fetch and pull with no arguments

查看:113
本文介绍了没有参数的Git抓取和拉取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 foo 的git分支。

  > git status 
#在分支上foo
没有提交(工作目录干净)

它最初使用此命令检出:

 > git checkout origin / foo -b foo --track 

我想从远程存储库。我知道这些命令中的任何一个都足够了:

 > git fetch origin foo#忽略缺少合并
> git pull origin foo

如果我省略了 fetch pull ,将会默认提取(或拉出)我目前检出的分支。成对的命令相当于什么?

 > git checkout foo 
> git pull

 > git checkout foo 
> git pull origin foo


解决方案

不一般取决于你在哪一个分支,你的配置,月相等。

你可以从 git拉手册页,正如我在下面描述的那样,但我通常会尽量避免必须通过执行: git fetch origin 和然后 git merge origin / foo 。 (我写了一篇一些有关这个的博客文章 。)



然而,你的问题其实是关于 git pull 的默认行为,当你没有指定远程或refspec。我们可以从 git pull 手册页,特别是 DEFAULT BEHAVIOR 部分。这是很难弄清楚的,所以我已经大胆地提出了真正适用于你的问题的唯一部分,因为(a)你在分支上 foo , (b)您按照问题描述创建了该分支,并且(c)您没有更改配置。


经常人们不使用任何参数就使用git pull。传统上,这相当于说 git pull origin 然而,当分支< name> 时存在配置分支。< name> .remote ,该值用于代替 origin

为了确定要使用的URL从配置 remote。< origin> .url 的值获取,如果没有任何这样的变量,那么 URL上的值:使用 $ GIT_DIR / remotes /< origin> 文件中的一行



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

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



globbing refspec必须有一个非空RHS(即必须存储在远程跟踪分行),其LHS和RHS必须以 / * 结尾。以上规定,所有远程分支机构都使用同名的 refs / remotes / origin / 层次结构中的远程追踪分支进行跟踪。



确定要在提取后合并哪个远程分支的规则有点牵扯,以避免向后兼容。



如果给出显式的refspecs



当命令行中没有给出refspec时,那么git pull会使用来自配置的refspec或 $ GIT_DIR /遥控器/<来源> 。在这种情况下,以下规则适用:


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


  2. 如果refspec是通配符,则不会合并。

  3. li>


当您使用以下命令创建分支 foo 时:

  git checkout origin / foo -b foo --track 

...它将设置下列配置选项,它们将您的分支 foo refs / heads / foo 在 origin 存储库中:

  branch.foo.remote = origin 
branch.foo.merge = refs / heads / foo

所以,如果你把上面的那些粗暴的句子放在一起,答案是是的,在这个你在分支 foo 时描述的情况,命令 git pull git pull origin foo 是等价的。


I have a git branch checked out named foo.

> git status
# On branch foo
nothing to commit (working directory clean)

It was originally checked out using this command:

> git checkout origin/foo -b foo --track

I want to get updates to this branch from the remote repository. I know that either of these commands will suffice:

> git fetch origin foo # ignore the lack of merging
> git pull origin foo

If I omit the arguments to fetch or pull, will git default to fetching (or pulling) the branch that I currently have checked out? That is, are the following pairs of commands equivalent?

> git checkout foo
> git pull

and

> git checkout foo
> git pull origin foo

解决方案

Unfortunately, whether they are equivalent or not in general depends on which branch you are on, your configuration, phase of the moon, etc.

You can figure this out from the git pull man page, as I've described below, but I would generally try to avoid having to work that out by doing: git fetch origin and then git merge origin/foo. (I wrote a somewhat rambling blog post about this.)

However, your question is really about the default behaviour of git pull when you don't specify a remote or a refspec. We can figure this out from the git pull man page, and in particular the DEFAULT BEHAVIOUR section. This is somewhat tough to figure out, so I've put in bold the only parts that really apply to your question given you given that (a) you are on branch foo, (b) you created that branch as you described in the question, and (c) you haven't been changing your configuration.

Often people use git pull without giving any parameter. Traditionally, this has been equivalent to saying git pull origin. However, when configuration branch.<name>.remote is present while on branch <name>, that value is used instead of origin.

In order to determine what URL to use to fetch from, the value of the configuration remote.<origin>.url is consulted and if there is not any such variable, the value on URL: line in $GIT_DIR/remotes/<origin> file is used.

In order to determine what remote branches to fetch (and optionally store in the remote-tracking branches) when the command is run without any refspec parameters on the command line, values of the configuration variable remote.<origin>.fetch are consulted, and if there aren’t any, $GIT_DIR/remotes/<origin> file is consulted and its Pull: lines are used. In addition to the refspec formats described in the OPTIONS section, you can have a globbing refspec that looks like this:

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

A globbing refspec must have a non-empty RHS (i.e. must store what were fetched in remote-tracking branches), and its LHS and RHS must end with /*. The above specifies that all remote branches are tracked using remote-tracking branches in refs/remotes/origin/ hierarchy under the same name.

The rule to determine which remote branch to merge after fetching is a bit involved, in order not to break backward compatibility.

If explicit refspecs were given on the command line of git pull, they are all merged.

When no refspec was given on the command line, then git pull uses the refspec from the configuration or $GIT_DIR/remotes/<origin>. In such cases, the following rules apply:

  1. If branch.<name>.merge configuration for the current branch exists, that is the name of the branch at the remote site that is merged.

  2. If the refspec is a globbing one, nothing is merged.

  3. Otherwise the remote branch of the first refspec is merged.

When you created the branch foo with:

git checkout origin/foo -b foo --track

... it will have set the following config options, which associate your branch foo with refs/heads/foo in the origin repository:

branch.foo.remote=origin
branch.foo.merge=refs/heads/foo

So, if you put that together with the emboldened sentences above, the answer is "Yes, in this situation you describe, when you're on branch foo, the commands git pull and git pull origin foo are equivalent."

这篇关于没有参数的Git抓取和拉取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆