我有一个 Git 仓库。该仓库有多个远程仓库(我想是这样的)。如何获得属于该仓库的远程仓库列表?
类似于 `git list --remotes` 这样的命令吗?
类似于 `git list --remotes` 这样的命令吗?
你可以使用以下命令获取任何配置的远程URL列表:
git remote -v
这将会给你类似下面的内容:
base /home/***/htdocs/base (fetch)
base /home/***/htdocs/base (push)
origin git@bitbucket.org:*** (fetch)
origin git@bitbucket.org:*** (push)
如果您只需要远程仓库的名称(而不是其他任何数据),那么简单的git remote
即可。
$ git remote
iqandreas
octopress
origin
到目前为止,答案已经告诉您如何找到现有的分支:
git branch -r
或者是同一项目的代码库[请见下方注释]:
git remote -v
还有另一种情况。您可能想了解托管在同一服务器上的其他项目存储库。
为了发现这些信息,我使用SSH或PuTTY登录主机,并使用ls
命令查找包含其他存储库的目录。例如,如果我通过输入以下命令来克隆存储库:
git clone ssh://git.mycompany.com/git/ABCProject
并且想知道还有什么其他的选择,我通过SSH或PuTTY登录到git.mycompany.com并输入:
ls /git
假设ls
命令的输出如下:
ABCProject DEFProject
我可以使用这个命令
git clone ssh://git.mycompany.com/git/DEFProject
获取访问其他项目需要使用git remote
命令。
注意:通常
git remote
命令只会显示关于origin
的信息 -- 即我克隆该项目的仓库。如果你与另外两个或更多人协作开发同一个项目,并且直接访问彼此的存储库而不是通过origin
传递所有内容,则可以使用git remote
命令。
就我个人而言,我有完全相同的问题,但我在这里找不到答案。它可能不是可移植的,但至少对于gitolite,我可以运行以下命令来获得我想要的结果:
$ ssh git@git.xxx.com info
hello akim, this is gitolite 2.3-1 (Debian) running on git 1.7.10.4
the gitolite config gives you the following access:
R W android
R W bistro
R W checkpn
...
查看远程分支的简单方法是:
git branch -r
查看本地分支:
git branch -l
git ls-remote
命令可以显示所有可从上游服务器获取的远程分支 (https://git-scm.com/docs/git-ls-remote)。我认为此页面上的所有答案都只告诉你如何列出已经获取的远程分支和跟踪分支,这是一个进退两难的局面。在你知道如何在上游服务器上引用它之前,你无法获取远程分支。 - Reb.Cabin这些方法都不能按照问题提问者的要求工作,而我也经常需要这样的功能。例如:
$ git remote
fatal: Not a git repository (or any of the parent directories): .git
$ git remote user@bserver
fatal: Not a git repository (or any of the parent directories): .git
$ git remote user@server:/home/user
fatal: Not a git repository (or any of the parent directories): .git
$ git ls-remote
fatal: No remote configured to list refs from.
$ git ls-remote user@server:/home/user
fatal: '/home/user' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
这样做的主要目的是,除了远程用户和服务器,您没有任何信息,并希望找出您可以访问什么。
大多数答案都假定您是在git工作集内查询。而提问者则假设您不在其中。
一个实际的例子是,假设服务器上有一个名为foo.git的代码库。 某人决定明智地将其更改为foo2.git。这时能够列出服务器上的git目录会非常好。是的,我看到了对git的问题。但仍然很好用。
git remote -v
是为了显示详细信息。git remote
只会简单地列出远程仓库的名称(在这个例子中是 base 和 origin)。使用 -v 选项可以显示每个远程仓库的获取和推送操作的 URL。 - dhjgit branch
支持。基本区别在于短选项与长选项(实质上是单破折号与双破折号)。虽然如此,为remote
子命令支持 --list 也是有意义的。如果您真的需要它,我建议提交一个功能请求。 - Randallgit remote -v
时什么都没有。 - Adam Cox