如何在 Git 中签出远程分支?
有人将“新功能”分支推送到共享存储库:
git push -u new_feature_branch
现在,我想在本地计算机上创建此分支的副本以测试新功能。
做到这一点最简单的方法是什么? (我需要在结账
之前获取
/拉动
吗?)
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
/ pull
before checkout
?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我通常发现没有必要使用 git fetch 。
git pull
就足够了。git pull
会将您的存储库与远程同步。然后,new_feature_branch 将可用。git checkout new_feature_branch 会注意到 origin 中的分支,并为您创建一个新的本地跟踪分支并切换到该分支。
I generally find it unnecessary to use
git fetch
.git pull
is sufficient.git pull
will synchronize your repository with the remote. The new_feature_branch will then be available.git checkout new_feature_branch
will notice the branch in origin and create a new local tracking branch for you and switch to that branch.最简单的方法是:
这仅在最初完成。从现在开始,您可以像处理其他分支一样继续使用该分支。
The simplest way to do this is:
This is only done initially. From now on you can continue working with the branch as you do for the others you use.
您需要获取上游更改,以便本地存储库包含相关对象(
git fetch --all
或git fetch
)。之后,您可以使用 git checkout执行签出(如果您想明确执行此操作,可以键入 git checkout -b/
分支>
;本地名称不必与远程名称相同)。如果您还没有该名称的本地分支,它将签出远程分支并跟踪它。作为替代方案,您可以使用 git pull;,但这将 - 使用默认设置 - 将远程分支合并到您的当前分支中,这可能不是您想要的。
You need to fetch upstream changes so your local repository includes the relevant objects (
git fetch --all
orgit fetch <remote>
).Afterwards you can perform a checkout using
git checkout <branch>
(if you like to do it explicitly, you can typegit 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.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 fetch
,然后在获取后运行git checkout new_branch_name
如果您使用 vs code 添加 git Graph 扩展来诊断问题。对您以后的使用也有帮助。
for synchronize your repository with the remote branch run
git fetch
then after fetching rungit checkout new_branch_name
if you are using vs code add git Graph extension for diagnose the issue. It will help you in future use as well.