gitコマンドの使い方 ~ git commit ~

git commit コマンド概要

git commit コマンドは次の2つのオブジェクトを作成しリポジトリに登録するコマンドです。

  • treeオブジェクト:ディレクトリの情報
  • commitオブジェクト:blobオブジェクトやtreeオブジェクトなどのhash値をまとめた

commitオブジェクトで管理される情報を知ることでcommitコマンドをより理解することができます。

commitオブジェクトで管理される情報とその元となる情報は以下の通りです。

項目内容
commit-hashcommitオブジェクトから生成される40文字のhash値
tree-hashtreeオブジェクトから生成される40文字のhash値
treeオブジェクトとはディレクトリ情報
blob-hashblobオブジェクトから生成される40文字のhash値
blobオブジェクトとは圧縮されたファイル情報
Authorcommitを作成したユーザ名とメールアドレス
Datecommit作成日付
commitメッセージcommit作成時に入力したcommitメッセージ

git commit コマンドの使い方

git commit [オプション]

commitコマンドの概要で説明したcommitオブジェクトを作成し、HEADの位置を作成したcommitに変更します。

git commit オプション

オプションなし

git commit

commitメッセージをエディタ(デフォルトはVim)で入力した後にcommitオブジェクトが作成されます。

Vimの場合このような画面が表示されますので、メッセージを入力後に「Esc」キーでコマンドモードに変更し「wq」と入力後に「Enter」キーでcommitメッセージを保存することができます。

# 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:
#       deleted:    file-A.txt
#
# Untracked files:
#       file-A.txt
#

-m

git commit -m 'コミットメッセージ'

-mオプションを指定するとコミットメッセージの入力と同時にcommitオブジェクトを作成することができます。

-a

git commit -a

staging-area(ステージングエリア)に登録されているファイルをworking-directory(作業ディレクトリ)で更新・削除した場合に、ステージング処理(git add)とcommitオブジェクトの作成を同時に行うことができます。

-aオプションはコマンドの入力回数を省略する目的で利用するため、-mオプションと併用して実行することが多いです。

併用する場合は以下のいずれかで指定します。

git commit -a -m 'コミットメッセージ'

git commit -am 'コミットメッセージ'

一連の操作の流れ

$ echo 'aaa' > file-A.txt

$ git add .

$ git commit -m 'file-A add'
[master (root-commit) 952ba9b] file-A add
 1 file changed, 1 insertion(+)
 create mode 100644 file-A.txt

$ git log --oneline
952ba9b (HEAD -> master) file-A add

$ echo 'AAA' >> file-A.txt

$ git commit -am 'file-A upd'
warning: LF will be replaced by CRLF in file-A.txt.
The file will have its original line endings in your working directory
[master dc92520] file-A upd
 1 file changed, 1 insertion(+)

$ git log --oneline
dc92520 (HEAD -> master) file-A upd
952ba9b file-A add

–amend

git commit --amend

git commit --amend -m 'コミットメッセージ'

直前のcommitを書き換えます。

軽微な修正(インデントを間違えたなど)した場合などに利用する。

以下の例は、1つ目のcommitの後にファイルに1行追加し–amend指定してcommitを変更しています。

git log でcommitが1つだけになっていることに注目して下さい。

$ echo 'aaa' > file-A.txt

$ git add .

$ git commit -m 'file-A add'
[master (root-commit) e72fcb0] file-A add
 1 file changed, 1 insertion(+)
 create mode 100644 file-A.txt

$ echo 'AAA' >> file-A.txt

$ git add .

$ git commit --amend -m 'file-A upd'
[master ebd499a] file-A upd
 Date: Sun Apr 5 14:42:42 2020 +0900
 1 file changed, 2 insertions(+)
 create mode 100644 file-A.txt

$ git log --oneline
ebd499a (HEAD -> master) file-A upd

git commit の全てのオプションを確認する方法

以下のコマンドを実行するとブラウザでgit commitのヘルプページが表示される

git commit --help

Gitコマンドの使い方一覧

Git設定git config ~Gitリポジトリの設定~
初期化git init ~ローカルリポジトリの構築~
ログ&設定値確認git log ~commitログの履歴確認~
git status ~作業ディレクトリとステージングエリアの更新状態確認~
git diff ~commitやファイルの差分確認~
git show ~commitの内容確認~
git remote ~リモート追跡ブランチのリモートリポジトリの設定確認~
git reflog ~HEADやブランチの移動履歴の確認と整理~
ステージングエリアの操作git add ~ステージングエリア(インデックス)にファイル追加~
git ls-files ~ステージングエリア(インデックス)のファイル一覧表示~
git rm ~ステージングエリア(インデックス)のファイル削除~
ローカルリポジトリの操作git commit ~blobやtreeなどをまとめたcommitを作成~
git merge ~指定したブランチの内容を取り込み新しいcommitを作成~
git rebase ~commitの履歴の整理~
git cherry-pic ~特定のcommitの変更内容だけを取り込む~
git cat-file ~リポジトリのオブジェクト(commit,tree,blob)の内容表示~
git revert ~指定したcommitを取り消すcommitを作成~
commitのエイリアスなどgit branch ~commitツリーの枝(ブランチ)を作成~
git tag ~リリースなどのタイミングで特定のcommitに名前を付ける~
git stash ~commitを作成する前の変更内容を一時保存~
commitの内容をステージングエリアや作業ディレクトリに反映git checkout ~commitをステージングエリアと作業ディレクトリに展開~
git reset ~HEADの位置やステージングエリアと作業ディレクトリの内容を変更~
リモートリポジトリとのやり取りgit clone ~リモートリポジトリからローカルリポジトリ作成~
git fetch ~リモートリポジトリのブランチをローカルリポジトリに反映~
git pull ~git fetchとgit mergeを同時に行う~
git push ~ローカルリポジトリのブランチをリモートリポジトリに反映~