githubでフォークしたコードをローカルに取得&githubにコミット

前回、git開発環境をUbuntu上で整え、github上でNOVさんのiKnow! gemのコードをフォークするところまでやりましたが、次にローカルでこのフォークしたソースコードをいじって遊びたいと思います。githubの自分のアカウントにコードをアップするところまでを今回の目標とします。

githubからソースコードを取得

まず、githubからローカルへの取得は以下のように簡単にできます。

$ git clone git://github.com/fujibee/iknow.git
Initialized empty Git repository in /home/fujibee/work/tmp/iknow/.git/
remote: Counting objects: 499, done.
remote: Compressing objects: 100% (443/443), done.
remote: Total 499 (delta 253), reused 111 (delta 31)
Receiving objects: 100% (499/499), 146.27 KiB | 62 KiB/s, done.
Resolving deltas: 100% (253/253), done.
$ ls -F iknow/
ChangeLog  README  Rakefile  examples/  iknow.gemspec  lib/  test/

設定とか

それで、取得したコードに対して修正を加え、コミットするわけですが、そのまえに設定に気をつける必要があるようです。

あ、私のようなgit初心者の方は、まず最初に

$ git config --global user.name "Keita Yamaguchi"
$ git config --global user.email keita.yamaguchi@gmail...

のように名前とメールアドレスを設定しておいて下さい。これを忘れるとマシン名とかがコミットログに残って恥ずかしい感じになります。

とっても優しい github の使い方 - ¬¬日常日記

私もgit初心者なので、その通りにします。

$ git config --global user.name "fujibee"
$ git config --global user.email "fujibee@gmail..."

ほかの設定値についてはここらへんが参考になります。

http://d.hatena.ne.jp/uneasy/20081225/1230212101

ローカルへコミット

gitはまだ詳しくないのでやってみた結果の動作を見てですが、まずローカルリポジトリ(ローカルコピーとは違う)にコミットして、そのあとリモートリポジトリ(githubとか)にpushして公開する、という方法のようです。

ローカルリポジトリへのコミットは、Subversionのように簡単にできます。が、事前にコミット対象を明示的に指定しておかないといけないようです。

$ git add iknow.gemspec Rakefile
$ git commit

commitコマンドの後は、viなどでコミットログを書き込みます。

githubに初めてのpushへの道のり

さらに、ローカルリポジトリからgithubへのpushには、認証としてgithubへの公開鍵の登録が必要になるようです。
この辺を参考に、進めてみます。

Connecting to GitHub with SSH - GitHub Help

まず、ローカルでssh keyを作成して、それを、githubに登録します。

$ cd ~/.ssh
-bash: cd: /home/fujibee/.ssh: No such file or directory

とりあえずないですね。上記ページにしたがって、作成します。

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/fujibee/.ssh/id_rsa):
Created directory '/home/fujibee/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/fujibee/.ssh/id_rsa.
Your public key has been saved in /home/fujibee/.ssh/id_rsa.pub.
The key fingerprint is:
7a:51:2a:7a:dd:79:06:be:58:dd:94:0d:54:9d:38:6d fujibee@ubuntu810server
The key's randomart image is:
+--[ RSA 2048]----+
|              +.+|
|             + E.|
|          .   +  |
|         o     + |
|      . S .   o .|
|     . + + + o   |
|    . o o = + .  |
|     . . o +     |
|        . .      |
+-----------------+

できました。最後のrandomartってのは何なのかな?2次元的なfingerprintのようなものでしょうか?面白そうですが、ここでは深入りしないようにします。

$ cat ~/.ssh/id_rsa.pub

で出てくる、公開鍵をgithubに登録します。登録先は、ログイン後、アカウントページにあります。

では、さっそくgithubにpushしてみましょう。

$ git push
fatal: protocol error: expected sha/ref, got '
*********'

You can't push to git://github.com/user/repo.git
Use git@github.com:user/repo.git

*********'

ふむ、URLの表現がまずいようです。エラーメッセージに従い、git@のほうでやってみます。

$ git push git@github.com:fujibee/iknow.git
The authenticity of host 'github.com (65.74.177.129)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes

初回なのでキー追加OKかどうか聞かれました。

Warning: Permanently added 'github.com,65.74.177.129' (RSA) to the list of known hosts.
Enter passphrase for key '/home/fujibee/.ssh/id_rsa':

で、秘密鍵のパスワード入力です。

Counting objects: 7, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 395 bytes, done.
Total 4 (delta 3), reused 0 (delta 0)
To git@github.com:fujibee/iknow.git
   5157e51..eb2e66d  master -> master

無事、pushできました。

実際、githubリポジトリにも反映されています!


余談

実はコミットしたのは今日(12/28)なのですが、github上のコミット日時が12/25になっています。なぜかというと、ローカルのマシンの時計がすごく遅れていて12/25になっていたからです。。これで気づきました。VMWare上のサーバは(それもノート上でスリープとかサスペンドとかやっている場合特に)時計が狂いやすいので、気をつけましょう。。