如何检出远程 Git 分支? [英] How do I check out a remote Git branch?
问题描述
有人使用 git push origin test
将名为 test
的分支推送到共享存储库.我可以用 git branch -r
看到分支.
现在我正在尝试检查远程 test
分支.
我试过了:
git checkout test
什么都不做git checkout origin/test
给出*(无分支)
.这是令人困惑的.我怎么能在没有分支"?
如何检出远程 Git 分支?
With One Remote
Somebody pushed a branch called test
with git push origin test
to a shared repository. I can see the branch with git branch -r
.
Now I'm trying to check out the remote test
branch.
I've tried:
git checkout test
which does nothinggit checkout origin/test
gives* (no branch)
. Which is confusing. How can I be on "no branch"?
How do I check out a remote Git branch?
With One Remote
Jakub's answer actually improves on this. With Git versions ≥ 1.6.6, with only one remote, you can do:
git fetch
git checkout test
As user masukomi points out in a comment, git checkout test
will NOT work in modern git if you have multiple remotes. In this case use
git checkout -b test <name of remote>/test
or the shorthand
git checkout -t <name of remote>/test
With >1 Remotes
Before you can start working locally on a remote branch, you need to fetch it as called out in answers below.
To fetch a branch, you simply need to:
git fetch origin
This will fetch all of the remote branches for you. You can see the branches available for checkout with:
git branch -v -a
With the remote branches in hand, you now need to check out the branch you are interested in, giving you a local working copy:
git checkout -b test origin/test
I also created the image below for you to share the differences, look at how to fetch works, and also how it's different to pull:
这篇关于如何检出远程 Git 分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!