gpt4 book ai didi

不带参数的 Git 获取和 pull

转载 作者:IT王子 更新时间:2023-10-29 00:35:40 25 4
gpt4 key购买 nike

我检查了一个名为 foo 的 git 分支。

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

它最初是使用以下命令 check out 的:

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

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

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

如果我省略 fetchpull 的参数,git 会默认获取(或 pull )我当前 checkout 的分支吗? 也就是说,下面这几对命令是等价的吗?

> git checkout foo
> git pull

> git checkout foo
> git pull origin foo

最佳答案

不幸的是,它们是否等价通常取决于你在哪个分支、你的配置、月相等。

您可以从 git pull 中算出这一点手册页,正如我在下面描述的那样,但我通常会尽量避免通过这样做来解决这个问题:git fetch origin然后 git merge origin/foo . (我写了一个 somewhat rambling blog post about this 。)

但是,您的问题实际上是关于 git pull 的默认行为的当您不指定远程或 refspec 时。我们可以从 git pull man page 中算出这一点,特别是 DEFAULT BEHAVIOUR section .这有点难以理解,所以我将唯一真正适用于你的问题的部分加粗,因为 (a) 你在分支 foo 上。 ,(b) 您按照问题中的描述创建了该分支,并且 (c) 您没有更改配置。

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.

当您创建分支时 foo与:

git checkout origin/foo -b foo --track

... 它将设置以下配置选项,这些选项关联您的分支 foorefs/heads/fooorigin存储库:

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

所以,如果你把它和上面的大胆句子放在一起,答案是“是的,在你描述的这种情况下,当你在分支 foo 上时,命令 git pullgit pull origin foo 是等价的。 "

关于不带参数的 Git 获取和 pull ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6860893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com