git を使い始める

インストールなど


$ git --version
git version 1.8.3.3
参考=>Mac OSX に git をインストールする (Homebrew を使用) - asa nisi masa

git の 初期設定を行う

自分のプロフィールを設定する


$ git config --global user.name "hoge hoge"
$ git config --global user.email "hogehoge@gmail.com"

コマンド出力の色を自動に設定する


$ git config --global color.ui auto

git のリポジトリを作成する

作業用のディレクトリを作成する。


$ mkdir $HOME/test

リポジトリを初期化する。


$ git init
Initialized empty Git repository in /Users/hoge/test/.git/

ファイルをコミットする

準備

カレントディレクトリには、index.html を作成済。

add

カレントディレクトリ配下のファイルをインデックスに記録する。


$ git add .

commit

ファイルをコミットする。

  • コミットの概要をコマンドの引数で指定する場合、 -m オプションで指定する。


$ git commit -m "first commit"

[master (root-commit) ff4e462] first commit
1 file changed, 7 insertions(+)
create mode 100644 index.html

  • コミットのログをエディタで指定する場合、git commit だけ入力すると、エディタで入力できる。


$ git commit

vi エディタに以下のメッセージが初期表示される


# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
#
# modified: index.html
#

概要、空白、理由を記入して保存しエディタを終了する。#始まり行は記録されないので残ったままでよい。


コミットのテスト     ・・概要
             ・・空白
チュートリアル      ・・理由

  • (後で調べる)チケットシステムと連動している場合、概要の行にチケット番号を書けばよいのかな。
  • エディタを保存せずに終了すると、コミットがキャンセルされるとのこと。

コミット結果が表示され、コメントの概要部分が表示された。以下は、後述する変更のコミット時のメッセージだが、参考に貼っておく。


[master 8c073d7] コミットのテスト
1 file changed, 2 insertions(+), 1 deletion(-)

ファイルを変更する

変更する

お好みのエディタで変更。

diff

変更箇所を確認する


$ git diff index.html

diff --git a/index.html b/index.html
index 18720b1..b213557 100644
--- a/index.html
+++ b/index.html
@@ -2,6 +2,7 @@
test

test1
-test2
+test2-change
+test3-add


  • 変更前は、 - で表示される
  • 変更後は、 + で表示される
add

変更を記録する

  • ファイルを指定する場合。


$ git add index.html

  • ワークツリー内の全ての変更を記録する場合。


$ git add -u

commit

コミットすると結果が表示され、コメントの概要部分が表示された。


git commit

[master 8c073d7] 変更をコミットするテスト
1 file changed, 2 insertions(+), 1 deletion(-)

Tips

  • バージョン管理対象外のファイルを指定することができる
    • .gitignore ファイルに記載すると、対象外にできる

コマンドとオプションのメモ

コミット前にインデックスへ記録
git add <パス> ディレクトリやファイルを指定して add する ディレクトリ指定の場合、バージョン管理されていない新規ファイルは add されない
git add -u ワークツリーに含まれるバージョン管理対象のファイルを一律 add する バージョン管理されていない新規ファイルは add されない
git add -A git add -u のファイルに加え、新規ファイルも対象として add する .ignore 指定されているものは add されない
git add -p 変更内容のうち、コミットに含める/含めないものを対話的に指定して add する
コミットする
git commit <パス> ディレクトリやファイルを指定してコミット
git commit -m "コミット概要" コミット概要をオプションで指定してコミット
git commit コミットの概要、理由をエディタで入力
git commit -a 一括コミットする
git commit -v コミットログの記録時に、パッチを再確認する 例1を参照

例1)-v オプションをつけた場合の、エディタの表示例。赤字部分が付加されている。


# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
#
# new file: test.data
#
diff --git a/test.data b/test.data
new file mode 100644
index 0000000..ccc3e7b
--- /dev/null
+++ b/test.data
@@ -0,0 +1 @@
+aaaaa

コミット前に変更を確認する
git diff バージョン管理されている全てのファイルの変更内容を表示する 例1
git status コミットされていないものを確認する 例2


例1)git diff - 2つのファイルを変更


$ git diff

diff --git a/index.html b/index.html
index b213557..e525f90 100644
--- a/index.html
+++ b/index.html
@@ -4,5 +4,6 @@
test1
test2-change
test3-add
+test4-add2


diff --git a/test.data b/test.data
index ccc3e7b..745dfcb 100644
--- a/test.data
+++ b/test.data
@@ -1 +1,2 @@
aaaaa
+bbbb


例2)git status - 2ファイル変更、1ファイル新規作成した状態


$ git status

# On branch master
# Changes not staged for commit:
# (use "git add ..." to update what will be committed)
# (use "git checkout -- ..." to discard changes in working directory)
#
# modified: index.html
# modified: test.data
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
# test2.data
no changes added to commit (use "git add" and/or "git commit -a")

コミット後にコミット内容を確認する
git log コミットの履歴を表示する 例1
git log --pretty=short コミットの履歴を表示する。コメントは1行目のみ表示する 例2
git log -<世代数> 指定した世代数分の履歴を表示する 例2
git -p 履歴をパッチ形式で表示する 例2
git show 直前のコミットの内容を表示する

例1)git log - 2世代のコミットを作成


$ git log

commit 8c073d791ed2bfa64b3c396ff736ede030d729f0
Author: hoge hoge
Date: Sun Aug 4 11:07:16 2013 +0900

変更をコミットするテスト

チュートリアル

commit ff4e46265a1b96e91e4492f23cfef1c6ec49b28a
Author: hoge hoge
Date: Sun Aug 4 10:48:22 2013 +0900

first commit


例2)1世代のみ、パッチ形式で表示する。コメントは1行目のみ表示する。


$ git log -1 -p --pretty=short

commit 982efc9bf67ee0727c853ba7772b27c69fbcfa67
Author: Reiko Hisada

ファイル追加

diff --git a/test.data b/test.data
new file mode 100644
index 0000000..ccc3e7b
--- /dev/null
+++ b/test.data
@@ -0,0 +1 @@
+aaaaa

その他のコマンド

この辺りのサイトが適当そうなのでリンクを貼っておきます。
Gitを使いこなすための20のコマンド | OSDN Magazine

参考書

開発ツール徹底攻略 (WEB+DB PRESS plus)

開発ツール徹底攻略 (WEB+DB PRESS plus)