gitのインストールからgithubの登録まで

最近人気のgithubを試してみました。

gitさわるの初めてだったのでインストールするところから書いて行きます!

まずはgitのインストール

触ってるサーバがCentOS 5.1なので、yumってみることにしました。

/etc/yum.repos.d/CentOS-Base.repo に以下を追記。

[dag]
name=Dag RPM Repository for Redhat EL5
baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag
gpgcheck=1
enabled=1
gpgkey=http://dag.wieers.com/packages/RPM-GPG-KEY.dag.txt

あとはyumでインストールするだけ。

[miki@server ~] yum install git

ラクチンでした。

gitレポジトリを作ってみる

基本的な使い方を覚えるためにまずはちょっと練習してみます。
適当なディレクトリを掘ってそこでイニシャライズの呪文を唱えます。

[miki@server ~]$ mkdir test
[miki@server ~]$ cd test/
[miki@server test]$ ls
[miki@server test]$ git init
Initialized empty Git repository in .git/
[miki@server test]$ 

おお、svnでレポジトリ作るのに比べると遥かに簡単だ!

次はここで適当なファイルを作ってみます。

[miki@server test]$ echo 'print "hoge";' > test.pl
[miki@server test]$ cat test.pl 
print "hoge";
[miki@server test]$ 

ファイルが出来たらaddしてみる。ついでにstatusも見てみる。

[miki@server test]$ git add test.pl 
[miki@server test]$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file: test.pl
#
[miki@server test]$ 

ふむ。ではcommitしてみましょう。

[miki@server test]$ git commit -m "初コミット"

*** Your name cannot be determined from your system services (gecos).

Run

  git config user.email "you@email.com"
  git config user.name "Your Name"

To set the identity in this repository.
Add --global to set your account's default

fatal: empty ident  <miki@server> not allowed
[miki@server test]$ 

あらら?おこられた。
どうやらメアドと名前をconfigで設定する必要があるらしい。メンドクセー。
こんなかんじでconfig登録してからもう一度commitしてみます。

[miki@server test]$ git config user.email "miki@cpan.org"
[miki@server test]$ git config user.name "miki"
[miki@server test]$ git commit -m "初コミット"
Created initial commit 250e828: 初コミット
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 test.pl
[miki@server test]$ 

statusとlog見てみる。

[miki@server test]$ git status
# On branch master
nothing to commit (working directory clean)
[miki@server test]$
[miki@server test]$
[miki@server test]$ git log
commit 250e8286935a00656e46a198220b8284ff5713c3
Author: miki <miki@cpan.org>
Date:   Tue Dec 23 00:33:37 2008 +0900

    初コミット
[miki@server test]$ 

うん、まぁこんな感じになるんだな。今までのところはまぁ何となく想定の範囲内だぞ。

branchを作ってみる

ここら辺からちょっとsvnとは様子が変わってきます。

まずgit branchとしてみます。

[miki@server test]$ ls
test.pl
[miki@server test]$ git branch
* master
[miki@server test]$ 

git branchは現在のbranchの一覧を表示するコマンドだそうです。
どうやらデフォルトでmasterという名前のbranchが作られてるみたいです。

つぎにnew_branchという名前のbranchを作ります。

[miki@server test]$ git branch new_branch
[miki@server test]$ 
[miki@server test]$ git branch
* master
  new_branch

masterとnew_branchが表示されるようになりました。
ちなみにmasterのほうに*がついていますが、これが着いている方がカレントを意味しています。

では次にnew_branchの方にしてみましょう。
git checkout でbranchをチェンジします。

[miki@server test]$ git checkout new_branch
Switched to branch "new_branch"
[miki@server test]$ git branch
  master
* new_branch
[miki@server test]$ 

new_branchの方がカレントになりましたね。

svnのときはbranchとかtrunkとかは単なるディレクトリでしたが、gitではちょっと違う概念で構成されているようです。
なんと表現すればいいのかな、、実態のファイルに対して「モード」が色々あるような、そんなイメージでしょうか。

branchのマージ

branchのマージについてもだいぶ簡単にできるようになってます。
まず先ほど作ったnew_branchを少し弄ってみましょう。

[miki@server test]$ echo 'exit;' >> test.pl 
[miki@server test]$ cat test.pl 
print "hoge";
exit;
[miki@server test]$ 

この状態でcommitしてみます。

[miki@server test]$ git commit -a -m "exitを追加"
Created commit c9fcbb6: exitを追加
 1 files changed, 1 insertions(+), 0 deletions(-)
[miki@server test]$ 

commitのオプションで-aと付けていますが、これはaddの略です。
svnではaddは新規にファイルやフォルダを追加する場合に使いましたが、gitでは変更の場合にもaddが必要となるようです。よくわかりませんが、そういうものらしいです。で、そのめんどくさいaddをcommit時にまとめて行うのが-aオプションです。ふーん。

さてさて、それではマージをしてみましょう。new_branchからmasterにチェンジして、そこからnew_branchをマージしてみます。

