如何使用 git 下载分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2294313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:52:41  来源:igfitidea点击:

How to download a branch with git?

gitbranch

提问by Andrew

I have a project hosted on GitHub. I created a branch on one computer, then pushed my changes to GitHub with:

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

git push origin branch-name

Now I am on a different computer, and I want to download that branch. So I tried:

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

git pull origin branch-name

...but all this did was overwrite my master branch with the changes in my new branch.

...但所有这一切都用我的新分支中的更改覆盖了我的主分支。

What do I need to do to properly pull my remote branch, without overwriting existing branches?

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

回答by Andrew

Thanks to a related question, I found out that I need to "checkout" the remote branch as a new local branch, and specify a new local branch name.

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

git checkout -b newlocalbranchname origin/branch-name

Or you can do:

或者你可以这样做:

git checkout -t origin/branch-name

The latter will create a branch that is also set to track the remote branch.

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



Update:It's been 5 years since I originally posted this question. I've learned a lot and git has improved since then. My usual workflow is a little different now.

更新:我最初发布这个问题已经 5 年了。我学到了很多东西,从那时起 git 有了改进。我平时的工作流程现在有点不同。

If I want to fetch the remote branches, I simply run:

如果我想获取远程分支,我只需运行:

git pull

This will fetch all of the remote branches and merge the current branch. It will display an output that looks something like this:

这将获取所有远程分支并合并当前分支。它将显示如下所示的输出:

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.

Now git knows about my new my-bugfix-branch. To switch to this branch, I can simply run:

现在 git 知道我的新my-bugfix-branch. 要切换到这个分支,我可以简单地运行:

git checkout my-bugfix-branch

Normally, I would need to create the branch before I could check it out, but in newer versions of git, it's smart enough to know that you want to checkouta local copy of this remote branch.

通常,我需要先创建分支,然后才能检出它,但在较新版本的 git 中,它足够聪明,知道您想要检出此远程分支的本地副本。

回答by Dan J

For any Git newbies like me, here are some steps you could follow to download a remote repository, and then switch to the branch that you want to view. They probably abuse Git in some way, but it did the job for me! :-)

对于像我这样的 Git 新手,您可以按照以下步骤下载远程存储库,然后切换到您想要查看的分支。他们可能以某种方式滥用 Git,但它为我完成了工作!:-)

Clone the repository you want to download the code for (in this example I've picked the LRResty projecton Github):

克隆您要为其下载代码的存储库(在本例中,我选择了Github 上的LRResty 项目):

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

Check what branch you are using at this point (it should be the master branch):

检查此时您使用的是哪个分支(它应该是 master 分支):

$ git branch    
* master

Check out the branch you want, in my case it is called 'arcified':

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

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

Confirm you are now using the branch you wanted:

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

$ git branch    
* arcified
  master

If you want to update the code again later, run git pull:

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

$ git pull
Already up-to-date.

回答by ardsrk

You could use git remote like:

你可以像这样使用 git remote:

git fetch origin

and then setup a local branch to track the remote branch like below:

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

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

You would now have the contents of the remote github branch in local-branch-name.

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

You could switch to that local-branch-name and start work:

您可以切换到该本地分支名称并开始工作:

git checkout [local-branch-name]

回答by Old Markus

you can use :

您可以使用 :

git clone <url> --branch <branch>

git clone <url> --branch <branch>

to clone/download only the contents of the branch.

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

This was helpful to me especially, since the contents of my branch were entirely different from the master branch (though this is not usually the case). Hence, the suggestions listed by others above didn't help me and I would end up getting a copy of the master even after I checked out the branch and did a git pull.

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

This command would directly give you the contents of the branch. It worked for me.

此命令将直接为您提供分支的内容。它对我有用。

回答by Srikanta Sahoo

Navigate to the folder on your new machine you want to download from git on git bash.

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

Use below command to download the code from any branch you like

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

git clone 'git ssh url' -b 'Branch Name'

git clone 'git ssh url' -b 'Branch Name'

It will download the respective branch code.

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

回答by Pablo Ezequiel

Git clone and cd in the repo name:

存储库名称中的 Git clone 和 cd:

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

Switch to the branch (a GitHub page) that I want:

切换到我想要的分支(一个 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'

And pull the branch:

并拉动分支:

$ git pull
Already up-to-date.

ls:

ls:

$ ls
index.html      params.json     stylesheets

回答by TachyonImpact

Create a new directory, and do a clone instead.

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

git clone (address of origin) (name of branch)

git clone(原地址)(分支名称)