更改存储库的远程分支并仅拉取更改

Change remote branch of repository and pull only the changes

我有一个有很多分支的远程仓库。每次有新版本,都会创建一个新分支。当有新版本可用时,我如何更改分支并提取更改以避免再次克隆它。我只想下载新提交。

行政总结:

git fetch origin
git checkout new-version`

其中 origin 是远程名称,new-version 是分支名称。

详情:

git fetchdocumentation 说:

Fetch branches and/or tags (collectively, "refs") from one or more other repositories, along with the objects necessary to complete their histories. Remote-tracking branches are updated.

这基本上意味着它会从远程下载存储库的历史记录。但是,一个存储库可以有多个遥控器,因此 git fetch 可以使用 --all 标志获取其中一个或多个遥控器。

By default, any tag that points into the histories being fetched is also fetched; the effect is to fetch tags that point at branches that you are interested in. This default behavior can be changed by using the --tags or --no-tags options... By using a refspec that fetches tags explicitly, you can fetch tags that do not point into branches you are interested in as well.

意思是,例如,如果您在远程有分支 A、B 和 C,而在本地您在分支 C。如果你 git fetch origin C,默认情况下,这也会获取指向该分支的标签,但不会获取与其他分支相关的标签,例如 AB.
如果要获取所有标签,只需在命令中添加 --tags

最后,

When no remote is specified, by default the origin remote will be used, unless there’s an upstream branch configured for the current branch.

资源:

  • git fetchdocumentation,都解释的很好
  • Git Pro book,第 3.5 章介绍了这一点。
  • 您可能还想看看这个相关问题:What is the difference between 'git pull' and 'git fetch'?