git/第一回勉強会 http://www.ark-web.jp/sandbox/wiki/4779.html
僕自身勉強中なんですが。
第一回git勉強会 †
- 2009/09/08 参加者 志田、竹村さん、大辻さん
- ネタ元: gittutorial(7) http://www8.atwiki.jp/git_jp/pub/git-manual-jp/Documentation/gittutorial.html
動画 †
まず概念から †
- 分散レポジトリ管理とは?
http://sourceforge.jp/magazine/09/02/02/0655246
- 各自自分用レポジトリをもつ
- レポジトリ ≒ 作業ディレクトリ
- 他のレポジトリにcommitできる(push)
- 他のレポジトリからパッチを取得してcommitできる(pull)
- 複数人でレポジトリを同期しながら開発をしていくには?
http://sourceforge.jp/magazine/09/02/02/0655246
- マスターレポジトリ - 1つ
- 中央のみんな用レポジトリをメンバーなら誰でもアクセスできる場所におく
- 個人用レポジトリ - 人数分
- 各自個人用レポジトリにcommit後、それをマスターレポジトリにpush
- 他人がpushしたcommitをマスターレポジトリpullして個人用レポジトリに
特徴、何がうれしいのか? †
- ローカルにすべての情報があるので早い
- diffとかlogとかサクサク。
- ローカルにすべてあるので、ビビらずにcommitできる。
- ぐちゃぐちゃし放題
- ブランチが強力
- 簡単にブランチ作成
- 簡単にブランチ間をマージ
- どのタイミングからブランチしたのか、ブランチポイントを覚えている
- マージされたかどうかを覚えている
- 簡単にブランチ削除
- 実はローカルレポジトリからみてリモートレポジトリもブランチ
- pushとはリモート上のブランチにmergeしてるってこと
まずローカルレポジトリレポジトリを新規に作ってみよう †
- チュートリアルどおりに
$ tar xzf project.tar.gz $ cd project $ git init Initialized empty Git repository in /home/staff/shida/work/git/project/.git/
- .gitというディレクトリができた。これがローカルレポジトリ。
- この段階では、まだprojectの全ファイルはコミットされていない。
- git statusで現状を見てみる
$ git status # On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # file1 # file2 # file3 nothing added to commit but untracked files present (use "git add" to track)
- commit対象ファイルはないが、 file1, file2, file3がuntracked fileの状態だ。とのこと。
- git addで追跡対象に加える
$ git add .
- もう一回git status で見てみる
$ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: file1 # new file: file2 # new file: file3 #
- これで、commit対象になった。git commit する
$ git commit -m 'file1,2,3を追加' [master (root-commit) 0bb8c14] file1,2,3を追加 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 file1 create mode 100644 file2 create mode 100644 file3
- 再度、statusを見てみる
$ git status # On branch master nothing to commit (working directory clean)
- 作業ディレクトリはクリーンとのこと
- historyを見てみる
$ git log commit 0bb8c14ea62d2b0f0021dc7d1cc217b970e2e706 Author: Yuki SHIDA <shida@ark-web.jp> Date: Tue Sep 8 16:10:40 2009 +0900 file1,2,3を追加
- gitのリビジョンはsvnのような通し番号ではなく「0bb8c14ea62d2b0f0021dc7d1cc217b970e2e706」といった長いハッシュ。
- 共同開発者間で、差分をユニークに特定できるような名前付けが必要なため。
ステージングエリアとかインデックスというやつ †
- gitでは3つの概念がある
- 作業ディレクトリ
- ステージングエリア
- レポジトリ
- 作業ディレクトリの内容は一旦ステージングエリアに上げないといけない
- それが git add
修正してみる †
- なんか修正する
$ echo "なんか修正" >> file1 $ echo "なんか修正" >> file2 $ echo "なんか修正" >> file3
- statusをみる
$ 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: file1 # modified: file2 # modified: file3 no changes added to commit (use "git add" and/or "git commit -a")
- まだfile1, file2, file3はコミット対象ではない
$ git add file{1,2,3} $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: file1 # modified: file2 # modified: file3 - これでステージングエリアに追加された
- git diff --cached でステージングエリアとレポジトリの比較ができる
$ git diff --cached diff --git a/file1 b/file1 index e69de29..ec1e872 100644 --- a/file1 +++ b/file1 @@ -0,0 +1 @@ +なんか修正なんか修正 diff --git a/file2 b/file2 index e69de29..7c15ca3 100644 --- a/file2 +++ b/file2 @@ -0,0 +1 @@ +なんか修正 diff --git a/file3 b/file3 index e69de29..7c15ca3 100644 --- a/file3 +++ b/file3 @@ -0,0 +1 @@
- 単なるgit diff だと作業ディレクトリとステージングエリアとの差分を表示
$ touch file4 $ git commit -m 'file4追加' file4 $ echo "なんか修正" >> file4 $ git diff diff --git a/file4 b/file4 index e69de29..8009f23 100644 --- a/file4 +++ b/file4 @@ -0,0 +1 @@ +aaaなんか修正
- commit -a とすると、新規追加ファイルでなければaddしなくてもcommitできる
$ git commit -a -m 'file4もaddしなくてもcommitされるよ' [master 582a14c] file4もaddしなくてもcommitされるよ 4 files changed, 4 insertions(+), 0 deletions(-)
ブランチ管理 †
- 「experimental」というブランチを作る
$ git branch experimental
- 今このレポジトリに存在するブランチ一覧
$ git branch experimental * master
- 「*」が付いてるのが今操作しているブランチ。だから、今操作しているのは「master」ブランチ。これはレポジトリに最初からあるブランチ。
- git checkoutでブランチを切り替え
$ git checkout experimental Switched to branch 'experimental' $ git branch * experimental master
- expermimentalブランチ上で修正をしてみる
$ echo "ブランチ上でなんか修正" >> file1 $ git commit -a -m "file1にブランチ experimental から修正" [experimental dd78c7e] file1にブランチ experimental から修正 1 files changed, 1 insertions(+), 0 deletions(-)
- masterブランチに戻って、マージしてみる
$ git checkout master Switched to branch 'master' $ git branch experimental * master $ git merge experimental Updating 582a14c..dd78c7e Fast forward file1 | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
- mergeはsvn mergeと違ってconflictしなければ速攻commitされる
$ git status # On branch master nothing to commit (working directory clean) $ git log commit dd78c7e439e3973c1eef2c60b1a4eb43ae31a53b Author: Yuki SHIDA <shida@ark-web.jp> Date: Tue Sep 8 16:30:19 2009 +0900 file1にブランチ experimental から修正
- マージ済みのブランチは削除する
$ git branch -d experimental Deleted branch experimental (was dd78c7e). $ git branch * master
![[PukiWiki] [PukiWiki]](image/sandbox.gif)



