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

git push origin branch-name

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

git pull origin branch-name

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

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


当前回答

Git checkout -b branch/name

Git拉源码分支/名称

其他回答

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

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

快速回答:

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

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

在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

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

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

git clone 'git ssh 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中,它足够智能,可以知道您想签出这个远程分支的本地副本。