0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【勉強用】Gitのコマンドを実際に動かしてみた(③ブランチ編)

0
Last updated at Posted at 2023-11-03

はじめに

今回はGitのコマンドを実際に動かして、バージョンの確認や変更について理解を深めたいと思います。参考図書はこちらの動かして学ぶ!Git入門を使用しました。

image.png

コマンド

19. git branch

このコマンドを実行すると、現在あるブランチの一覧と、どのブランチにいるか(HEADがどのブランチに乗っているか)が表示されます。

それでは、実行して確認してみましょう。

Git Bash
$ git  branch
* main

現在はmainといブランチにいることが分かりました。

20 . git branch ブランチ名

このコマンドを実行すると、指定した名前のブランチを作成できます。

それではpracticeというブランチを作成してみましょう。

Git Bash
$ git branch practice
$ git branch
* main
  practice

新たにpracticeブランチを作成できました。このコマンドではHEAD自体は移動しません。また-vオプションを加えるとブランチの先端があるコミットの情報も表示されます。

それでは実行してみましょう。

Git Bash
$ git branch -v
* main     0e420a2 Have a nice day !
  practice 0e420a2 Have a nice day !

practiceブランチにはまだ何もコミットしていないので、masterブランチと同じコミットとなります。

21. git checkout ブランチ名

このコマンドはブランチ間を移動する際に使用するコマンドです。

それでは、mainブランチからpracticeブランチに移動してみましょう。

Git Bash
$ git checkout practice
Switched to branch 'practice'

$ git branch
  main
* practice

mainブランチからpracticeブランチに移動することが出来ました。

ただし、ここで注意が必要です。現在いるブランチにコミットしていないステージングや作業ツリーが存在する際にブランチを移動すると、Gitがエラーを検知して切り替えをやめてしまいます。変更が失われてしまう可能性があるためです。

22. git commit (他ブランチ)

他ブランチに移動した後に、変更をコミットすると移動元のブランチを変更することなく、移動後のブランチを伸ばすことが出来ます。

それでは新たにprac.txtを作成し、practiceブランチにコミットし、HEADが移動したか確認してみましょう。

Git Bash
#prac.txtを作成
$ git add prac.txt
$ git commit -m "commit prac.txt"
[practice ba022a9] commit prac.txt
 1 file changed, 1 insertion(+)
 create mode 100644 prac.txt

$ git log
commit ba022a93f4b5b1c7a5572ae3b62b7d155b750e6b (HEAD -> practice)
Author: HANAKO <hanako@sample.com>
Date:   Sat Nov 4 06:19:35 2023 +0900

    commit prac.txt

commit 0e420a2f7324894fe09791f137d93d3fa3e07959 (main)
Author: HANAKO <hanako@sample.com>
Date:   Fri Nov 3 20:36:10 2023 +0900

    Have a nice day !

commit aa365952d8d38be4538bde4703f51ca27e76c98e
Author: HANAKO <hanako@sample.com>
Date:   Fri Nov 3 20:32:42 2023 +0900

    Enjoy !

commit 714ed154be5186cca4f5fc1b540526fb207ccb2a
Author: HANAKO <hanako@sample.com>
Date:   Fri Nov 3 15:30:56 2023 +0900

無事、practiceブランチにコミットすることが出来ました。

23. git log --all --graph

このコマンドを実行すると、ブランチの移動を考慮したコミットの流れを視覚的に表示します。

それでは、これまでのコミットの流れを確認してみましょう。

Git Bash
$ git log --all --graph
* commit ba022a93f4b5b1c7a5572ae3b62b7d155b750e6b (HEAD -> practice)
| Author: HANAKO <hanako@sample.com>
| Date:   Sat Nov 4 06:19:35 2023 +0900
|
|     commit prac.txt
|
* commit 0e420a2f7324894fe09791f137d93d3fa3e07959 (main)
| Author: HANAKO <hanako@sample.com>
| Date:   Fri Nov 3 20:36:10 2023 +0900
|
|     Have a nice day !
|
* commit aa365952d8d38be4538bde4703f51ca27e76c98e
| Author: HANAKO <hanako@sample.com>
| Date:   Fri Nov 3 20:32:42 2023 +0900
|
|     Enjoy !
|
* commit 714ed154be5186cca4f5fc1b540526fb207ccb2a
| Author: HANAKO <hanako@sample.com>
| Date:   Fri Nov 3 15:30:56 2023 +0900
|
|     check status

視覚的にコミットの流れが分かりましたね。またコミットの流れ自体を見たいときは、--onelineオプションを追加します。

それでは、実行してみましょう。

Git Bash
$ git log --all --oneline
ba022a9 (HEAD -> practice) commit prac.txt
0e420a2 (main) Have a nice day !
aa36595 Enjoy !
714ed15 check status
e6e9a3b first commit

先ほどより見やすくなりましたね。詳しいコミットの状態も確認したいときは--graph、流れを確認したいときは--onelineオプションを加えると良さそうです。

24. git merge 取り込みたいブランチ名

