Gitハンズオン
      zaki 2012/5/16
ハンズオンの想定参加者
• 開発で git を使ったことがある

• 普段の生活ではなんとかなっている
 が、中で何が起きているのかはよくわ
 からない

• コマンドラインを怖がらない

• git のことをもっとよく知りたい
レシピ
• Git の内部構造について

  http://keijinsonyaban.blogspot.jp/2011/05/git.html


• Git Immersion
  http://gitimmersion.com/
• A successful Git branching model とgit flow
  http://keijinsonyaban.blogspot.jp/2010/10/successful-git-branching-model.html

  https://github.com/nvie/gitflow

  http://www.oreilly.co.jp/community/blog/2011/11/branch-model-with-git-flow.html
環境構築
環境構築(MacPorts)
•   git
    sudo port install git-core +svn +bash_completion
    コマンドプロンプトで git と打って not found と言われないこと

•   .profile に以下の記述をしてプロンプトにブランチ名表示

    # for bash completion
    if [ -f /opt/local/etc/bash_completion ]; then
        . /opt/local/etc/bash_completion
    fi
    if [ -f $BASH_COMPLETION_DIR/git ]; then
           export PS1='[033[01;32m]u@h[033[01;33m] w$(__git_ps1) n[033[01;34m]$[033[00m] '

    else
           export PS1='[033[01;32m]u@h[033[01;33m] w n[033[01;34m]$[033[00m] '

    fi
環境構築(Homebrew)
•   git
    sudo brew install git bash-completion
    コマンドプロンプトで git と打って not found と言われないこと

•   .profile に以下の記述をしてプロンプトにブランチ名表示

    # for bash completion

    if [ -f `brew --prefix`/etc/bash_completion ]; then
        . `brew --prefix`/etc/bash_completion

    fi

    if [ -f `brew --prefix`/etc/bash_completion.d/git-completion.bash ]; then
           export PS1='[033[01;32m]u@h[033[01;33m] w$(__git_ps1) n[033[01;34m]$[033[00m] '

    else

           export PS1='[033[01;32m]u@h[033[01;33m] w n[033[01;34m]$[033[00m] '

    fi
Git の内部構造について


http://keijinsonyaban.blogspot.jp/2011/05/git.html
用語集
•   リポジトリ                     ブランチをチェックアウトしている場合
                              はそのブランチの名前
    コミットの集合(正確にはインデックスも
    リポジトリの一部)                 特定のコミット(タグなど)をチェックアウ
                              トしている場合は、そのコミットの事
•   インデックス
                          •   ブランチ(リファレンス)
    次のコミットに含まれる内容を覚えている
    場所                        コミットの別名。ただし、特定のコミット
                              を指すのではなく、コミット操作によって
•   ワーキングツリー
                              自動的に対象とするコミットが変化する
    リポジトリと関連付けられたディレクトリ
                          •   タグ
•   コミット
                              特定のコミットにつけた別名
    ある時点でのワーキングツリーのスナップ
                          •   master
    ショット
                              開発用ブランチのデファクトの名前
