如何在 Git 中签出远程分支?

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

How to checkout a remote branch in Git?

gitgit-branchgit-checkout

提问by Misha Moroshko

Someone pushed a "new feature" branch to the shared repo:

有人将“新功能”分支推送到共享存储库:

git push -u new_feature_branch

Now, I would like to create a copy of this branch on my local machine in order to test the new feature.

现在,我想在本地计算机上创建此分支的副本以测试新功能。

What would be the easiest way to do this? (Do I need to fetch/ pullbefore checkout?)

什么是最简单的方法来做到这一点?(我需要fetch/pull之前checkout吗?)

回答by Bill Door

I generally find it unnecessary to use git fetch. git pullis sufficient. git pullwill synchronize your repository with the remote. The new_feature_branchwill then be available.

我通常觉得没有必要使用git fetch. git pull足够了。git pull将您的存储库与远程同步。然后new_feature_branch将可用。

git checkout new_feature_branchwill notice the branch in origin and create a new local tracking branch for you and switch to that branch.

git checkout new_feature_branch将注意到 origin 中的分支并为您创建一个新的本地跟踪分支并切换到该分支。

git pull
git checkout new_feature_branch

回答by Adam Dymitruk

The simplest way to do this is:

最简单的方法是:

git fetch
git checkout -t origin/new_feature_branch

This is only done initially. From now on you can continue working with the branch as you do for the others you use.

这仅在最初完成。从现在开始,您可以像处理其他分支一样继续使用该分支。

回答by Troels Thomsen

You need to fetch upstream changes so your local repository includes the relevant objects (git fetch --allor git fetch <remote>).

您需要获取上游更改,以便您的本地存储库包含相关对象(git fetch --allgit fetch <remote>)。

Afterwards you can perform a checkout using git checkout <branch>(if you like to do it explicitly, you can type git checkout -b <branch> <remote>/<branch>; the local name doesn't have to be the same as the remote). If you don't already have a local branch of that name, it will checkout the remote branch and track it.

之后,您可以执行结帐操作git checkout <branch>(如果您想明确地执行此操作,可以键入git checkout -b <branch> <remote>/<branch>;本地名称不必与远程名称相同)。如果您还没有该名称的本地分支,它将检出远程分支并跟踪它。

As an alternative to this, you can use git pull <remote> <branch>, but this will - with default settings - merge the remote branch into your current, which is probably not what you want.

作为替代方案,您可以使用git pull <remote> <branch>,但这将 - 使用默认设置 - 将远程分支合并到您当前的分支中,这可能不是您想要的。

回答by Mukesh Chapagain

git fetch && git checkout new_feature_branch