> For the complete documentation index, see [llms.txt](https://imaizume.gitbook.io/git-drill/-LMR6DYjovjrTX3cyT6K/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://imaizume.gitbook.io/git-drill/-LMR6DYjovjrTX3cyT6K/add-and-commit.md).

# add & commit | ファイルのコミットログを作成する (★)

![概念図](/files/-LV9BXBH5V2jZWAzJiIe)

## 難易度 :star:

## 概要

* `git add FILE` で次のコミットログにFILEを含める
* `git commit` でコミットログを作成する

## 例題 (リポジトリ: [git-drill/add-and-commit](https://github.com/git-drill/add-and-commit))

* :computer: `list.txt` に `- banana` というテキストを追加した
* :octocat: 変更の内容を履歴として保存したい。

## 例題の模範回答

{% embed url="<https://youtu.be/rqrU0cNaPA4>" %}

1. `git status` で、`list.txt` が変更された状態(modified)であることを確認。
2. `git diff` で、変更内容が表示されることを確認。
3. `git add list.txt` で、変更内容を次のコミットに含める。
4. `git diff` で、変更内容が表示されなくなったことを確認。
5. `git diff --staged` で、変更内容が表示されることを確認。
6. `git commit -m "new item: banana"` で、コミットを作成。
7. `git log` で新しいコミットが追加されていることを確認。
8. `git diff` で新しい変更がないことを確認。

## 例題解説

**コミットは、git上での変更箇所のまとまりです。** ファイルを変更した場合には、**変更内容を** `git commit` **コマンドでコミットに記録することができます。**

ただし`git commit`を実行する前に、次のコミットに変更箇所を含めることを事前にgitへ伝える必要があります。 **そこで** `git add` **コマンドを使って、次のコミットに変更を含めるようgitに指示します。** 今回は `list.txt` に加えた変更を、次のコミットに含めるようを指示しました。

**また**`git add list.txt`**の実行前後で** `git diff` **コマンドの結果が異なっています。** これは`git add`によって、次のコミットに変更を含めることが確定したため、通常の`diff`では表示されなくなったためです。 コミット予定の変更内容を確認したい場合には`git diff --staged`を実行します。

そして `git commit` 後に `git diff` を実行しても変更差分が表示されなくなり、無事にコミットが作成されたことがわかります。 本例題では1ファイルの変更でコミットを作成しましたが、**複数のファイルを変更した場合も同じ方法でコミットが作成できます。** 詳しくは演習問題で試してみましょう。

**なお** `git commit -m` **を** `git commit` **(**`-m`**なし)で実行した場合、コミットメッセージを編集するためのエディタ(vim)が起動します。** vimは他のエディタと異なり特殊なコマンドによる操作が必要になります。 **操作方法が分からない人はvimの画面で** `:q!` **と入力して画面を閉じましょう。**

## 演習 (リポジトリ: 例題と同じ)

## 演習1

例題に続けて `list.txt` に自分の好きなように変更を加え、例題回答と同じ手順でコミットを作成してください。 これを慣れるまで繰り返してみましょう。

## 演習2

`list.txt` をコピーした新しいリスト`list2.txt`を作成して、任意の内容を書き込みコミットしてください。 また前後で`git diff`も試してみましょう、例題の場合と何が違うでしょうか。

## 演習3

`list.txt` と `list2.txt` の両方に変更を加えた状態で、`git add list.txt` を実行しましょう。 どんな状態になるでしょうか。 また続けてコミットした場合には、どんな内容がコミットに記録されるでしょうか。

## 演習4

好きな画像ファイルをリポジトリに追加しましょう。 このとき`git diff`では何が表示されるでしょうか。

## 演習の模範回答

{% embed url="<https://youtu.be/AaZXcFil1gk>" %}

こちらの動画で回答を確認してください(テキストでの解説はありません)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://imaizume.gitbook.io/git-drill/-LMR6DYjovjrTX3cyT6K/add-and-commit.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
