# git merge

では実際にmergeしてみましょう。

現在`feature1`にcommitが1つ積まれている状態で、`main`の方は`feture1`を切った時点からcommitを積まれていません。`feature1`を`main`にmergeしましょう。`main`にいることを確認して、以下のコマンドを叩きましょう。`--no-ff`をオプションとして指定しない場合、`feature1`のみにcommitが積まれていると`Fast-forward`でのmergeになるのため、3way mergeをする場合は指定します。

```bash
git merge --no-ff feature1
```

上記を叩くと、エディタが開き以下のように表示されるかと思います。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2FhRnS8oD1WRNmOLZBouXV%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-27%2013.52.48.png?alt=media&#x26;token=78fd7319-ff95-4184-93ae-061e36a2c23f" alt=""><figcaption></figcaption></figure>

commit messageがデフォルトで`` Merge branch 'feature1` ``と入力されているかと思います。こちらのファイルを閉じましょう。

git logにてcommitを確認しましょう。--graphオプションをつけると、以下のようにmergeした際の図式を視覚的にみやすく表示することができます。

```bash
git log --oneline --graph
```

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2FDkwLs2fvlw75MZtKOh9s%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-27%2014.00.58.png?alt=media&#x26;token=cccbf4e7-9486-4254-a0a5-e6ec4e8f19f0" alt=""><figcaption></figcaption></figure>

次にmainとfeatureでそれぞれcommitを積んで、mergeしてみましょう。

以下コマンドを叩き、`feature2`にて作業します。

```bash
git switch -c feature2
```

`a.txt`を以下のように編集しましょう。

<pre data-title="a.txt◎"><code>sample1
tarou
good evening
<strong>world
</strong></code></pre>

その後、`git add`、`git commit`をしましょう。

```bash
git add .
```

```bash
git commit
```

commit messageは以下とします。

```
a.txtにworldを追加
```

`main`に切り替えましょう。

```bash
git switch main
```

`b.txt`を以下のように編集します。

<pre class="language-bash" data-title="b.txt◎"><code class="lang-bash">sample2
<strong>tarou
</strong></code></pre>

その後、`git add`、`git commit`をしましょう。

```bash
git add .
```

```bash
git commit
```

commit messageは以下とします。

```
b.txtにtarouを追加
```

ではmergeしてみましょう。

```bash
git merge --no-ff feature2
```

commit messageはデフォルトのままとします。

再度git logにて確認してみましょう。

```bash
git log --oneline --graph
```

以下のように表示されるかと思います。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2FP1FpvYBfMHCEOrimlMTJ%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-27%2014.11.02.png?alt=media&#x26;token=fc3ecddf-8f33-4abd-8036-62052c1bfb10" alt=""><figcaption></figcaption></figure>
