SlideShare a Scribd company logo
1 of 47
Download to read offline
理解したつもりになるGit入門




       2010/10/02 Hokkaido.pm #2
     株式会社アイダック CPS事業部
                      佐々木義広
自己紹介

•   佐々木義広
•   Twitter @aloelight
•   Blog http://blog.vitamin11.org/
•   プログラマ 兼 サーバ管理者
今日の内容 

• Gitとは
• 簡単なGitの使い方
• SubversionとGitの違い
Gitとは

Git(ぎっと)はプログラムなどのソースコード管理を行う分
散型バージョン管理システム。動作速度に重点が置かれている。
Linuxカーネルのソースコード管理を目的として、リーナス・
トーバルズによって開発された。
                     Wikipediaより引用
バージョン管理システムとは

主にプログラムのソースコード等のファイルの変更履歴を管理
するシステム
バージョン管理を行う理由

 • 誰がソースを変更したのかすぐわかる
      commitのauthorをみれば一発

 • 不具合があれば簡単に前のバージョンに戻せる
      index.html.2010102 ← こういう不幸なファイルが生まれな
い

 • 複数人が平行して開発する状況を想定している
        可能なら自動でmerge,ダメなら衝突を報告してくれる
メジャーなバージョン管理システム

•   Bazaar
•   CVS
•   Subversion
•   Mercurial
•   Monotone
•   その他色々
Gitを使う理由

•   Perl本体のバージョン管理がGit
•   国内のメジャーなPerl HackerがGithubを使っている
•   Subversionからの移行が割と容易
•   動作速度が速い
•   ネットワークに繋がっていなくてもcommitできる
•   どんどん気兼ねなくcommitできる
•   「Subversionは中学生まで」と言われたから
実際に使ってみる
インストール

各OSのパッケージ管理システムでさっくりとインストール

 • Mac OSX
      # port install git-core

 • Linux RedHat系
      # yum install git

 • Linux Debian系
     # aptitude install git-core
最初に覚えるべきコマンド




    git help
実行してみる
yoshi@mb yoshi% git help 
usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]
           [-p|--paginate|--no-pager] [--no-replace-objects]
           [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]
           [--help] COMMAND [ARGS]

The most commonly used git commands are:
   add        Add file contents to the index
   bisect     Find by binary search the change that introduced a bug
   branch     List, create, or delete branches
   checkout   Checkout a branch or paths to the working tree
   clone      Clone a repository into a new directory

---snip---

   rm         Remove files from the working tree and from the index
   show       Show various types of objects
   status     Show the working tree status
   tag        Create, list, delete or verify a tag object signed with GPG

See 'git help COMMAND' for more information on a specific command.
初期設定

自分のユーザ名とメールアドレスを設定する

$ git config --global user.name 'Yoshihiro Sasaki'
$ git config --global user.email 'aloelight at gmail.com'

$ cat ~/.gitconfig
[user]
        name = Yoshihiro Sasaki
        email = aloelight at gmail.com
どこで使われるの?

コミット時に自動で使われ,コミットログで見れます

yoshi@mb Plack-Middleware-Parallel-Scoreboard% git log
commit 993c5c4cdfe6395f4bb21a633de90d5159135cd8
Author: Yoshihiro Sasaki <aloelight@gmail.com>
Date:   Wed Jul 14 20:21:27 2010 +0900

    copy from http://gist.github.com/464066
開発の流れ

1. リポジトリの作成
2. ファイルの新規追加,変更
3. コミット
4. 2,3を繰り返す
リポジトリを作成する

