Git を使えるようになるまで頑張る その 1

Git のいろは

ここを見ながら適当に色々やってみた結果です.

動作環境は,さくら VPS 512 上の Ubuntu 10.04 です.

$ sudo aptitude install git-core

で Git はインストールされています (RVM のために既にインストールはしていました).

まず,使えるエディタを Vim に変更したかったので,Twitter で軽く質問的な tweet を投げました.そして返って来た返答がこちら.

$ git config --global core.editor vim

※core.editor = vim と誤って記載していたのを修正

もしくは

$ export EDITOR=vim

後者の方は,.bashrc なり .zshrc なりに書いておけばいいでしょう.

とりあえず私は前者を実行しました.このとき,何のレスポンスも無かったので変更されてるのかどうか分かりませんでしたが,Twitter で man git-var で解説を見てご覧と言われたので,見てみたら,どうやら

$ git var -l

で確認出来そうでした.実際 core.editor = vim となっていました.

また,この設定は $HOME/.gitconfig にも記載されていました.

さて,Git のいろはの最初の方だけを見ながら,ひとまず rollback 的な動作はまったく考えず,commit の繰り返しだけとにかくやってみよう,ということで実験.

$ cd ~
$ mkdir -p work/git/test0
$ cd work/git/test0

とりあえず適当に Git 実験用ディレクトリ作ってそこに移動して…

$ git init

はい,なんとこれだけでリポジトリが作られるようです.

ここで Git の幾つかの機能を実験.

$ git status
$ git diff
$ git log

git status で

# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)

…と言われますね.

git diff は何も出ません.そりゃ何もしてないのだから差分は何も無いわけですな.

git log も

fatal: bad default revision 'HEAD'

まだログは何も無い.エラー的な文章になってる意味は分かりません.

さて,ファイルを作ってみます.

$ touch a.txt

ここで同様に 3 つのコマンドを実行してみると git status だけ結果が変わってます.

# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       a.txt
nothing added to commit but untracked files present (use "git add" to track)

ははぁ.a.txt を git add でどうにかしなさい,というような感じですね.

git add は,Git のいろはでも「追跡対象に追加する」という説明以外は特に説明もなく出てますね.

じゃあやりましょう.

$ git add a.txt
$ git commit

git commit した時点で Vim が起動されたと思います.されてない人は… 環境変数読み直すとかが出来てない可能性が…?

で,この辺 Subversion と同じで私は何となく分かります.要するに,1 行目に commit message を追加すればいいのでしょう.2 行目以降の # で始まる行は,全部コメントとなり無視される…と.ああ,書かれていますね.

Please enter the commit message ... with '#' will be ignored, ...

では早速 1 行目に

add a.txt

とでも書いて,保存して終了します.

すると,おっ?何か出ましたね.

[master (root-commit) 65e64e5] add a.txt
 Committer: ka <ka@XXX.sakura.ne.jp>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

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

If the identity used for this commit is wrong, you can fix it with:

    git commit --amend --author='Your Name <you@example.com>'

 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a.txt

コミット出来たような気がします.

$ git log

で,何やらログが見えますね.

commit 65e64e5cb591a3f4740bf5448ede40ab18c0d812
Author: ka <ka@XXX.sakura.ne.jp>
Date:   Tue Sep 27 23:43:39 2011 +0900

    add a.txt

さて,では a.txt をちょいと編集してみましょう.abcd とでも追記します.

で,

$ git diff

すると

diff --git a/a.txt b/a.txt
index e69de29..acbe86c 100644
--- a/a.txt
+++ b/a.txt
@@ -0,0 +1 @@
+abcd

おっ.差分出ますね.

次は git add しないで commit してみます.

$ git commit
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   a.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

エラーです.add しないと,commit 出来ないんですね.理由はまだよく分かりません.

さて,git commit には -a オプションを付けるとなんだか add をすっ飛ばせそうな雰囲気が Git のいろはの 18 ページから漂ってきます.試しましょう.

$ git commit -a

おお!エディタ開きますね!で,同じく commit message を書いて,保存して終了.

[master 0716bf5] add abcd to a.txt
 Committer: ka <ka@www8070u.sakura.ne.jp>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

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

If the identity used for this commit is wrong, you can fix it with:

    git commit --amend --author='Your Name <you@example.com>'

 1 files changed, 1 insertions(+), 0 deletions(-)

commit message は add abcd to a.txt としてみました.

で,git log を見てみると…

$ git log
commit 0716bf575448159ce6e316eb11881753326e9b2f
Author: ka <ka@XXX.sakura.ne.jp>
Date:   Tue Sep 27 23:51:41 2011 +0900

    add abcd to a.txt

commit 65e64e5cb591a3f4740bf5448ede40ab18c0d812
Author: ka <ka@XXX.sakura.ne.jp>
Date:   Tue Sep 27 23:43:39 2011 +0900

    add a.txt

ふむぅ.ログ見えますね.

とりあえず,今日はここまで.