如何查看远程 Git 分支?
有人使用 git Push origin test
将名为 test
的分支推送到共享存储库。我可以使用 gitbranch -r 看到分支。如何查看远程 test
分支?我尝试过:
git checkout test
,它没有执行任何操作git checkout origin/test
给出*(无分支)
Somebody pushed a branch called test
with git push origin test
to a shared repository. I can see the branch with git branch -r
. How do I check out the remote test
branch? I've tried:
git checkout test
, which does nothinggit checkout origin/test
gives* (no branch)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
要获取所有远程分支,请使用以下命令:
然后签出该分支:
To get all remote branches, use this:
Then check out to the branch:
在我看来,没有人提出最简单的方法(或者也许我太笨了,认为这是“一种方法”)。但无论如何,试试这个:
这在同样的情况下对我有用(在我上次拉取请求后在远程创建的分支)。
It seems to my that no one suggested the simplest way (or maybe I'm too dumb to think this is "a way"). But anyway, try this:
This worked for me in the same case (a branch created on the remote after my last pull request).
对于我们来说,似乎
remote.origin.fetch
配置出现了问题。因此,除了master
之外,我们看不到任何其他远程分支,因此git fetch [--all]
没有帮助。 git checkout mybranch 和 git checkout -b mybranch --track origin/mybranch 都不起作用,尽管它肯定是在远程。之前的配置只允许获取
master
:使用
*
修复它并从 origin 获取新信息:现在我们可以
git checkout
本地远程分支。我不知道这个配置如何最终出现在我们的本地存储库中。
For us, it seems the
remote.origin.fetch
configuration gave a problem. Therefore, we could not see any other remote branches thanmaster
, sogit fetch [--all]
did not help. Neithergit checkout mybranch
norgit checkout -b mybranch --track origin/mybranch
did work, although it certainly was at remote.The previous configuration only allowed
master
to be fetched:Fix it by using
*
and fetch the new information from origin:Now we could
git checkout
the remote branch locally.I don't have any idea how this configuration ended up in our local repository.
根据配置的是一个还是多个远程存储库,答案会有所不同。这样做的原因是,对于单个远程情况,可以简化一些命令,因为歧义较少。
针对 Git 2.23 进行了更新:对于旧版本,请参阅末尾的部分。
使用一个远程
在这两种情况下,首先从远程存储库获取数据,以确保您已下载所有最新更改。
这将为您获取所有远程分支。您可以通过以下方式查看可用于签出的分支:
以
remotes/*
开头的分支可以被视为远程分支的只读副本。要在分支上工作,您需要从中创建一个本地分支。这是通过 Git 命令switch
(自 Git 2.23 开始)通过给它远程分支的名称(减去远程名称)来完成的:在这种情况下,Git 正在猜测(可以使用
禁用) --no-guess
)表明您正在尝试签出并跟踪具有相同名称的远程分支。使用多个远程库
在存在多个远程存储库的情况下,需要显式命名远程存储库。
和以前一样,首先获取最新的远程更改:
这将为您获取所有远程分支。您可以通过以下方式查看可用于签出的分支:
有了远程分支,您现在需要使用
-c
签出您感兴趣的分支以创建新的本地分支:有关更多信息使用
git switch
:在 Git 2.23 之前,
Git 2.23 中添加了
git switch
,在此之前git checkout
用于切换分支。仅使用单个远程存储库进行签出:
如果配置了多个远程存储库,那么它会变得有点长
The answer has been split depending on whether there is one remote repository configured or multiple. The reason for this is that for the single remote case, some of the commands can be simplified as there is less ambiguity.
Updated for Git 2.23: For older versions, see the section at the end.
With One Remote
In both cases, start by fetching from the remote repository to make sure you have all the latest changes downloaded.
This will fetch all of the remote branches for you. You can see the branches available for checkout with:
The branches that start with
remotes/*
can be thought of as read only copies of the remote branches. To work on a branch you need to create a local branch from it. This is done with the Git commandswitch
(since Git 2.23) by giving it the name of the remote branch (minus the remote name):In this case Git is guessing (can be disabled with
--no-guess
) that you are trying to checkout and track the remote branch with the same name.With Multiple Remotes
In the case where multiple remote repositories exist, the remote repository needs to be explicitly named.
As before, start by fetching the latest remote changes:
This will fetch all of the remote branches for you. You can see the branches available for checkout with:
With the remote branches in hand, you now need to check out the branch you are interested in with
-c
to create a new local branch:For more information about using
git switch
:Prior to Git 2.23
git switch
was added in Git 2.23, prior to thisgit checkout
was used to switch branches.To checkout out with only a single remote repository:
if there are multiple remote repositories configured then it becomes a bit longer
旁注:使用现代 Git (>= 1.6.6),您可以使用 just
(注意它是 'test' 而不是 'origin/test')来执行神奇的 DWIM-mery 并为您创建本地分支“测试”,其上游将是远程的-跟踪分支“起源/测试”。
gitbranch
输出中的* (nobranch)
意味着您位于未命名的分支上,处于所谓的“分离 HEAD”状态(HEAD 直接指向提交,而不是对某些本地分支的符号引用)。如果您在此未命名分支上进行了一些提交,则始终可以在当前提交之外创建本地分支:评论中建议的更现代的方法:
强调
git checkout origin/test
Sidenote: With modern Git (>= 1.6.6), you are able to use just
(note that it is 'test' not 'origin/test') to perform magical DWIM-mery and create local branch 'test' for you, for which upstream would be remote-tracking branch 'origin/test'.
The
* (no branch)
ingit branch
output means that you are on unnamed branch, in so called "detached HEAD" state (HEAD points directly to commit, and is not symbolic reference to some local branch). If you made some commits on this unnamed branch, you can always create local branch off current commit:A more modern approach as suggested in the comments:
emphasis on
git checkout origin/test
在这种情况下,您可能想要创建一个本地
test
分支来跟踪远程test
分支:在
git
的早期版本中,您需要显式的--track
选项,但现在当您从远程分支分支时,这是默认选项。要创建本地分支并切换到它,请使用:
In this case, you probably want to create a local
test
branch which is tracking the remotetest
branch:In earlier versions of
git
, you needed an explicit--track
option, but that is the default now when you are branching off a remote branch.To create the local branch and switch to it, use:
接受的答案对您不起作用?
虽然第一个和选定的答案在技术上正确,但您可能尚未从远程存储库检索所有对象和引用。如果是这种情况,您将收到以下错误:
解决方案
如果您收到此消息,则必须先执行
git fetch origin
,其中origin
是远程存储库的名称,然后再运行git checkout remote_branch
>。下面是一个包含响应的完整示例:如您所见,运行 git fetch origin 检索了我们尚未设置为在本地计算机上跟踪的任何远程分支。从那里,由于我们现在有了对远程分支的引用,我们可以简单地运行 git checkout remote_branch ,我们将获得远程跟踪的好处。
Accepted answer not working for you?
While the first and selected answer is technically correct, there's the possibility you have not yet retrieved all objects and refs from the remote repository. If that is the case, you'll receive the following error:
Solution
If you receive this message, you must first do a
git fetch origin
whereorigin
is the name of the remote repository prior to runninggit checkout remote_branch
. Here's a full example with responses:As you can see, running
git fetch origin
retrieved any remote branches we were not yet setup to track on our local machine. From there, since we now have a ref to the remote branch, we can simply rungit checkout remote_branch
and we'll gain the benefits of remote tracking.我尝试了上面的解决方案,但没有成功。试试这个,它有效:
这将获取远程分支并创建一个名为
local_branch_name
的新本地分支(如果尚不存在)并跟踪其中的远程分支。I tried the above solution, but it didn't work. Try this, it works:
This will fetch the remote branch and create a new local branch (if not exists already) with name
local_branch_name
and track the remote one in it.您基本上可以看到分支,但您还没有该分支的本地副本!...
您需要
获取
分支...您可以简单地获取然后签出到分支,使用下面的一行命令可以做到这一点:
我还创建了下图,以便您分享差异,看看
fetch
的工作原理以及它与pull
的不同之处:< a href="https://i.sstatic.net/ODFYa.png" rel="noreferrer">
You basically see the branch, but you don't have a local copy of that yet!...
You need to
fetch
the branch...You can simply fetch and then checkout to the branch, use the one line command below to do that:
I also created the image below for you to share the differences, look at how
fetch
works and also how it's different topull
:使用:
在我的良性案例中,其他答案不适用于现代 Git。如果远程分支是新的,您可能需要先拉取,但我还没有检查这种情况。
Use:
Other answers do not work with modern Git in my benign case. You might need to pull first if the remote branch is new, but I haven't checked that case.
这将为未命名源的远程(文档):
要添加新的远程,您需要首先执行以下操作:
第一个告诉 Git 远程存在,第二个获取承诺。
This will DWIM for a remote not named origin (documentation):
To add a new remote, you will need to do the following first:
The first tells Git the remote exists, the second gets the commits.
要克隆 Git 存储库,请执行以下操作:
上述命令检查所有分支,但仅初始化
master
分支。如果您想签出其他分支,请执行以下操作:此命令签出远程分支,并且您的本地分支名称将与远程分支相同。
如果您想在结帐时覆盖本地分支名称:
现在您的本地分支名称是
enhancement
,但您的远程分支名称是future_branch
。To clone a Git repository, do:
The above command checks out all of the branches, but only the
master
branch will be initialized. If you want to checkout the other branches, do:This command checks out the remote branch, and your local branch name will be same as the remote branch.
If you want to override your local branch name on checkout:
Now your local branch name is
enhancement
, but your remote branch name isfuture_branch
.我总是这样做:
git fetch origin && git checkout --track origin/branch_name
I always do:
git fetch origin && git checkout --track origin/branch_name
对于上述所有建议,我陷入了一种情况,看到
error: pathspec 'desired-branch' did not match any file(s)known to git.
。我使用的是 Git 版本 1.8.3.1。所以这对我有用:
背后的解释是我注意到在获取远程分支时,它被获取到FETCH_HEAD:
I was stuck in a situation seeing
error: pathspec 'desired-branch' did not match any file(s) known to git.
for all of the suggestions above. I'm on Git version 1.8.3.1.So this worked for me:
The explanation behind is that I've noticed that when fetching the remote branch, it was fetched to FETCH_HEAD:
你可以尝试
或者
You can try
or
首先,您需要执行以下操作:
git fetch
# 如果您不知道分支名称其次,您可以通过以下方式将远程分支检出到本地:
-b
will create来自您选择的远程分支的指定名称的新分支。First, you need to do:
git fetch
# If you don't know about branch nameSecond, you can check out remote branch into your local by:
-b
will create new branch in specified name from your selected remote branch.git remote show
命令将列出所有分支(包括未跟踪的分支)。然后你就可以找到你需要获取的远程分支名称。示例:
使用以下步骤获取远程分支:
示例:
The
git remote show <origin name>
command will list all branches (including un-tracked branches). Then you can find the remote branch name that you need to fetch.Example:
Use these steps to fetch remote branches:
Example:
我使用以下命令:
I use the following command:
命令
等于
,然后
两者都会从
development
创建一个latestfixs_for_dev
Commands
are equal to
and then
Both will create a
latest fixes_for_dev
fromdevelopment
只需使用远程分支的名称运行 git checkout 即可。 Git 会自动创建一个跟踪的本地分支远程分支:
但是,如果在多个远程分支中找到该分支名称,则此操作将不起作用,因为 Git 不知道该使用哪一个。在这种情况下,您可以使用:
或
在 2.19,Git 学习了
checkout.defaultRemote< /code>
配置,它指定在解决此类歧义时默认使用的远程。
Simply run
git checkout
with the name of the remote branch. Git will automatically create a local branch that tracks the remote one:However, if that branch name is found in more than one remote, this won't work as Git doesn't know which to use. In that case you can use either:
or
In 2.19, Git learned the
checkout.defaultRemote
configuration, which specifies a remote to default to when resolving such an ambiguity.有很多替代方案,例如:
替代方案 1:
这是最简单的方法。
替代方案 2:
效果相同,但分两步。
There are many alternatives, for example:
Alternative 1:
It's the simplest way.
Alternative 2:
It's the same, but in two steps.
这些答案都不适合我。这有效:
None of these answers worked for me. This worked:
如果分支位于
origin
远程之外的其他位置,我喜欢执行以下操作:这将签出
upstream
远程上的next
分支到名为second/next
的本地分支。这意味着如果您已经有一个名为 next 的本地分支,则不会发生冲突。If the branch is on something other than the
origin
remote I like to do the following:This will checkout the
next
branch on theupstream
remote in to a local branch calledsecond/next
. Which means if you already have a local branch named next it will not conflict.git fetch && git checkout 你的分支名称
git fetch && git checkout your-branch-name
gitbranch -r 表示对象名称无效,因为该分支名称不在 Git 的本地分支列表中。使用以下命令从源更新本地分支列表:
然后尝试再次检查远程分支。
这对我有用。
我相信 git fetch 会拉入所有远程分支,这不是原始发布者想要的。
git branch -r
says the object name is invalid, because that branch name isn't in Git's local branch list. Update your local branch list from origin with:And then try checking out your remote branch again.
This worked for me.
I believe
git fetch
pulls in all remote branches, which is not what the original poster wanted.TL;DR
使用
git switch
而不是git checkout
。更多详细信息请参见此页面。我认为答案已经过时了。 Git 现在将
checkout
的部分功能拆分为switch
和restore
。以下是我的总结:
如果您想更新远程分支的某些内容,您应该创建一个本地分支来“跟踪”远程分支。您可以在本地更新任何您想要的内容,最后推送到远程。如果您在克隆存储库后直接检出远程分支,您可能会看到“分离的 HEAD”状态和来自 Git 的以下消息:
那么我们如何创建本地分支来跟踪远程分支?
要创建本地分支来跟踪远程分支,您可以使用 git checkout <远程分支名称> 或 git switch <远程分支名称> 。如果你有一个文件或文件夹与你的远程分支名称相同,
git checkout
会输出一些错误消息,但git switch
可以正常工作!示例:
查看所有分支,我们要创建一个本地分支来跟踪远程分支
remotes/origin/asd
,并且我们还有文件名asd
:文件名与远程分支相同,如果我们使用 git checkout 命令创建本地分支来跟踪远程分支,Git 应该输出一些错误消息
如果我们使用 <代码>git开关!
TL;DR
Using
git switch
rather thangit checkout
. More details are on this page.I think the answer is obsolete. Git split some functions of
checkout
toswitch
andrestore
now.The following is my summary:
If you want to update something for a remote branch, you should create a local branch to "track" the remote branch. You can update anything you want in local and finally push to remote. If you check out to the remote branch directly after cloning your repository, you may see the "detached HEAD" status and the following message from Git:
So how can we create a local branch to track a remote branch?
To create a local branch to track a remote branch, you can use
git checkout <remote branch name>
orgit switch <remote branch name>
. If you have a file or folder has same name as your remote branch name,git checkout
would output some error message, butgit switch
can work normally!Example:
See all branches, and we want to create a local branch to track the remote branch
remotes/origin/asd
, and we also have the file nameasd
:The filename is same as remote branch, and Git should output some error messages if we are using the
git checkout
command to create a local branch to track a remote branchIt works if we are using
git switch
!从远程获取并签出分支。
例如:
Fetch from the remote and checkout the branch.
E.g.:
其他人给出了解决方案,但也许我可以告诉你原因。
不等于不起作用,所以我猜当您在终端中输入 'git checkout test' 并按 Enter 键时,没有消息出现,也没有发生错误。我说得对吗?
如果答案是“是”,我可以告诉你原因。
原因是您的工作树中有一个名为“test”的文件(或文件夹)。
当
git checkout xxx
解析时,xxx
视为分支名称,但没有任何名为 test 的分支。xxx
是一个路径,幸运的是(或者不幸的是),有一个名为test.txt的文件。所以 git checkout xxx 意味着放弃 xxx 文件中的任何修改。xxx
的文件,那么Git会尝试根据一些规则创建xxx
。其中一条规则是,如果remotes/origin/xxx
存在,则创建一个名为xxx
的分支。Other guys and gals give the solutions, but maybe I can tell you why.
Does nothing
doesn't equaldoesn't work
, so I guess when you type 'git checkout test' in your terminal and press enter key, no message appears and no error occurs. Am I right?If the answer is 'yes', I can tell you the cause.
The cause is that there is a file (or folder) named 'test' in your work tree.
When
git checkout xxx
parsed,xxx
as a branch name at first, but there isn't any branch named test.xxx
is a path, and fortunately (or unfortunately), there is a file named test. Sogit checkout xxx
means discard any modification inxxx
file.xxx
either, then Git will try to create thexxx
according to some rules. One of the rules is create a branch namedxxx
ifremotes/origin/xxx
exists.获取新创建的分支
切换到另一个分支
To get newly created branches
To switch into another branch
git checkout -b "Branch_name" [ B 表示创建本地分支]
gitbranch --all
git checkout -b "您的分支名称"
gitbranch
git pull origin "您的分支名称"
成功从 master 签出分支到开发分支
git checkout -b "Branch_name" [ B means Create local branch]
git branch --all
git checkout -b "Your Branch name"
git branch
git pull origin "Your Branch name"
successfully checkout from the master branch to dev branch