yoshi@mb tmp% module-setup Hokkaido   
--snip--
yoshi@mb tmp% cd Hokkaido/
yoshi@mb Hokkaido% git init
Initialized empty Git repository in /Users/yoshi/tmp/Hokkaido/.git/
.gitの中身
yoshi@mb Hokkaido% tree .git
.git
|-- HEAD
|-- config
|-- description
|-- hooks
|   |-- applypatch-msg.sample
|   |-- commit-msg.sample
|   |-- post-commit.sample
|   |-- post-receive.sample
|   |-- post-update.sample
|   |-- pre-applypatch.sample
|   |-- pre-commit.sample
|   |-- pre-rebase.sample
|   |-- prepare-commit-msg.sample
|   `-- update.sample
|-- info
|   `-- exclude
|-- objects
|   |-- info
|   `-- pack
`-- refs
    |-- heads
    `-- tags
今の状態の確認

yoshi@mb Hokkaido% git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       .gitignore
#       .shipit
#       Changes
#       MANIFEST.SKIP
#       Makefile.PL
#       README
#       lib/
#       t/
#       xt/
nothing added to commit but untracked files present (use "git add" to track)
ファイルを追加する

yoshi@mb Hokkaido% git add .          
yoshi@mb Hokkaido% git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   .gitignore
#       new file:   .shipit
#       new file:   Changes
#       new file:   MANIFEST.SKIP
#       new file:   Makefile.PL
#       new file:   README
#       new file:   lib/Hokkaido.pm
#       new file:   t/00_compile.t
#       new file:   xt/01_podspell.t
#       new file:   xt/02_perlcritic.t
#       new file:   xt/03_pod.t
#       new file:   xt/perlcriticrc
#
コミットする

yoshi@mb Hokkaido% git commit -m '初期状態をコミットします'
[master (root-commit) 3bec145] 初期状態をコミットします
 12 files changed, 137 insertions(+), 0 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 .shipit
 create mode 100644 Changes
 create mode 100644 MANIFEST.SKIP
 create mode 100644 Makefile.PL
 create mode 100644 README
 create mode 100644 lib/Hokkaido.pm
 create mode 100644 t/00_compile.t
 create mode 100644 xt/01_podspell.t
 create mode 100644 xt/02_perlcritic.t
 create mode 100644 xt/03_pod.t
 create mode 100644 xt/perlcriticrc
ファイルを変更してみる 1

yoshi@mb Hokkaido% vim t/00_compile.t 
yoshi@mb Hokkaido% git diff t/00_compile.t 
diff --git a/t/00_compile.t b/t/00_compile.t
index aa5eee9..a054976 100644
--- a/t/00_compile.t
+++ b/t/00_compile.t
@@ -1,4 +1,7 @@
 use strict;
-use Test::More tests => 1;
+use Test::More tests => 2;
 
 BEGIN { use_ok 'Hokkaido' }
+
+my @method = qw/cities members/;
+can_ok 'Hokkaido', @method;
ファイルを変更してみる 2
yoshi@mb Hokkaido% vim lib/Hokkaido.pm 
yoshi@mb Hokkaido% git diff lib/            
diff --git a/lib/Hokkaido.pm b/lib/Hokkaido.pm
index 1e5e118..f2f7921 100644
--- a/lib/Hokkaido.pm
+++ b/lib/Hokkaido.pm
@@ -1,7 +1,15 @@
 package Hokkaido;
 use strict;
 use warnings;
-our $VERSION = '0.01';
+our $VERSION = '0.02';
+
+sub cities {
+       qw/sapporo obihiro kushiro/
+}
+
+sub members {
+       qw/foo bar baz/
+}
 
 1;
 __END__
再度,現在の状態を確認

yoshi@mb Hokkaido% git status
# 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:   lib/Hokkaido.pm
#       modified:   t/00_compile.t
#
no changes added to commit (use "git add" and/or "git commit -a")
変更をコミット

yoshi@mb Hokkaido% git add -u
yoshi@mb Hokkaido% git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   lib/Hokkaido.pm
#       modified:   t/00_compile.t
#


yoshi@mb Hokkaido% git commit -m 'cities, members関数とそのテストの追加'
[master 66dd358] cities, members関数とそのテストの追加
 2 files changed, 13 insertions(+), 2 deletions(-)
毎回 git add するのは何故か

     ちゃんと変更したファイルを吟味してコミットしよう

git add と git commit はこんなイメージになります
+--------------+  git add ( staging )   +-------+  git commit   +------------+
| working copy | ---------------------> | index | ------------> | repository |
+--------------+                        +-------+               +------------+
その他の機能

•   タグ付け
•   チェックアウト
•   ブランチの作成
•   マージの手順と解説
•   同一ファイルの変更の一部をコミット
•   バグが混入したコミットを探す
タグを作成する

yoshi@mb Hokkaido% git log
commit 66dd3588d1047bcf57b363a6ac7cd39a23ea73ef
Author: Yoshihiro Sasaki <aloelight@gmail.com>
Date:   Thu Sep 30 01:28:18 2010 +0900

    cities, members関数とそのテストの追加

commit 3bec145de901a0420768d3fdb6e480f044e32ef8
Author: Yoshihiro Sasaki <aloelight@gmail.com>
Date:   Thu Sep 30 01:09:39 2010 +0900

      初期状態をコミットします
yoshi@mb Hokkaido% git tag 0.01 3bec145de
yoshi@mb Hokkaido% git tag 0.02
yoshi@mb Hokkaido% git tag -l
0.01
0.02
チェックアウト

yoshi@mb Hokkaido% git log --oneline
66dd358 cities, members関数とそのテストの追加
3bec145 初期状態をコミットします

yoshi@mb Hokkaido% git checkout 3bec145       
Note: checking out '3bec145'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 3bec145... 初期状態をコミットします
ブランチの作成

yoshi@mb Hokkaido% git checkout 3bec145       
Note: checking out '3bec145'.

yoshi@mb Hokkaido% git checkout -b b0.01
Switched to a new branch 'b0.01'

yoshi@mb Hokkaido% git status
# On branch b0.01
nothing to commit (working directory clean)

yoshi@mb Hokkaido% git branch
* b0.01
  master
ブランチの作成とcheckoutを同時に行
         う
yoshi@mb Hokkaido% git checkout -b b0.02 0.02    
Switched to a new branch 'b0.02'

yoshi@mb Hokkaido% git branch
  b0.01
* b0.02
  master
マージの手順 1

1. マージ元になる帯広ブランチを作成する
yoshi@mb Hokkaido% git checkout -b obihiro master

2. lib/Hokkaido.pmに関数を追加
yoshi@mb Hokkaido% git diff
diff --git a/lib/Hokkaido.pm b/lib/Hokkaido.pm
index f2f7921..808390e 100644
--- a/lib/Hokkaido.pm
+++ b/lib/Hokkaido.pm
@@ -11,6 +11,9 @@ sub members {
        qw/foo bar baz/
 }
 
+sub obihiro {
+       qw/三方六 マルセイバターサンド 豚丼/
+}
マージの手順 2

3. obihiroブランチにcommit
yoshi@mb Hokkaido% git commit -a -m 'obihiro関数の追加'
[obihiro b33faa0] obihiro関数の追加
 1 files changed, 3 insertions(+), 0 deletions(-)

4. masterブランチに切り替えて,merge
yoshi@mb Hokkaido% git checkout master 
Switched to branch 'master'
yoshi@mb Hokkaido% git merge obihiro
Updating 66dd358..b33faa0
Fast-forward
 lib/Hokkaido.pm |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
マージ結果の解説 1

            Updating 66dd358..b33faa0って何?

yoshi@mb Hokkaido% git log --oneline
b33faa0 obihiro関数の追加
66dd358 cities, members関数とそのテストの追加
3bec145 初期状態をコミットします
マージ結果の解説 2

           Fast-forwardって何?
merge前のリポジトリの状態
+------+         +--------+     +---------+
| 0.01 |   -->   |  0.02  | --> | obihiro |
+------+         +--------+     +---------+
                   ^
                   H
                   H
                 +--------+
                 | master |
                 +--------+


Fast-forwardでのmerge後の状態
+------+         +------+     +---------+
| 0.01 |   -->   | 0.02 | --> | obihiro |
+------+         +------+     +---------+
                                ^
                                H
                                H
                              +---------+
                              | master  |
                              +---------+
マージ結果の解説 3

     じゃあFast-forwardじゃないマージはどんなの?
merge前
+--------+         +------+     +---------+
|  0.01  |   -->   | 0.02 | --> | obihiro |
+--------+         +------+     +---------+
                     |
                     |
                     v
+--------+         +------+
| master |   ==>   | 0.03 |
+--------+         +------+


merge後
+------+         +------+     +---------+     +--------+     +--------+
| 0.01 |   -->   | 0.02 | --> | obihiro | --> | merged | <== | master |
+------+         +------+     +---------+     +--------+     +--------+
                   |                            ^
                   |                            |
                   v                            |
                 +------+                       |
                 | 0.03 | ----------------------+
                 +------+
同一ファイルの一部をコミット 1
                       とりあえず変更してみる

yoshi@mb Hokkaido% vim lib/Hokkaido.pm
yoshi@mb Hokkaido% git diff -U0           
diff --git a/lib/Hokkaido.pm b/lib/Hokkaido.pm
index 808390e..47acbf9 100644
--- a/lib/Hokkaido.pm
+++ b/lib/Hokkaido.pm
@@ -4 +4 @@ use warnings;
-our $VERSION = '0.02';
+our $VERSION = '0.03';
@@ -7 +7 @@ sub cities {
-       qw/sapporo obihiro kushiro/
+       qw/sapporo obihiro kushiro asahikawa hakodata/
@@ -15 +15 @@ sub obihiro {
-       qw/三方六 マルセイバターサンド 豚丼/
+       qw/豚丼 三方六 豚丼 マルセイバターサンド 豚丼/
同一ファイルの一部をコミット 2

yoshi@mb Hokkaido% git add -p
diff --git a/lib/Hokkaido.pm b/lib/Hokkaido.pm
index 808390e..47acbf9 100644
--- a/lib/Hokkaido.pm
+++ b/lib/Hokkaido.pm
@@ -1,10 +1,10 @@
 package Hokkaido;
 use strict;
 use warnings;
-our $VERSION = '0.02';
+our $VERSION = '0.03';

 sub cities {
-       qw/sapporo obihiro kushiro/
+       qw/sapporo obihiro kushiro asahikawa hakodata/
 }

 sub members {
Stage this hunk [y,n,q,a,d,/,j,J,g,s,e,?]? y
同一ファイルの一部をコミット 3

次のhunkはaddしたくないのでnを選択
@@ -12,7 +12,7 @@ sub members {
 }
 
 sub obihiro {
-       qw/三方六 マルセイバターサンド 豚丼/ 
+       qw/豚丼 三方六 豚丼 マルセイバターサンド 豚丼/
 }
 1;
 __END__
Stage this hunk [y,n,q,a,d,/,K,g,e,?]? n
バグが混入したコミットを探す 1

• git bisect で簡単に探せます
• 各コミットに対して good or bad を設定する
• good, bad の間を二分探索で原因となるコミットを探す
バグが混入したコミットを探す 2

                   bisectのサブコマンド
•   git bisect start
•   git bisect good [コミット]
•   git bisect bad [コミット]
•   git bisect reset
•   git bisect run command
バグが混入したコミットを探す  3

    わかりにくいのでDEMO
まとめ

• git help と  git status を覚えればなんとかなる
• 動作が速いのでbranchの作成,mergeを気軽に行える
• git bisect と git add -p が便利
最後に宣伝
質問タイム
ご清聴ありがとうございました

More Related Content

Viewers also liked

テーマ「最適化」
テーマ「最適化」テーマ「最適化」
テーマ「最適化」technocat
 
"Ops Tools with Perl" 2012/05/12 Hokkaido.pm
"Ops Tools with Perl" 2012/05/12 Hokkaido.pm"Ops Tools with Perl" 2012/05/12 Hokkaido.pm
"Ops Tools with Perl" 2012/05/12 Hokkaido.pmRyosuke IWANAGA
 
Plack::Request with Encoding
Plack::Request with EncodingPlack::Request with Encoding
Plack::Request with Encodingmoznion
 
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)goccy
 
YAPC::Asia 2013 - CPAN Testers Reports の情報を上手に使う
YAPC::Asia 2013 - CPAN Testers Reports の情報を上手に使うYAPC::Asia 2013 - CPAN Testers Reports の情報を上手に使う
YAPC::Asia 2013 - CPAN Testers Reports の情報を上手に使うmoznion
 
Takao.mt 2013
Takao.mt 2013Takao.mt 2013
Takao.mt 2013moznion
 
テーマ「なんでもないようなこと」
テーマ「なんでもないようなこと」テーマ「なんでもないようなこと」
テーマ「なんでもないようなこと」technocat
 
CPAN/便利モジュール
CPAN/便利モジュールCPAN/便利モジュール
CPAN/便利モジュールYoshihiro Sasaki
 
Perl 非同期プログラミング
Perl 非同期プログラミングPerl 非同期プログラミング
Perl 非同期プログラミングlestrrat
 
変数、リファレンス
変数、リファレンス変数、リファレンス
変数、リファレンスcharsbar
 
仮設ネットカフェ
仮設ネットカフェ仮設ネットカフェ
仮設ネットカフェAkiko Iwakiri
 
エロサイト管理者の憂鬱3 - Hokkaiodo.pm#4 -
エロサイト管理者の憂鬱3 - Hokkaiodo.pm#4 -エロサイト管理者の憂鬱3 - Hokkaiodo.pm#4 -
エロサイト管理者の憂鬱3 - Hokkaiodo.pm#4 -Yusuke Wada
 
Hokkaido pm 8 LT
Hokkaido pm 8 LTHokkaido pm 8 LT
Hokkaido pm 8 LTmoznion
 
Yet Another Perl Cooking
Yet Another Perl CookingYet Another Perl Cooking
Yet Another Perl Cookingmoznion
 
Separate Model from Catalyst
Separate Model from CatalystSeparate Model from Catalyst
Separate Model from Catalysttechmemo
 
Net stalking with-lastfm
Net stalking with-lastfmNet stalking with-lastfm
Net stalking with-lastfmmoznion
 
今年作ったもの2013 #hokkaidopm
今年作ったもの2013 #hokkaidopm今年作ったもの2013 #hokkaidopm
今年作ったもの2013 #hokkaidopm鉄次 尾形
 

Viewers also liked (20)

Using Dancer
Using DancerUsing Dancer
Using Dancer
 
Use Carton
Use CartonUse Carton
Use Carton
 
テーマ「最適化」
テーマ「最適化」テーマ「最適化」
テーマ「最適化」
 
"Ops Tools with Perl" 2012/05/12 Hokkaido.pm
"Ops Tools with Perl" 2012/05/12 Hokkaido.pm"Ops Tools with Perl" 2012/05/12 Hokkaido.pm
"Ops Tools with Perl" 2012/05/12 Hokkaido.pm
 
Plack::Request with Encoding
Plack::Request with EncodingPlack::Request with Encoding
Plack::Request with Encoding
 
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
 
YAPC::Asia 2013 - CPAN Testers Reports の情報を上手に使う
YAPC::Asia 2013 - CPAN Testers Reports の情報を上手に使うYAPC::Asia 2013 - CPAN Testers Reports の情報を上手に使う
YAPC::Asia 2013 - CPAN Testers Reports の情報を上手に使う
 
Takao.mt 2013
Takao.mt 2013Takao.mt 2013
Takao.mt 2013
 
テーマ「なんでもないようなこと」
テーマ「なんでもないようなこと」テーマ「なんでもないようなこと」
テーマ「なんでもないようなこと」
 
CPAN/便利モジュール
CPAN/便利モジュールCPAN/便利モジュール
CPAN/便利モジュール
 
Perl 非同期プログラミング
Perl 非同期プログラミングPerl 非同期プログラミング
Perl 非同期プログラミング
 
変数、リファレンス
変数、リファレンス変数、リファレンス
変数、リファレンス
 
cpanfile
cpanfilecpanfile
cpanfile
 
仮設ネットカフェ
仮設ネットカフェ仮設ネットカフェ
仮設ネットカフェ
 
エロサイト管理者の憂鬱3 - Hokkaiodo.pm#4 -
エロサイト管理者の憂鬱3 - Hokkaiodo.pm#4 -エロサイト管理者の憂鬱3 - Hokkaiodo.pm#4 -
エロサイト管理者の憂鬱3 - Hokkaiodo.pm#4 -
 
Hokkaido pm 8 LT
Hokkaido pm 8 LTHokkaido pm 8 LT
Hokkaido pm 8 LT
 
Yet Another Perl Cooking
Yet Another Perl CookingYet Another Perl Cooking
Yet Another Perl Cooking
 
Separate Model from Catalyst
Separate Model from CatalystSeparate Model from Catalyst
Separate Model from Catalyst
 
Net stalking with-lastfm
Net stalking with-lastfmNet stalking with-lastfm
Net stalking with-lastfm
 
今年作ったもの2013 #hokkaidopm
今年作ったもの2013 #hokkaidopm今年作ったもの2013 #hokkaidopm
今年作ったもの2013 #hokkaidopm
 

理解したつもりになるGit入門

  • 1. 理解したつもりになるGit入門 2010/10/02 Hokkaido.pm #2 株式会社アイダック CPS事業部 佐々木義広
  • 2. 自己紹介 • 佐々木義広 • Twitter @aloelight • Blog http://blog.vitamin11.org/ • プログラマ 兼 サーバ管理者
  • 3.
  • 4.
  • 8. バージョン管理を行う理由 • 誰がソースを変更したのかすぐわかる       commitのauthorをみれば一発 • 不具合があれば簡単に前のバージョンに戻せる       index.html.2010102 ← こういう不幸なファイルが生まれな い • 複数人が平行して開発する状況を想定している         可能なら自動でmerge,ダメなら衝突を報告してくれる
  • 9. メジャーなバージョン管理システム • Bazaar • CVS • Subversion • Mercurial • Monotone • その他色々
  • 10. Gitを使う理由 • Perl本体のバージョン管理がGit • 国内のメジャーなPerl HackerがGithubを使っている • Subversionからの移行が割と容易 • 動作速度が速い • ネットワークに繋がっていなくてもcommitできる • どんどん気兼ねなくcommitできる • 「Subversionは中学生まで」と言われたから
  • 12. インストール 各OSのパッケージ管理システムでさっくりとインストール • Mac OSX       # port install git-core • Linux RedHat系       # yum install git • Linux Debian系      # aptitude install git-core
  • 14. 実行してみる yoshi@mb yoshi% git help  usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]            [-p|--paginate|--no-pager] [--no-replace-objects]            [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]            [--help] COMMAND [ARGS] The most commonly used git commands are:    add        Add file contents to the index    bisect     Find by binary search the change that introduced a bug    branch     List, create, or delete branches    checkout   Checkout a branch or paths to the working tree    clone      Clone a repository into a new directory ---snip---    rm         Remove files from the working tree and from the index    show       Show various types of objects    status     Show the working tree status    tag        Create, list, delete or verify a tag object signed with GPG See 'git help COMMAND' for more information on a specific command.
  • 15. 初期設定 自分のユーザ名とメールアドレスを設定する $ git config --global user.name 'Yoshihiro Sasaki' $ git config --global user.email 'aloelight at gmail.com' $ cat ~/.gitconfig [user]         name = Yoshihiro Sasaki         email = aloelight at gmail.com
  • 16. どこで使われるの? コミット時に自動で使われ,コミットログで見れます yoshi@mb Plack-Middleware-Parallel-Scoreboard% git log commit 993c5c4cdfe6395f4bb21a633de90d5159135cd8 Author: Yoshihiro Sasaki <aloelight@gmail.com> Date:   Wed Jul 14 20:21:27 2010 +0900     copy from http://gist.github.com/464066
  • 18. リポジトリを作成する yoshi@mb tmp% module-setup Hokkaido    --snip-- yoshi@mb tmp% cd Hokkaido/ yoshi@mb Hokkaido% git init Initialized empty Git repository in /Users/yoshi/tmp/Hokkaido/.git/
  • 19. .gitの中身 yoshi@mb Hokkaido% tree .git .git |-- HEAD |-- config |-- description |-- hooks |   |-- applypatch-msg.sample |   |-- commit-msg.sample |   |-- post-commit.sample |   |-- post-receive.sample |   |-- post-update.sample |   |-- pre-applypatch.sample |   |-- pre-commit.sample |   |-- pre-rebase.sample |   |-- prepare-commit-msg.sample |   `-- update.sample |-- info |   `-- exclude |-- objects |   |-- info |   `-- pack `-- refs     |-- heads     `-- tags
  • 20. 今の状態の確認 yoshi@mb Hokkaido% git status # On branch master # # Initial commit # # Untracked files: #   (use "git add <file>..." to include in what will be committed) # #       .gitignore #       .shipit #       Changes #       MANIFEST.SKIP #       Makefile.PL #       README #       lib/ #       t/ #       xt/ nothing added to commit but untracked files present (use "git add" to track)
  • 21. ファイルを追加する yoshi@mb Hokkaido% git add .           yoshi@mb Hokkaido% git status # On branch master # # Initial commit # # Changes to be committed: #   (use "git rm --cached <file>..." to unstage) # #       new file:   .gitignore #       new file:   .shipit #       new file:   Changes #       new file:   MANIFEST.SKIP #       new file:   Makefile.PL #       new file:   README #       new file:   lib/Hokkaido.pm #       new file:   t/00_compile.t #       new file:   xt/01_podspell.t #       new file:   xt/02_perlcritic.t #       new file:   xt/03_pod.t #       new file:   xt/perlcriticrc #
  • 22. コミットする yoshi@mb Hokkaido% git commit -m '初期状態をコミットします' [master (root-commit) 3bec145] 初期状態をコミットします  12 files changed, 137 insertions(+), 0 deletions(-)  create mode 100644 .gitignore  create mode 100644 .shipit  create mode 100644 Changes  create mode 100644 MANIFEST.SKIP  create mode 100644 Makefile.PL  create mode 100644 README  create mode 100644 lib/Hokkaido.pm  create mode 100644 t/00_compile.t  create mode 100644 xt/01_podspell.t  create mode 100644 xt/02_perlcritic.t  create mode 100644 xt/03_pod.t  create mode 100644 xt/perlcriticrc
  • 23. ファイルを変更してみる 1 yoshi@mb Hokkaido% vim t/00_compile.t  yoshi@mb Hokkaido% git diff t/00_compile.t  diff --git a/t/00_compile.t b/t/00_compile.t index aa5eee9..a054976 100644 --- a/t/00_compile.t +++ b/t/00_compile.t @@ -1,4 +1,7 @@  use strict; -use Test::More tests => 1; +use Test::More tests => 2;    BEGIN { use_ok 'Hokkaido' } + +my @method = qw/cities members/; +can_ok 'Hokkaido', @method;
  • 24. ファイルを変更してみる 2 yoshi@mb Hokkaido% vim lib/Hokkaido.pm  yoshi@mb Hokkaido% git diff lib/             diff --git a/lib/Hokkaido.pm b/lib/Hokkaido.pm index 1e5e118..f2f7921 100644 --- a/lib/Hokkaido.pm +++ b/lib/Hokkaido.pm @@ -1,7 +1,15 @@  package Hokkaido;  use strict;  use warnings; -our $VERSION = '0.01'; +our $VERSION = '0.02'; + +sub cities { +       qw/sapporo obihiro kushiro/ +} + +sub members { +       qw/foo bar baz/ +}    1;  __END__
  • 25. 再度,現在の状態を確認 yoshi@mb Hokkaido% git status # 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:   lib/Hokkaido.pm #       modified:   t/00_compile.t # no changes added to commit (use "git add" and/or "git commit -a")
  • 26. 変更をコミット yoshi@mb Hokkaido% git add -u yoshi@mb Hokkaido% git status # On branch master # Changes to be committed: #   (use "git reset HEAD <file>..." to unstage) # #       modified:   lib/Hokkaido.pm #       modified:   t/00_compile.t # yoshi@mb Hokkaido% git commit -m 'cities, members関数とそのテストの追加' [master 66dd358] cities, members関数とそのテストの追加  2 files changed, 13 insertions(+), 2 deletions(-)
  • 27. 毎回 git add するのは何故か ちゃんと変更したファイルを吟味してコミットしよう git add と git commit はこんなイメージになります +--------------+  git add ( staging )   +-------+  git commit   +------------+ | working copy | ---------------------> | index | ------------> | repository | +--------------+                        +-------+               +------------+
  • 28. その他の機能 • タグ付け • チェックアウト • ブランチの作成 • マージの手順と解説 • 同一ファイルの変更の一部をコミット • バグが混入したコミットを探す
  • 29. タグを作成する yoshi@mb Hokkaido% git log commit 66dd3588d1047bcf57b363a6ac7cd39a23ea73ef Author: Yoshihiro Sasaki <aloelight@gmail.com> Date:   Thu Sep 30 01:28:18 2010 +0900     cities, members関数とそのテストの追加 commit 3bec145de901a0420768d3fdb6e480f044e32ef8 Author: Yoshihiro Sasaki <aloelight@gmail.com> Date:   Thu Sep 30 01:09:39 2010 +0900       初期状態をコミットします yoshi@mb Hokkaido% git tag 0.01 3bec145de yoshi@mb Hokkaido% git tag 0.02 yoshi@mb Hokkaido% git tag -l 0.01 0.02
  • 30. チェックアウト yoshi@mb Hokkaido% git log --oneline 66dd358 cities, members関数とそのテストの追加 3bec145 初期状態をコミットします yoshi@mb Hokkaido% git checkout 3bec145        Note: checking out '3bec145'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:   git checkout -b new_branch_name HEAD is now at 3bec145... 初期状態をコミットします
  • 31. ブランチの作成 yoshi@mb Hokkaido% git checkout 3bec145        Note: checking out '3bec145'. yoshi@mb Hokkaido% git checkout -b b0.01 Switched to a new branch 'b0.01' yoshi@mb Hokkaido% git status # On branch b0.01 nothing to commit (working directory clean) yoshi@mb Hokkaido% git branch * b0.01   master
  • 32. ブランチの作成とcheckoutを同時に行 う yoshi@mb Hokkaido% git checkout -b b0.02 0.02     Switched to a new branch 'b0.02' yoshi@mb Hokkaido% git branch   b0.01 * b0.02   master
  • 33. マージの手順 1 1. マージ元になる帯広ブランチを作成する yoshi@mb Hokkaido% git checkout -b obihiro master 2. lib/Hokkaido.pmに関数を追加 yoshi@mb Hokkaido% git diff diff --git a/lib/Hokkaido.pm b/lib/Hokkaido.pm index f2f7921..808390e 100644 --- a/lib/Hokkaido.pm +++ b/lib/Hokkaido.pm @@ -11,6 +11,9 @@ sub members {         qw/foo bar baz/  }   +sub obihiro { +       qw/三方六 マルセイバターサンド 豚丼/ +}
  • 34. マージの手順 2 3. obihiroブランチにcommit yoshi@mb Hokkaido% git commit -a -m 'obihiro関数の追加' [obihiro b33faa0] obihiro関数の追加  1 files changed, 3 insertions(+), 0 deletions(-) 4. masterブランチに切り替えて,merge yoshi@mb Hokkaido% git checkout master  Switched to branch 'master' yoshi@mb Hokkaido% git merge obihiro Updating 66dd358..b33faa0 Fast-forward  lib/Hokkaido.pm |    3 +++  1 files changed, 3 insertions(+), 0 deletions(-)
  • 35. マージ結果の解説 1 Updating 66dd358..b33faa0って何? yoshi@mb Hokkaido% git log --oneline b33faa0 obihiro関数の追加 66dd358 cities, members関数とそのテストの追加 3bec145 初期状態をコミットします
  • 36. マージ結果の解説 2 Fast-forwardって何? merge前のリポジトリの状態 +------+     +--------+     +---------+ | 0.01 | --> |  0.02  | --> | obihiro | +------+     +--------+     +---------+                ^                H                H              +--------+              | master |              +--------+ Fast-forwardでのmerge後の状態 +------+     +------+     +---------+ | 0.01 | --> | 0.02 | --> | obihiro | +------+     +------+     +---------+                             ^                             H                             H                           +---------+                           | master  |                           +---------+
  • 37. マージ結果の解説 3 じゃあFast-forwardじゃないマージはどんなの? merge前 +--------+     +------+     +---------+ |  0.01  | --> | 0.02 | --> | obihiro | +--------+     +------+     +---------+                  |                  |                  v +--------+     +------+ | master | ==> | 0.03 | +--------+     +------+ merge後 +------+     +------+     +---------+     +--------+     +--------+ | 0.01 | --> | 0.02 | --> | obihiro | --> | merged | <== | master | +------+     +------+     +---------+     +--------+     +--------+                |                            ^                |                            |                v                            |              +------+                       |              | 0.03 | ----------------------+              +------+
  • 38. 同一ファイルの一部をコミット 1 とりあえず変更してみる yoshi@mb Hokkaido% vim lib/Hokkaido.pm yoshi@mb Hokkaido% git diff -U0            diff --git a/lib/Hokkaido.pm b/lib/Hokkaido.pm index 808390e..47acbf9 100644 --- a/lib/Hokkaido.pm +++ b/lib/Hokkaido.pm @@ -4 +4 @@ use warnings; -our $VERSION = '0.02'; +our $VERSION = '0.03'; @@ -7 +7 @@ sub cities { -       qw/sapporo obihiro kushiro/ +       qw/sapporo obihiro kushiro asahikawa hakodata/ @@ -15 +15 @@ sub obihiro { -       qw/三方六 マルセイバターサンド 豚丼/ +       qw/豚丼 三方六 豚丼 マルセイバターサンド 豚丼/
  • 39. 同一ファイルの一部をコミット 2 yoshi@mb Hokkaido% git add -p diff --git a/lib/Hokkaido.pm b/lib/Hokkaido.pm index 808390e..47acbf9 100644 --- a/lib/Hokkaido.pm +++ b/lib/Hokkaido.pm @@ -1,10 +1,10 @@  package Hokkaido;  use strict;  use warnings; -our $VERSION = '0.02'; +our $VERSION = '0.03';  sub cities { -       qw/sapporo obihiro kushiro/ +       qw/sapporo obihiro kushiro asahikawa hakodata/  }  sub members { Stage this hunk [y,n,q,a,d,/,j,J,g,s,e,?]? y
  • 40. 同一ファイルの一部をコミット 3 次のhunkはaddしたくないのでnを選択 @@ -12,7 +12,7 @@ sub members {  }    sub obihiro { -       qw/三方六 マルセイバターサンド 豚丼/  +       qw/豚丼 三方六 豚丼 マルセイバターサンド 豚丼/  }  1;  __END__ Stage this hunk [y,n,q,a,d,/,K,g,e,?]? n
  • 41. バグが混入したコミットを探す 1 • git bisect で簡単に探せます • 各コミットに対して good or bad を設定する • good, bad の間を二分探索で原因となるコミットを探す
  • 42. バグが混入したコミットを探す 2 bisectのサブコマンド • git bisect start • git bisect good [コミット] • git bisect bad [コミット] • git bisect reset • git bisect run command
  • 43. バグが混入したコミットを探す  3 わかりにくいのでDEMO
  • 44. まとめ • git help と  git status を覚えればなんとかなる • 動作が速いのでbranchの作成,mergeを気軽に行える • git bisect と git add -p が便利