[miki@server test]$ git branch 
  master
* new_branch
[miki@server test]$ git checkout master
Switched to branch "master"
[miki@server test]$ git merge new_branch
Updating 250e828..c9fcbb6

Fast forward
 test.pl |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
[miki@server test]$ 

うまくマージできました。簡単でしたね!

実際にはconflictを起こしてしまうと、それはそれで面倒なことになるかと思いますが、まぁその時は適当に頑張って解決してください。

つぎにすすみます。

ちがうところのレポジトリを持ってくる

"git clone <レポジトリ> <持ってきたいディレクトリ>"と指定すればオKです。

[miki@server test]$ cd ../
[miki@server ~]$
[miki@server ~]$ git clone test test2
Initialized empty Git repository in /home/miki/test2/.git/
remote: Generating pack...
remote: Done counting 6 objects.
remote: Deltifying 6 objects...
remote:  100% (6/6) done
remote: Total 6 (delta 0), reused 0 (delta 0)
Indexing 6 objects...
 100% (6/6) done


[miki@server ~]$ 

ここでtest2のレポジトリの上でいろいろ変更を加えるとします。

[miki@server ~]$ cd test2
[miki@server test2]$ ls
test.pl
[miki@server test2]$ echo "aaaaa" >> test.pl 
[miki@server test2]$ cat test.pl 
print "hoge";
exit;
aaaaa
[miki@server test2]$ 

書くのが面倒になってきたので変更部分は超適当になってきました!!

それではこいつをcommitしましょう。

あ、そうそう、ここでもconfigでメアドと名前の指定が必要となります。

[miki@server test2]$ git config user.email "hoge@hoge"
[miki@server test2]$ git config user.name "fuga"
[miki@server test2]$ 
[miki@server test2]$ git commit -a -m "適当な変更"
Created commit 4a7c49a: 適当な変更
 1 files changed, 1 insertions(+), 0 deletions(-)
[miki@server test2]$ 

それでは元々のレポジトリ(test)に戻って、test2の変更を引っ張ってきましょう。

[miki@server test2]$ cd ../test
[miki@server test]$ 
[miki@server test]$ 
[miki@server test]$ git pull ../test2
remote: Generating pack...
remote: Done counting 5 objects.
Result has 3 objects.
remote: Deltifying 3 objects...
remote: /3) done/3) done
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking 3 objects...
 100% (3/3) done
Updating c9fcbb6..4a7c49a

Fast forward
 test.pl |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
[miki@server test]$ 

別レポジトリの変更を一気に引っ張ってくることができました。

まさにsvkっぽい挙動です。

はじめてのgithub

gitの使い方はこの辺にしておきます。あとはgit チュートリアルをみてください。

本題のgithubに入ります。


http://github.comにアクセスしsign up nowをクリックします。

「OpenSource Free」から登録を進めます。ここらへんの説明は割愛しますがsshの公開鍵が必要となるので、事前に準備しておく必要があります。

あ、そうそう、メアドのところにcpanのメールアドレス入れたら勝手に顔写真がはいりました。Gravatarに対応してるっぽいですね。

さてさて、アカウント登録が終わってログイン状態になっていると仮定して、そこから話を続けます。

「Create a Repository」というリンクがあるのでこれをクリックしレポジトリを作ります。

今回はすでにCPANに上げていたモジュールをサンプル的に1つ2つそのままgithubに上げてみました。URLは http://github.com/miki/ です。

ここから先は特筆すべきことはないですね。。淡々とメニューに従って進んで行くだけです。

レポジトリ登録がおわると「次のステップ以降でこれこれをしなさいよ」という感じで懇切丁寧に手順を表示してくれるので、こいつを守ってコマンドを打ち込んでください。(これは本当に親切です)

感想

駆け足でgitを触ってみた訳ですが、率直な感想「svkとそんなには変わらんかなぁ」という印象です。

あえてsvkよりもよい点を上げるとすれば「インストールが断然ラク」という点でしょうか。svkは何気にインストールが難関だったりしますので。。

あとgitの何気にいい点をもう1つあげるとすると.gitという隠しディレクトリかな。

これはsvnでいうところの.svnと同じようなものなのですが、svnだと各ディレクトリの最下層のところまですべてに.svnが存在してましたが、gitだと最上位のディレクトリだけに置かれています。

じつはこれは意外と嬉しい!

tab補完でディレクトリたどって行く時に.svnがあるといちいちディレクトリのあるところで詰まるので、階層の深いプロダクトダッタリすると「イラっ」とする回数が異常に多かったのですが、gitだとそのストレスが全くないですね。平和です。

ただし、githubのサイトの反応が超遅くて、とてもイライラしましたが。。あまりにも遅杉。。


まぁ、なにはともあれ、githubにデビューできました。めでたし。めでたし。


(参考サイト)
チュートリアル (バージョン 1.5.1 以降用)
とっても優しい github の使い方