检出现有的远程分支 [英] Checkout existing remote branch

查看:77
本文介绍了检出现有的远程分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了不同的方法来检出现有的远程分支:

I have seen different ways to checkout existing remote branch:

假设我的朋友推送了新的分支"bigbug",并且我想签出并将本地工作副本切换到该分支,我有以下选择:

Suppose my friend has pushed new branch 'bigbug' and I want to check out and switched my local working copy to that branch, I have below options:

1. git checkout -b bigbug origin/bigbug

2. git checkout -t origin/bigbug

3. git fetch
   git checkout bigbug

以上三个选项在当前git版本中是否可用并且有效吗?如果所有这些都有效,那么它们与使用哪一个之间有什么区别?

Are all above three options available in current git release and valid? If all are valid then is there any difference between them and which one to use?

推荐答案

结帐命令的基础是:

git checkout --[options] <local branch> <remote>/<tracked branch>

执行git checkout -b bigbug origin/bigbug时,是对Git执行两个命令:

When you perform a git checkout -b bigbug origin/bigbug you are saying to Git to execute two commands:

  1. git分支bigbug origin/bigbug(git创建了一个名为bigbug的分支,并设置了对origin/bigbug的跟踪)
  2. git checkout bigbug(git更改您的工作空间以匹配bigbug分支)

执行git checkout -t origin/bigbug时,是对Git说要执行上面相同的两个命令.不同之处在于,它将使用与远程分支相同的名称来命名本地分支(在第一个示例中,您可以将远程分支的名称更改为所需的名称). -t 选项与-track 相同.

When you perform git checkout -t origin/bigbug you are saying to Git to execute the same two commands above. The difference is that it will name the local branch with the same name of the remote branch (in the first example you can change the name of the remote branch to whichever you want). The -t options is the same of --track.

在上一个命令中,当您运行时:git fetch告诉Git在远程存储库上查找新的提交,分支等.然后,当您运行git checkout bigbug时,告诉它更改工作区以匹配bigbug.分支. 如果您有一个使用该名称的本地分支机构,Git将对其进行签出.如果没有,它将把远程分支查找为一个匹配的名称,然后创建一个具有相同名称的本地分支..

In your last command, when you run: git fetch you tell Git to lookup on the remote repositories for new commits, branches, etc. Then when you run git checkout bigbug you tell it to change the workspace to match the bigbug branch. If you have a local branch with that name, Git will checkout it. If not, it will look the remote branches to one matching name and then create a local branch with the same name.

因此,当您使用一个或另一个时,取决于您要的内容.在大多数情况下,它们将以相同的方式工作(在上一个示例中,当您已经具有与远程分支同名的本地分支时除外).最重要的是确切地知道命令和选项的作用,并将它们与所需的内容进行相应的组合.

So, when you use one or another it depends of what you want. Most of the time they will work as the same (except in the last example, when you already have a local branch with the same name of a remote branch). The most importante thing is to know exactly what the command and options do and combine them accordingly to what you want.

这篇关于检出现有的远程分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