•   HEAD
git のデータ構造
•   blob                                    git ls-tree <tree id> [path] で、tree の

    ファイルの中身を保持するデータ。                        情報が表示できる(ex. git ls-tree

    blob はファイルの中身のハッシュ値                     HEAD

    によって識別されるため、リポジト                        git cat-file tree <hash> で中身そのも
    リやパスによらず一意のIDをもつ                        のを表示することも可能(化けるけど)

    git hash-object <ファイル名> でhash       •   commit
    を表示(ex. git hash-object greeting        コミットに関連する情報を束ねるた
                                            めのデータ構造
    git cat-file blob <hash> でblob の中身
    を表示                                     git rev-parse <ref> で、commit のid
                                            を表示できる (ex. git rev-parse
•   tree
                                            HEAD
    ディレクトリ構造を管理するデータ。
                                            git cat-file commit <hash> でcommit
                                            の中身を表示できる
おまけ1

• リポジトリにどんな object が格納され
 ているかを表示するコマンド

 for i in $(find .git/objects/ -type f); do
 echo -n "$i "; git cat-file -t $(basename $
 (dirname $i))$(basename $i); done
おまけ2
• commit を使わない commit

 echo 'Hello, World!' > greeting
 git add !$
 treeid=$(git write-tree)
 commitid=$(echo “Initial commit” | git
 commit-tree $treeid)
 git update-ref refs/heads/master $commitid
練習
• 1つめのcommit で /greeting ファイルを、2つ目
 の commit で /folder/greeting2 ファイルをリポ
 ジトリに登録、3つ目のcommit で /greetingの
 中身を更新したリポジトリを作ってください

• そのリポジトリに対し、各 commit の commit
 ファイルからそれぞれの commit の tree ファイ
 ルと blob ファイルを表示してください
解答例
$ git init                                                 $ git log --pretty=oneline
Initialized empty Git repository in /Users/zaki/fromgit/   c802fa339e37c2cf6eaeca9faa40b2e38405cf44 3rd commit
q1/.git/                                                   27a946b45cfc917a68b823c40848a7e8706f9a6a 2nd commit
                                                           09a263dd39525b5f8badd1a0eb2983e052b058e1 1st commit

$ echo 'Hello!' > greeting
                                                           $ git cat-file commit 09a263dd39525b5f8badd1a0eb2983e052b058e1
                                                           tree 57b6e13c3f7c0d4d9f327a878ffe5639c55148ee
$ git add !$                                               author YAMAZAKI Makoto <makoto1975@gmail.com> 1337146517 +0900
git add greeting                                           committer YAMAZAKI Makoto <makoto1975@gmail.com> 1337146517 +0900

$ git ci -m "1st commit"                                   1st commit
[master (root-commit) 09a263d] 1st commit
 1 file changed, 1 insertion(+)                            $ git ls-tree 57b6e13c3f7c0d4d9f327a878ffe5639c55148ee
                                                           100644 blob 10ddd6d257e01349d514541981aeecea6b2e741d	   greeting
 create mode 100644 greeting
                                                           $ git cat-file commit 27a946b45cfc917a68b823c40848a7e8706f9a6a
$ mkdir folder                                             tree 06b2d98a9898376793134ae06dda58341af90656
                                                           parent 09a263dd39525b5f8badd1a0eb2983e052b058e1
$ echo 'Hello2!' > folder/greeting2                        author YAMAZAKI Makoto <makoto1975@gmail.com> 1337146573 +0900
                                                           committer YAMAZAKI Makoto <makoto1975@gmail.com> 1337146573 +0900
$ git add !$
git add folder/greeting2                                   2nd commit

                                                           $ git ls-tree 06b2d98a9898376793134ae06dda58341af90656
$ git ci -m "2nd commit"
                                                           040000 tree 3a06576055421d5d27ff1fe60b87d1e9ea76902c	   folder
[master 27a946b] 2nd commit                                100644 blob 10ddd6d257e01349d514541981aeecea6b2e741d	   greeting
 1 file changed, 1 insertion(+)
 create mode 100644 folder/greeting2                       $ git cat-file commit c802fa339e37c2cf6eaeca9faa40b2e38405cf44
                                                           tree 8422453ed2a20ecef084d404f4feac29a267a6d5
$ echo 'Hello, World!' > greeting                          parent 27a946b45cfc917a68b823c40848a7e8706f9a6a
                                                           author YAMAZAKI Makoto <makoto1975@gmail.com> 1337146602 +0900
                                                           committer YAMAZAKI Makoto <makoto1975@gmail.com> 1337146602 +0900
$ git add !$
git add greeting
                                                           3rd commit

$ git ci -m "3rd commit"                                   $ git ls-tree 8422453ed2a20ecef084d404f4feac29a267a6d5
[master c802fa3] 3rd commit                                040000 tree 3a06576055421d5d27ff1fe60b87d1e9ea76902c	   folder
 1 file changed, 1 insertion(+), 1 deletion(-)             100644 blob 8ab686eafeb1f44702738c8b0f24f2567c36da6d	   greeting
Git Immersion
Git Immersion とは


A Guided Tour that Walks trough the
       Fundamentals of Git
Git Immersion



http://gitimmersion.com/
補足など
• LAB 1
  --global をつけると ~/.gitconfig に、つけ
  ないと直近の .git/config に設定が記録さ
  れます。

• LAB 11
  stash ってtypoしやすいので ss=stash とか
  もあったほうがいいと思ってる
git-flow
git-flowによるブランチ管理



http://www.oreilly.co.jp/community/blog/2011/11/
         branch-model-with-git-flow.html

20120516 第7回ウフィカ社内ハンズオン Git基礎

  • 1.
    Gitハンズオン zaki 2012/5/16
  • 2.
    ハンズオンの想定参加者 • 開発で gitを使ったことがある • 普段の生活ではなんとかなっている が、中で何が起きているのかはよくわ からない • コマンドラインを怖がらない • git のことをもっとよく知りたい
  • 3.
    レシピ • Git の内部構造について http://keijinsonyaban.blogspot.jp/2011/05/git.html • Git Immersion http://gitimmersion.com/ • A successful Git branching model とgit flow http://keijinsonyaban.blogspot.jp/2010/10/successful-git-branching-model.html https://github.com/nvie/gitflow http://www.oreilly.co.jp/community/blog/2011/11/branch-model-with-git-flow.html
  • 4.
  • 5.
    環境構築(MacPorts) • git sudo port install git-core +svn +bash_completion コマンドプロンプトで git と打って not found と言われないこと • .profile に以下の記述をしてプロンプトにブランチ名表示 # for bash completion if [ -f /opt/local/etc/bash_completion ]; then . /opt/local/etc/bash_completion fi if [ -f $BASH_COMPLETION_DIR/git ]; then export PS1='[033[01;32m]u@h[033[01;33m] w$(__git_ps1) n[033[01;34m]$[033[00m] ' else export PS1='[033[01;32m]u@h[033[01;33m] w n[033[01;34m]$[033[00m] ' fi
  • 6.
    環境構築(Homebrew) • git sudo brew install git bash-completion コマンドプロンプトで git と打って not found と言われないこと • .profile に以下の記述をしてプロンプトにブランチ名表示 # for bash completion if [ -f `brew --prefix`/etc/bash_completion ]; then . `brew --prefix`/etc/bash_completion fi if [ -f `brew --prefix`/etc/bash_completion.d/git-completion.bash ]; then export PS1='[033[01;32m]u@h[033[01;33m] w$(__git_ps1) n[033[01;34m]$[033[00m] ' else export PS1='[033[01;32m]u@h[033[01;33m] w n[033[01;34m]$[033[00m] ' fi
  • 7.
  • 8.
    用語集 • リポジトリ ブランチをチェックアウトしている場合 はそのブランチの名前 コミットの集合(正確にはインデックスも リポジトリの一部) 特定のコミット(タグなど)をチェックアウ トしている場合は、そのコミットの事 • インデックス • ブランチ(リファレンス) 次のコミットに含まれる内容を覚えている 場所 コミットの別名。ただし、特定のコミット を指すのではなく、コミット操作によって • ワーキングツリー 自動的に対象とするコミットが変化する リポジトリと関連付けられたディレクトリ • タグ • コミット 特定のコミットにつけた別名 ある時点でのワーキングツリーのスナップ • master ショット 開発用ブランチのデファクトの名前 • HEAD
  • 9.
    git のデータ構造 • blob git ls-tree <tree id> [path] で、tree の ファイルの中身を保持するデータ。 情報が表示できる(ex. git ls-tree blob はファイルの中身のハッシュ値 HEAD によって識別されるため、リポジト git cat-file tree <hash> で中身そのも リやパスによらず一意のIDをもつ のを表示することも可能(化けるけど) git hash-object <ファイル名> でhash • commit を表示(ex. git hash-object greeting コミットに関連する情報を束ねるた めのデータ構造 git cat-file blob <hash> でblob の中身 を表示 git rev-parse <ref> で、commit のid を表示できる (ex. git rev-parse • tree HEAD ディレクトリ構造を管理するデータ。 git cat-file commit <hash> でcommit の中身を表示できる
  • 10.
    おまけ1 • リポジトリにどんな objectが格納され ているかを表示するコマンド for i in $(find .git/objects/ -type f); do echo -n "$i "; git cat-file -t $(basename $ (dirname $i))$(basename $i); done
  • 11.
    おまけ2 • commit を使わないcommit echo 'Hello, World!' > greeting git add !$ treeid=$(git write-tree) commitid=$(echo “Initial commit” | git commit-tree $treeid) git update-ref refs/heads/master $commitid
  • 12.
    練習 • 1つめのcommit で/greeting ファイルを、2つ目 の commit で /folder/greeting2 ファイルをリポ ジトリに登録、3つ目のcommit で /greetingの 中身を更新したリポジトリを作ってください • そのリポジトリに対し、各 commit の commit ファイルからそれぞれの commit の tree ファイ ルと blob ファイルを表示してください
  • 13.
    解答例 $ git init $ git log --pretty=oneline Initialized empty Git repository in /Users/zaki/fromgit/ c802fa339e37c2cf6eaeca9faa40b2e38405cf44 3rd commit q1/.git/ 27a946b45cfc917a68b823c40848a7e8706f9a6a 2nd commit 09a263dd39525b5f8badd1a0eb2983e052b058e1 1st commit $ echo 'Hello!' > greeting $ git cat-file commit 09a263dd39525b5f8badd1a0eb2983e052b058e1 tree 57b6e13c3f7c0d4d9f327a878ffe5639c55148ee $ git add !$ author YAMAZAKI Makoto <makoto1975@gmail.com> 1337146517 +0900 git add greeting committer YAMAZAKI Makoto <makoto1975@gmail.com> 1337146517 +0900 $ git ci -m "1st commit" 1st commit [master (root-commit) 09a263d] 1st commit 1 file changed, 1 insertion(+) $ git ls-tree 57b6e13c3f7c0d4d9f327a878ffe5639c55148ee 100644 blob 10ddd6d257e01349d514541981aeecea6b2e741d greeting create mode 100644 greeting $ git cat-file commit 27a946b45cfc917a68b823c40848a7e8706f9a6a $ mkdir folder tree 06b2d98a9898376793134ae06dda58341af90656 parent 09a263dd39525b5f8badd1a0eb2983e052b058e1 $ echo 'Hello2!' > folder/greeting2 author YAMAZAKI Makoto <makoto1975@gmail.com> 1337146573 +0900 committer YAMAZAKI Makoto <makoto1975@gmail.com> 1337146573 +0900 $ git add !$ git add folder/greeting2 2nd commit $ git ls-tree 06b2d98a9898376793134ae06dda58341af90656 $ git ci -m "2nd commit" 040000 tree 3a06576055421d5d27ff1fe60b87d1e9ea76902c folder [master 27a946b] 2nd commit 100644 blob 10ddd6d257e01349d514541981aeecea6b2e741d greeting 1 file changed, 1 insertion(+) create mode 100644 folder/greeting2 $ git cat-file commit c802fa339e37c2cf6eaeca9faa40b2e38405cf44 tree 8422453ed2a20ecef084d404f4feac29a267a6d5 $ echo 'Hello, World!' > greeting parent 27a946b45cfc917a68b823c40848a7e8706f9a6a author YAMAZAKI Makoto <makoto1975@gmail.com> 1337146602 +0900 committer YAMAZAKI Makoto <makoto1975@gmail.com> 1337146602 +0900 $ git add !$ git add greeting 3rd commit $ git ci -m "3rd commit" $ git ls-tree 8422453ed2a20ecef084d404f4feac29a267a6d5 [master c802fa3] 3rd commit 040000 tree 3a06576055421d5d27ff1fe60b87d1e9ea76902c folder 1 file changed, 1 insertion(+), 1 deletion(-) 100644 blob 8ab686eafeb1f44702738c8b0f24f2567c36da6d greeting
  • 14.
  • 15.
    Git Immersion とは AGuided Tour that Walks trough the Fundamentals of Git
  • 16.
  • 17.
    補足など • LAB 1 --global をつけると ~/.gitconfig に、つけ ないと直近の .git/config に設定が記録さ れます。 • LAB 11 stash ってtypoしやすいので ss=stash とか もあったほうがいいと思ってる
  • 18.
  • 19.