#contents

** git(ギット)とは? [#k7cf0725]

- バージョン管理システム
- 公式サイト: http://git-scm.com/
- 必要となる知識: Linux, vim
- 動作環境: Linuxローカル環境(CentOS 5.11 - 32bit)

** gitインストール [#n54a3bde]

 # yum -y update
 # yum -y install git
 Loaded plugins: fastestmirror
 Loading mirror speeds from cached hostfile
  * base: ftp.iij.ad.jp
  * extras: ftp.iij.ad.jp
  * updates: ftp.iij.ad.jp
 Setting up Install Process
 No package git available.
 Nothing to do

- yum install git失敗のためEPELリポジトリをインストール

 # wget http://ftp-srv2.kddilabs.jp/Linux/distributions/fedora/epel/5/i386/epel-release-5-4.noarch.rpm 
 # rpm -ivh epel-release-5-4.noarch.rpm
 # vi /etc/yum.repos.d/epel.repo
 --------------------
    [epel]
    ...
    enabled=1
    priority=1
    ...
 --------------------

 # yum -y install git
 # git --version
 git version 1.8.2.1

** バージョン管理の流れ [#ofbb42e8]

+ 作業ディレクトリ
+ ステージングエリア(インデックス)
+ リポジトリ(ローカル、リモート)

** gitの設定 [#i8e137e1]

- 必要&便利な設定

 $ git config --global user.name "Yuji Shimojo"
 $ git config --global user.email "yjshimojo@gmail.com"
 $ git config --global color.ui true

- 設定を確認

 $ git config -l
 user.name=Yuji Shimojo
 user.email=yjshimojo@gmail.com
 color.ui=true

- gitコマンドのヘルプ

 $ git config --help
 $ git help config

** リポジトリへのコミット [#hf6393d7]

- 作業用ディレクトリ作成

 $ mkdir myweb
 $ cd myweb/

- 作成したディレクトリをgitで使用する

 $ git init
 Initialized empty Git repository in /home/yuji/myweb/.git/

- ファイルを追加

 $ vi index.html
 --------------------
     line 1
 --------------------
 $ cat index.html
 line 1

- 作業ディレクトリ=>ステージングエリアへファイルをアップ

 $ git add index.html

- ステージングエリア=>リポジトリへファイルをアップ

 $ git commit
 --------------------
     initial commit // コミットメッセージ(任意)
 --------------------
 [master (root-commit) 12662e4] initial commit
  1 file changed, 1 insertion(+)
  create mode 100644 index.html

- コミット履歴の確認
** コミット履歴(ログ)の確認・オプション [#r80d1635]

 $ git log
 commit 12662e4f226f299708ddb5c89274739fd355f9b8 // 一意のコミットID
 Author: Yuji Shimojo <yjshimojo@gmail.com>
 Date:   Sun Sep 20 03:03:31 2015 +0900
     initial commit          // コミットメッセージ

- 1行でログを表示

 $ git log --oneline
 12662e4 initial commit

- 変更された場所を表示

 $ git log -p
 diff --git a/index.html b/index.html
 new file mode 100644
 index 0000000..89b24ec
 --- /dev/null
 +++ b/index.html
 @@ -0,0 +1 @@
 +line 1

- 変更されたファイルを表示

 $ git log --stat
 commit 12662e4f226f299708ddb5c89274739fd355f9b8
 Author: Yuji Shimojo <yjshimojo@gmail.com>
 Date:   Sun Sep 20 03:03:31 2015 +0900
 
     initial commit
 
  index.html | 1 +
  1 file changed, 1 insertion(+)

** 状態確認 [#t762b327]

 $ vi index.html
 --------------------
     line 1
     line 2
 --------------------

- 現在の状態を確認

 $ git status
 # On branch master
 # Changes not staged for commit:        // ステージング未反映、未コミット
 #   (use "git add <file>..." to update what will be committed)    // git addでステージングに反映可能
 #   (use "git checkout -- <file>..." to discard changes in working directory)    // git checkoutで変更前の状態に戻すことが可能
 #
 #	modified:   index.html
 #
 no changes added to commit (use "git add" and/or "git commit -a")

- 変更前の状態に戻す

 $ git checkout -- index.html
 $ cat index.html
 line 1

** 差分確認 [#oa8047aa]

- 作業ディレクトリ上のファイルの変更差分確認

 $ git diff
 diff --git a/index.html b/index.html
 index 89b24ec..7bba8c8 100644
 --- a/index.html
 +++ b/index.html
 @@ -1 +1,2 @@
  line 1
 +line 2

 $ git add index.html    // ステージングエリアへ追加
 $ git status
 # On branch master
 # Changes to be committed:
 #   (use "git reset HEAD <file>..." to unstage)
 #
 #	modified:   index.html
 #

- ステージングエリア上のファイルの変更差分確認

 $ git diff --cached
 diff --git a/index.html b/index.html
 index 89b24ec..7bba8c8 100644
 --- a/index.html
 +++ b/index.html
 @@ -1 +1,2 @@
  line 1
 +line 2

** gitでのファイル操作 [#ea512f9e]

- 複数ファイルをステージングエリアへ追加

 $ git add [filename1] [filename2] [filename3] ...

- カレントディレクトリ配下のファイルを全てステージングエリアへ追加

 $ git add .

- ファイルの削除・移動 ※addやcommit済みのファイルはgitコマンドを使用

 $ git rm index.html
 $ git mv file1 file2

** git管理からの除外設定 [#j5d8d38d]

 $ ls
 error.log  index.html

- .gitignoreファイルを作成しgit管理から除外するファイルを指定 ※当該ファイルが配置されたカレントディレクトリ&配下に適用

 $ vi .giignore
 --------------------
   *.log
 --------------------

** 直前のコミットを変更 [#qcd0ad2b]

- 画面遷移させずにコミットメッセージを追加

 $ git commit -m "ライン2を追加"
 [master 51e7ce3] ライン2を追加
  1 file changed, 1 insertion(+)

- 直前のコミットを変更 ※変更のため新たにコミット履歴(コミットID)が追加されない

 $ git commit --amend

** 過去のバージョンに戻る [#fbe88d2c]

- git add済み後にステージングエリア・作業ディレクトリ共に直前のバージョンに戻す

 $ git reset --hard HEAD

- 2つ前のバージョンに戻す

 $ git reset --hard HEAD^

- コミットIDを指定して過去バージョンに戻す ※IDは完全一致でなくても一意に特定可能な桁数であればOK(最低7桁)

 $ git reset --hard 12662e4f226f

- git resetで過去バージョンに一旦戻したがその操作をやり直したい(リドゥしたい)場合

 $ git reset --hard ORIG_HEAD

- ORIG_HEADには前回取り消されたHEADの情報が1つだけ保存

** ブランチを活用 [#j550fa6a]

- ブランチは分岐の意で別々のバージョンを並行して開発したい場合に利用
- ブランチ一覧表示(*は現在使用中のブランチ)

 $ git branch

- ブランチ作成

 $ git branch hoge

- ブランチを切替え

 $ git checkout hoge
 Switched to branch 'hoge'

** ブランチをマージ [#f5bf2191]

 $ git branch
   hoge
 * master

- masterブランチにhogeブランチの変更内容をマージ

 $ git merge hoge
 Updating 556ff5f..054edfd
 Fast-forward
  myscript.js | 1 +
  1 file changed, 1 insertion(+)
  create mode 100644 myscript.js

- ブランチを削除

 $ git branch -d hoge
 Deleted branch hoge (was 054edfd).


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS