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

git push origin branch-name

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

git pull origin branch-name

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

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


当前回答

创建一个新目录,然后做一个克隆。

Git克隆(源地址)(分支名称)

其他回答

对于像我这样的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 <url>——branch <branch>

只克隆/下载分支的内容。

这对我特别有帮助,因为我的分支的内容与主分支完全不同(尽管通常不是这样)。 因此,上面其他人列出的建议并没有帮助我,即使在我签出分支并做了git拉取之后,我最终还是会得到一个master的副本。

这个命令将直接为您提供分支的内容。这对我很管用。

在repo名称中复制Git和cd:

$ git clone https://github.com/PabloEzequiel/iOS-AppleWach.git
Cloning into 'iOS-AppleWach'...
$ cd iOS-AppleWach

切换到我想要的分支(GitHub页面):

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

拉树枝:

$ git pull
Already up-to-date.

ls:

$ ls
index.html      params.json     stylesheets

快速回答:

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

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

你可以像这样使用git远程:

git fetch origin

然后设置一个本地分支来跟踪远程分支,如下所示:

git branch --track [local-branch-name] origin/remote-branch-name

现在远程github分支的内容在local-branch-name中。

你可以切换到local-branch-name并开始工作:

git checkout [local-branch-name]