git commit コマンドは、ステージングエリアに追加された変更をリポジトリに記録するためのコマンドです。
履歴を残す基本操作であり、メッセージを添えて変更内容を保存します。
構文(Syntax)
git commit [オプション] [-m "メッセージ"]
主なオプション一覧
| オプション | 説明 | 使用例 |
|---|---|---|
-m "msg" | コミットメッセージを指定 | git commit -m "Add new feature" |
-a | 追跡済みファイルの変更を自動でステージしてコミット | git commit -a -m "Quick fix" |
--amend | 直前のコミットを修正 | git commit --amend -m "Fix typo" |
--allow-empty | 変更がなくても空のコミットを作成 | git commit --allow-empty -m "Trigger build" |
-v | 差分をエディタに表示してからコミット | git commit -v |
実行例
通常のコミット
git add app.py
git commit -m "Add new feature to app.py"
出力例:
[main 1a2b3c4] Add new feature to app.py
1 file changed, 5 insertions(+)
追跡済みファイルをまとめてコミット
git commit -a -m "Update config and remove debug logs"
(git add を省略してコミットできるが、新規ファイルは含まれない)
直前のコミットを修正
git commit --amend -m "Correct commit message"
空のコミットを作成
git commit --allow-empty -m "Trigger CI build"
エラー例(ステージが空)
git commit -m "Test commit"
出力例:
On branch main
nothing to commit, working tree clean
関連コマンド
git add: ファイルをステージングに追加する。git status: 現在の状態を確認する。git log: コミット履歴を表示する。
備考
git commitは ステージングされた変更のみ を記録する。- メッセージなしで実行すると、エディタが開いてコミットメッセージを入力できる。
--amendは履歴を書き換えるため、公開リポジトリに push 済みのコミットには注意が必要。
参考
- Git公式ドキュメント: https://git-scm.com/docs/git-commit

コメント