Gitを使った開発では、一般にはmasterブランチが開発の主軸で、最終的な成果は基本的にこのブランチに作り上げます。そのため最終的には、開発専用のブランチやエラー修正専用のブランチをmasterブランチに取り込む必要があります。このときに行うコマンドがgit mergeです。

それでは、practiceブランチに新たにmerge.txtを作成し、コミットします。その後、その変更をmainブランチにマージしてみましょう。

Git Bash
#practiceブランチにmerge.txtを追加
$ git add merge.txt
$ git commit -m "commit merge.txt"
[practice c691967] commit merge.txt
 1 file changed, 1 insertion(+)
 create mode 100644 merge.txt

$ git checkout main
Switched to branch 'main'

$ git merge practice
Updating 0e420a2..c691967
Fast-forward
 merge.txt | 1 +
 prac.txt  | 1 +
 2 files changed, 2 insertions(+)
 create mode 100644 merge.txt
 create mode 100644 prac.txt

無事、practiceブランチをmainブランチにマージすることが出来ました。

また、git log --all --onelineでブランチの状態を確認してみましょう。

Git Bash
$ git log --all --oneline
c691967 (HEAD -> main, practice) commit merge.txt
ba022a9 commit prac.txt
0e420a2 Have a nice day !
aa36595 Enjoy !
714ed15 check status
e6e9a3b first commit

HEADがmainブランチとpracticeブランチに重ねっているのが確認できます。

25. コンフリクトの修正

マージするとき、一方のブランチでの変更箇所が他方のブランチの変更箇所と重なっていると、自動での変更適用が失敗します。この状態をコンフリクトといいます。どちらの変更を適用するのかGitには判別できないので、ユーザが適用結果を教えてやる必要があります。

それでは、mainブランチのhello.txtにはYeah!という文章を、practiceブランチのほうにはHoo!という文章を追加し、それぞれコミットしてからmainブランチにマージしてみましょう。

Git Bash
mainブランチのhello.txtに`Yeah!`という文章を追加
$ git add hello.txt
$ git commit -m "Yeah !"
[main 8ba08a1] Yeah !
 1 file changed, 2 insertions(+), 1 deletion(-)

#practiceブランチのhello.txtに`Hoo!`という文章を追加
$ git add hello.txt
$ git commit -m "Hoo.txt"
[practice 1ca01f9] Hoo.txt
 1 file changed, 2 insertions(+), 1 deletion(-)

#mainブランチに移動
$ git merge practice
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.

無事(?)コンフリクトが発生しました。

それでは、コンフリクトの内容を確認してみましょう。hello.txtの中身を確認してみましょう。

hello.txt
hello!
Hello, everyone!
Have a nice day !
<<<<<<< HEAD
Yeah !
=======
Hoo !
>>>>>>> practice

ここでは=======を境にHEAD(ここではmainブランチ)にYeah !という変更、practiceブランチにHoo !という変更が加えられていることが分かります。

それでは、Hoo !という文章を削除して再度マージしましょう。

Git Bash
#hello.txtからHoo !という文章を削除
$ git add hello.txt
$ git commit -m "resolve conflict"
[main b79fc14] resolve conflict

hello.txtの中身を再度確認してみましょう。

Git hello.txt
hello!
Hello, everyone!
Have a nice day !
Yeah !

無事マージされましたね。

補足:

コンフリクトが起きたときには、作業ツリーも、ステージも、.gitディレクトリ内もマージの途中になっています。コンフリクトの解消も行わずに、マージ自体を取りやめたい場合はgit merge --abortを実行します。これを実行すると、マージ前の状態に戻してくれます。

26. git branch -d

これはブランチを削除するコマンドです。これは削除したいブランチ外にいないと実行できません。

それではpracticeブランチを削除してみましょう。

Git Bash
$ git branch -d practice
Deleted branch practice (was 1ca01f9).

$ git branch
* main

無事に、practiceブランチを削除できましたね。

27. 早送りマージ

これは変更を取り込むブランチ先端が、取り込まれるブランチ先端の祖先である場合に可能なマージです。早送りマージマージコミットを作らずにポインタを進めるだけで済ませます。原理上コンフリクトは発生しません。

それでは、practice2ブランチを作成し、"prac2.txt"を作成した後、早送りマージしてみましょう。

Git Bash
#practice2ブランチでprac2.txtを作成
$ git add prac2.txt
$ git commit -m "commit prac2.txt"
[practice2 3d2ad50] commit prac2.txt
 1 file changed, 1 insertion(+)
 create mode 100644 prac2.txt

$ git checkout main
Switched to branch 'main'

$ git merge practice2
Updating b79fc14..3d2ad50
Fast-forward #早送りマージが行われている
 prac2.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 prac2.txt

無事に早送りマージが行われていますね。

まとめ

今回の記事では、ブランチの操作を中心に学習しました。次回はタグの扱いについて学習したいと思います。

前回:【【勉強用】Gitのコマンドを実際に動かしてみた(②コミット編)
次回:【勉強用】Gitのコマンドを実際に動かしてみた(④タグ編)

参考文献

動かして学ぶ!Git入門

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?