akiyoko blog

akiyoko の IT技術系ブログです

Gitメモ ~ clone 編 ~

Gitを使い始めて、そろそろ1年になります。
だいぶ慣れたかと思いきや、急につまずくこともあったり。
なので、整理のためにも自分なりにまとめていこうと思います。

まずは初歩的なところから。


Githubからローカルにcloneする

やり方がいくつかあるので、まとめてみました。


1. HTTPSを使う場合

一番シンプルなのがこれ。

$ git clone https://github.com/twitter/bootstrap.git


clone以下のURLは、Githubの画面上部に表示されている、

f:id:akiyoko:20120704214534p:plain

赤枠の部分をコピペすればよいです。


環境によっては、プロキシの設定が必要な場合もあるかもしれません。
export http_proxy=http://< proxy_address >:< proxy_port >/


ちなみに、HTTPS以外にも HTTP や GITプロトコルを指定してもOKみたいですね。*1

$ git clone http://github.com/twitter/bootstrap.git
$ git clone git://github.com/twitter/bootstrap.git


 

2. SSHを使う場合

GitHubにSSH公開鍵を登録することで、SSH経由でアクセスすることができます。
後々もGithubを使って開発をするのであれば、SSH公開鍵を登録してしまった方が何かと楽かもしれません。なお、Githubアカウント必須です。


まずは、SSH鍵ペアを作成。
パスフレーズは無し、鍵ファイル名は「~/.ssh/github_rsa」とします。*2

$ ssh-keygen


Githubにログインして、[Account Settings] -> [SSH Keys] -> [Add SSH key] から、
作成した公開鍵(~/.ssh/github_rsa.pub)の内容をコピペ。

f:id:akiyoko:20120705003204p:plain


f:id:akiyoko:20120705003214p:plain

~/.ssh/config を設定。

Host github.com
  User git
  Hostname github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/github_rsa


github.com に接続テスト。

$ ssh git@github.com

Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,207.97.227.239' (RSA) to the list of known hosts.
PTY allocation request failed on channel 0
Hi akiyoko! You've successfully authenticated, but GitHub does not provide shell access.
                                                                                        Connection to github.com closed.


テストOKなら、cloneしてみる。

$ git clone ssh://git@github.com/twitter/bootstrap.git

# 「ssh://」を省略して、「:」で繋いでもOK
$ git clone git@github.com:twitter/bootstrap.git


参考

*1: プライベートリポジトリの場合にはこのURLタイプは利用できないようです。 https://help.github.com/articles/which-remote-url-should-i-use

*2: 公開鍵ファイル名を「~/.ssh/id_rsa(デフォルト)」のまま変更しなければ、以降の ~/.ssh/config の編集作業を省略できるが、さすがに後々の使い勝手を考えると別名にしておいた方がよさげ。