118
108

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ローカルgitリポジトリでリモートのリポジトリURL確認方法

Last updated at Posted at 2017-03-24

Gitを使っていると、ローカルにクローンしたリポジトリが、どこのリモートと接続されているか確認したい場面が多くあります。

本記事では、git cloneした後に、リモートURL(origin)を確認する方法を7つにまとめて紹介します。
それぞれの方法には特徴があるので、用途や状況に応じて使い分けてみてください。

実行環境の前提

以下のリポジトリをローカルにクローンして作業しています。

$ git clone git@github.com:ruby/ruby.git ruby
$ cd ruby

方法

方法1 git config --get remote.origin.url

一番シンプルにURLだけを確認するならこのコマンドです。

$ git config --get remote.origin.url

出力例:

git@github.com:ruby/ruby.git
  • メリット:URLだけを取得したいときに便利。
  • デメリット:fetch/pushなどの詳細は分からない。

方法2 git remote -v

リモート名(origin)と、そのfetch/pushのURLを一覧で確認できます。

$ git remote -v

出力例:

origin  git@github.com:ruby/ruby.git (fetch)
origin  git@github.com:ruby/ruby.git (push)
  • メリット:fetch/pushの両方を同時に確認できる。
  • デメリット:詳細な情報までは出ない。

方法3 git remote show -n origin

-n オプションを使うことで、ネットワークアクセスなしで情報を取得できます。

$ git remote show -n origin

出力例:

* remote origin
  Fetch URL: git@github.com:ruby/ruby.git
  Push  URL: git@github.com:ruby/ruby.git
  HEAD branch: (not queried)
  • メリット:オフライン環境でも確認できる。
  • デメリット:ブランチ情報などは取得できない。

方法4 git remote show origin

ネットワークに接続されていれば、より詳細な情報が確認できます。

$ git remote show origin

出力例(抜粋)

* remote origin
  Fetch URL: git@github.com:ruby/ruby.git
  Push  URL: git@github.com:ruby/ruby.git
  HEAD branch: trunk
  Remote branches:
    ruby_1_3
    ruby_1_4
    ...

  • メリット:リモートのブランチやHEADの情報まで把握できる。
  • デメリット:ネットワークアクセスが必要。

方法5 .git/configファイルを直接確認

Gitの内部設定ファイルを直接見る方法です。

$ cat .git/config

出力例:

[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
[remote "origin"]
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = git@github.com:ruby/ruby.git
  • メリット:Gitの仕組みを理解しやすく、スクリプトからも利用しやすい。
  • デメリット:表示情報が多く、読みにくいことも。

方法6 git config -l

Gitの設定全体をリスト表示し、その中からリモートURLを探す方法です。

$ git config -l

出力例

...
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
remote.origin.url=git@github.com:ruby/ruby.git
  • メリット:Git全体の設定と一緒に確認できる。
  • デメリット:大量の情報が出るため、目的の項目を探すのが大変な場合も。

方法7 git remote get-url origin

Git 2.7以降で使える便利なコマンド。origin のURLをシンプルに取得できます。

$ git remote get-url origin

出力例

git@github.com:ruby/ruby.git
  • メリット:URLだけを取得したい時に非常にスマート。
  • デメリット:Gitのバージョンが古いと使えない。

方法8 git ls-remote

リモートURLを明示的に指定して、リモートの参照一覧を取得することで、接続確認とURL確認を同時に行う方法です。

$ git ls-remote origin
aef29b7c7a0c9b1f786a8e297d0e6adf0f1d1f60        HEAD
aef29b7c7a0c9b1f786a8e297d0e6adf0f1d1f60        refs/heads/trunk
...

おまけ:Gitのヘルプを活用しよう

コマンドの詳細を調べたいときは、、git helpを活用すれば、何でも調査できる。例

$ git remote show --help
usage: git remote show [<options>] <name>

    -n                    do not query remotes

ヘルプを読む習慣をつけておくと、トラブル対応力が格段に上がります。

参照

118
108
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
118
108

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?