我有一个项目托管在GitHub上。我在一台计算机上创建了一个分支,然后将我的更改推到GitHub:

git push origin branch-name

现在我在另一台计算机上,我想下载那个分支。所以我试着:

git pull origin branch-name

...但这一切都是覆盖我的主分支与我的新分支的变化。

我需要做什么才能正确地拉出我的远程分支,而不覆盖现有的分支?


当前回答

导航到新机器上你想要从git上的git bash下载的文件夹。

使用下面的命令从您喜欢的任何分支下载代码

git clone 'git ssh url'

它将下载相应的分支代码。

其他回答

快速回答:

https://github.com/[username]/[respository]/archive/refs/heads/[branch].zip

您可以使用该链接直接下载,而无需使用复杂的cmd。

对于像我这样的Git新手,可以按照以下步骤下载远程存储库,然后切换到想要查看的分支。他们可能在某种程度上滥用Git,但它对我有用!: -)

克隆你想要下载代码的存储库(在这个例子中,我选择了Github上的LRResty项目):

$ git clone https://github.com/lukeredpath/LRResty.git
$ cd LRResty

检查你正在使用的分支(应该是主分支):

$ git branch    
* master

查看你想要的分支,在我的情况下,它被称为“arcified”:

 $ git checkout -b arcified origin/arcified
 Branch arcified set up to track remote branch arcified from origin.
 Switched to a new branch 'arcified'

确认你现在正在使用你想要的分支:

$ git branch    
* arcified
  master

如果你想稍后再次更新代码,运行git pull:

$ git pull
Already up-to-date.

我们可以使用以下神奇的命令来下载指定的分支:

git clone -b < branch name > <remote_repo url> 

由于一个相关的问题,我发现需要将远程分支“签出”为一个新的本地分支,并指定一个新的本地分支名称。

git checkout -b newlocalbranchname origin/branch-name

或者你可以这样做:

git checkout -t origin/branch-name

后者将创建一个分支,该分支也被设置为跟踪远程分支。


更新:我第一次提出这个问题已经5年了。从那时起,我学到了很多东西,git也有所提高。我通常的工作流程现在有点不同。

如果我想获取远程分支,我简单地运行:

git pull

这将获取所有远程分支并合并当前分支。它将显示一个类似这样的输出:

From github.com:andrewhavens/example-project
   dbd07ad..4316d29  master     -> origin/master
 * [new branch]      production -> origin/production
 * [new branch]      my-bugfix-branch -> origin/my-bugfix-branch
First, rewinding head to replay your work on top of it...
Fast-forwarded master to 4316d296c55ac2e13992a22161fc327944bcf5b8.

现在我知道我的新漏洞修复分支了。要切换到这个分支,我可以简单地运行:

git checkout my-bugfix-branch

通常,我需要在签出分支之前创建它,但在较新版本的git中,它足够智能,可以知道您想签出这个远程分支的本地副本。

导航到新机器上你想要从git上的git bash下载的文件夹。

使用下面的命令从您喜欢的任何分支下载代码

git clone 'git ssh url'

它将下载相应的分支代码。