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

git push origin branch-name

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

git pull origin branch-name

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

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


当前回答

$ git fetch

$ git checkout <分支>

Git fetch将获取所有远程分支。 您可以使用git branch -r(或git branch -rv)来验证远程分支。 如果你没有同名的分支,你可以使用git checkout <branch>直接切换到这个分支

其他回答

你可以使用:

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 clone -b < branch name > <remote_repo url> 

$ git fetch

$ git checkout <分支>

Git fetch将获取所有远程分支。 您可以使用git branch -r(或git branch -rv)来验证远程分支。 如果你没有同名的分支,你可以使用git checkout <branch>直接切换到这个分支