git commit コマンド

Git 基本操作Git 基本操作


前の章では git add コマンドを使ってコンテンツをステージング領域に書き込みました。

git commit コマンドはステージング領域のコンテンツをローカルリポジトリに追加します。

ステージング領域をローカルリポジトリにコミットする:

git commit -m [message]

[message] には備考情報を指定できます。

ステージング領域の指定ファイルをリポジトリにコミットする:

$ git commit [file1] [file2] ... -m [message]

-aパラメータ設定: ファイルを変更した後、git add コマンドを実行する必要はなく、直接コミットします。

$ git commit -a

コードをコミットするときのユーザー情報を設定する

開始する前に、コミットするユーザー情報(ユーザー名とメールアドレス)を設定する必要があります:

$ git config --global user.name 'サンプル'
$ git config --global user.email [email protected]

--global パラメータを削除すると、現在のリポジトリにのみ有効になります。

変更をコミットする

次に、hello.php のすべての変更をステージング領域のコンテンツとしてローカルリポジトリに追加できます。

次の例では、-m オプションを使用してコマンドラインでコミットコメントを提供します。

$ git add hello.php
$ git status -s
A  README
A  hello.php
$ git commit -m '第一次版本提交'
[master (root-commit) d32cf1f] 第一次版本提交
 2 files changed, 4 insertions(+)
 create mode 100644 README
 create mode 100644 hello.php
 

これでスナップショットが記録されました。再度 git status を実行すると:

$ git status
# On branch master
nothing to commit (working directory clean)

上記の出力は、最新のコミット以降に何も変更していないことを示しており、"working directory clean"、つまり「クリーンな作業ディレクトリ」です。

-m オプションを設定しない場合、Git はコミット情報を記入するためにエディタを開こうとします。Git がその設定で関連情報を見つけられない場合、デフォルトで vim が開きます。画面は次のようになります:

# 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 <file>..." to unstage)
#
# modified:   hello.php
#
~
~
".git/COMMIT_EDITMSG" 9L, 257C

git add でコミットをステージングする手順が面倒だと感じる場合は、Git では -a オプションを使用してこの手順をスキップすることもできます。コマンド形式は次のとおりです:

git commit -a

まず hello.php ファイルを次の内容に変更します:

<?php
echo 'サンプル:www.example.com';
echo 'サンプル:www.example.com';
?>

次に以下のコマンドを実行します:

$ git commit -am '修改 hello.php 文件'
[master 71ee2cb] 修改 hello.php 文件
 1 file changed, 1 insertion(+)


Git 基本操作Git 基本操作

その他の拡張