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?
我需要做什么来正确地拉动我的远程分支?
6 个解决方案
#1
274
Thanks to a related question, I found out that 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分支。切换到这个分支,我可以简单地运行:
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 checkout a local copy of this remote branch.
通常,在检出分支之前,我需要创建分支,但是在新版本的git中,知道您想要检出这个远程分支的本地副本就足够聪明了。
#2
52
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 project on 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):
检查您现在使用的是哪个分支(应该是主分支):
$ 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.
#3
20
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.
现在,您将拥有远程github分支的本地分支名称的内容。
You could switch to that local-branch-name and start work:
您可以切换到本地分支名称并开始工作:
git checkout [local-branch-name]
#4
7
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克隆'git ssh url' -b 'Branch Name'
It will download the respective branch code.
它将下载各自的分支代码。
#5
6
Git clone and cd in the repo name:
以repo名称的Git克隆和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
#6
-6
Create a new directory, and do a clone instead.
创建一个新目录,然后进行克隆。
git clone (address of origin) (name of branch)
git克隆(产地)(分公司名称)
#1
274
Thanks to a related question, I found out that 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分支。切换到这个分支,我可以简单地运行:
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 checkout a local copy of this remote branch.
通常,在检出分支之前,我需要创建分支,但是在新版本的git中,知道您想要检出这个远程分支的本地副本就足够聪明了。
#2
52
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 project on 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):
检查您现在使用的是哪个分支(应该是主分支):
$ 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.
#3
20
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.
现在,您将拥有远程github分支的本地分支名称的内容。
You could switch to that local-branch-name and start work:
您可以切换到本地分支名称并开始工作:
git checkout [local-branch-name]
#4
7
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克隆'git ssh url' -b 'Branch Name'
It will download the respective branch code.
它将下载各自的分支代码。
#5
6
Git clone and cd in the repo name:
以repo名称的Git克隆和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
#6
-6
Create a new directory, and do a clone instead.
创建一个新目录,然后进行克隆。
git clone (address of origin) (name of branch)
git克隆(产地)(分公司名称)