Upload
yoshihiro-sasaki
View
1.102
Download
0
Embed Size (px)
Citation preview
理解したつもりになる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 logcommit 993c5c4cdfe6395f4bb21a633de90d5159135cd8Author: Yoshihiro Sasaki <[email protected]>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 initInitialized 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.tindex 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;
ファイルを変更してみる 2yoshi@mb Hokkaido% vim lib/Hokkaido.pm yoshi@mb Hokkaido% git diff lib/ diff --git a/lib/Hokkaido.pm b/lib/Hokkaido.pmindex 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 -uyoshi@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 logcommit 66dd3588d1047bcf57b363a6ac7cd39a23ea73efAuthor: Yoshihiro Sasaki <[email protected]>Date: Thu Sep 30 01:28:18 2010 +0900
cities, members関数とそのテストの追加
commit 3bec145de901a0420768d3fdb6e480f044e32ef8Author: Yoshihiro Sasaki <[email protected]>Date: Thu Sep 30 01:09:39 2010 +0900
初期状態をコミットしますyoshi@mb Hokkaido% git tag 0.01 3bec145deyoshi@mb Hokkaido% git tag 0.02yoshi@mb Hokkaido% git tag -l0.010.02
チェックアウト
yoshi@mb Hokkaido% git log --oneline66dd358 cities, members関数とそのテストの追加3bec145 初期状態をコミットします
yoshi@mb Hokkaido% git checkout 3bec145 Note: checking out '3bec145'.
You are in 'detached HEAD' state. You can look around, make experimentalchanges and commit them, and you can discard any commits you make in thisstate without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you maydo 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.01Switched to a new branch 'b0.01'
yoshi@mb Hokkaido% git status# On branch b0.01nothing 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 diffdiff --git a/lib/Hokkaido.pm b/lib/Hokkaido.pmindex 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ブランチにcommityoshi@mb Hokkaido% git commit -a -m 'obihiro関数の追加'[obihiro b33faa0] obihiro関数の追加 1 files changed, 3 insertions(+), 0 deletions(-)
4. masterブランチに切り替えて,mergeyoshi@mb Hokkaido% git checkout master Switched to branch 'master'yoshi@mb Hokkaido% git merge obihiroUpdating 66dd358..b33faa0Fast-forward lib/Hokkaido.pm | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)
マージ結果の解説 1
Updating 66dd358..b33faa0って何?
yoshi@mb Hokkaido% git log --onelineb33faa0 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.pmyoshi@mb Hokkaido% git diff -U0 diff --git a/lib/Hokkaido.pm b/lib/Hokkaido.pmindex 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 -pdiff --git a/lib/Hokkaido.pm b/lib/Hokkaido.pmindex 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 が便利
最後に宣伝
質問タイム
ご清聴ありがとうございました