git 签出现有的远程分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31223669/
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-09 03:21:55  来源:igfitidea点击:

Checkout existing remote branch

gitgithub

提问by Nitesh Virani

I have seen different ways to checkout existing remote branch:

我已经看到了结帐现有远程分支的不同方法:

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:

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

1. git checkout -b bigbug origin/bigbug

2. git checkout -t origin/bigbug

3. git fetch
   git checkout bigbug

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 版本中都可用并且有效吗?如果所有都是有效的,那么它们与使用哪一个之间有什么区别吗?

回答by Lucas Saldanha

The base for the checkout command is:

checkout 命令的基础是:

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

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

当您执行 a 时,git checkout -b bigbug origin/bigbug您是在告诉 Git 执行两个命令:

  1. git branch bigbug origin/bigbug (git creates a branch with name bigbug and setup tracking to origin/bigbug)
  2. git checkout bigbug (git changes your workspace to match bigbug branch)
  1. git branch bigbug origin/bigbug(git 创建一个名为 bigbug 的分支并设置跟踪到 origin/bigbug)
  2. git checkout bigbug(git 更改您的工作区以匹配 bigbug 分支)

When you perform git checkout -t origin/bigbugyou 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 -toptions is the same of --track.

当您执行时,git checkout -t origin/bigbug您是在告诉 Git 执行上面相同的两个命令。不同之处在于它将本地分支命名为与远程分支相同的名称(在第一个示例中,您可以将远程分支的名称更改为您想要的任何名称)。该-t选项相同的--track

In your last command, when you run: git fetchyou tell Git to lookup on the remote repositories for new commits, branches, etc. Then when you run git checkout bigbugyou 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.

在您的最后一个命令中,当您运行时:git fetch您告诉 Git 在远程存储库中查找新的提交、分支等。然后当您运行时,git checkout bigbug您告诉它更改工作区以匹配 bigbug 分支。如果你有一个同名的本地分支,Git 会检出它。如果没有,它会将远程分支查找为一个匹配的名称,然后创建一个具有相同名称的本地分支

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.

因此,当您使用一种或另一种时,这取决于您想要什么。大多数情况下,它们的工作方式相同(除了最后一个示例,当您已经有一个与远程分支同名的本地分支时)。最重要的是确切地知道命令和选项的作用,并根据需要将它们组合起来。