在git中,如何检出远程存储库的远程分支? [英] In git, how do I check out a remote repository's remote branches?

查看:156
本文介绍了在git中,如何检出远程存储库的远程分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个裸露的远程存储库克隆了一个本地存储库。以下命令列出远程存储库的分支。

  $ git ls-remote< remote> 

74bd3eb190edb39db04f6c0c4dbbb9e1e96bc6db refs / remotes / test
85de54d6ae813c624b9623983e6b0a4948dae0fe refs / remotes / trunk

我想结帐并追踪该远端的远程分支 trunk 。我该怎么做?



请注意,这与检查远程存储库的本地分支不同。
这就是远程仓库的样子。

  $ git branch -a 

master
remotes / test
remotes / trunk

运行 git获取<远程> 来获取所有远程存储库的分支,我在本地存储库中获取该输出。

  $ git分支-r 

回购/ HEAD - > repo / master
repo / master


解决方案

您可以从任何远程获取任何引用(只要服务器愿意将它提供给您)。在 refs / heads / 之外获取ref的关键是提供以 refs / 开头的全部ref路径。如果需要,您甚至可以从未配置为远程的存储库中取出( git fetch 可以取URL而不是远程名称)。



默认情况下,配置的远程只能从远程仓库的 refs / heads / 命名空间中获取,因此它们不会在 refs / remotes / 。但是,你可以使用像 refs / remotes / trunk remotes / trunk 可能也有效,但也可能不明确)。

如果fetched refspec没有指定目标ref,它将被存储在特殊的FETCH_HEAD ref中。

p>




将存储库的 refs / remote / trunk 取入FETCH_HEAD并检查作为一个独立的HEAD:

  git fetch remote-name-or-url refs / remotes / trunk&& 
git checkout FETCH_HEAD

相同,但创建一个命名的本地分支,而不是使用分离HEAD:

  git fetch remote-name-or-url refs / remotes / trunk&& 
git checkout -b trunk -from-remote FETCH_HEAD

同样,但是直接进入本地分支:

  git fetch remote-name-or-url refs / remotes / trunk:trunk-from-remote&& 
git checkout trunk-from-remote

如果您正在使用已配置的远程设备,可以重写它的 remote。< remote-name> .fetch 配置,并添加一个额外的条目以自动从 refs / heads / refs / remotes /

 #fetch branchs远程进入remote-name / heads / * 
git config remote.remote-name.fetch'+ refs / heads / *:refs / remotes / remote-name / heads / *'&&
#远程访问远程名称/遥控器/ *
的遥控器git config --add remote.remote-name.fetch'+ refs /遥控器/ *:参考/遥控器/远程名称/遥控器为了避免可能的冲突,上面的例子配置了提取以将裁判存储到不相交的命名空间中(


$ b $> code> ... / heads /
... / remotes / )。如果你喜欢,你可以选择不同的名字。如果您确定不存在冲突,您甚至可以直接在 refs / remotes / remote-name / 下填充它们。


I have a local repository cloned from a bare remote repository. The following command lists al the remote repository's branches.

$ git ls-remote <remote>

74bd3eb190edb39db04f6c0c4dbbb9e1e96bc6db    refs/remotes/test
85de54d6ae813c624b9623983e6b0a4948dae0fe    refs/remotes/trunk

I want to checkout and track that remote's remote branch trunk. How do I do that?

Note that this is different from checking out a remote repository's local branch. This is how the remote repository looks like.

$ git branch -a

master
remotes/test
remotes/trunk

After running git fetch < remote > to fetch all of the remote repository's branches, I get this output in the local repository.

$ git branch -r

repo/HEAD -> repo/master
repo/master

解决方案

You can fetch any ref from any remote (as long as the server is willing to give it to you). The key to fetching refs outside refs/heads/ is to supply full ref paths that start with refs/. If desired, you can even pull from repositories that are not configured as remotes (git fetch can take a URL instead of a remote name).

By default, configured remotes will only fetch from the remote repository’s refs/heads/ namespace, so they will not pick up anything inside refs/remotes/. But, you could refer to a ref inside it by using a complete ref like refs/remotes/trunk (remotes/trunk might also work, but might also be ambiguous).

If the fetched refspec does not specify a destination ref, it will be stored in the special FETCH_HEAD ref.


Fetch a repository’s refs/remote/trunk into FETCH_HEAD and check it out as a detached HEAD:

git fetch remote-name-or-url refs/remotes/trunk &&
git checkout FETCH_HEAD

Same, but create a named, local branch instead of using a detached HEAD:

git fetch remote-name-or-url refs/remotes/trunk &&
git checkout -b trunk-from-remote FETCH_HEAD

Same, but directly into a local branch:

git fetch remote-name-or-url refs/remotes/trunk:trunk-from-remote &&
git checkout trunk-from-remote

If you are working with a configured remote, you can rewrite its remote.<remote-name>.fetch configuration and add an extra entry to automate fetching from both refs/heads/ and refs/remotes/.

# fetch branchs of remote into remote-name/heads/*
git config remote.remote-name.fetch '+refs/heads/*:refs/remotes/remote-name/heads/*' &&
# fetch remotes of remote into remote-name/remotes/*
git config --add remote.remote-name.fetch '+refs/remotes/*:refs/remotes/remote-name/remotes/*'

To avoid possible collisions, the above example configures fetch to store refs into disjoint namespaces (…/heads/ and …/remotes/). You can pick different names if you like. If you are sure there will be no conflicts, you can even stuff them both directly under refs/remotes/remote-name/.

这篇关于在git中,如何检出远程存储库的远程分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