概要
- ブランチをつくってコミットした内容は、他のブランチに影響しない
- ブランチ作成後に新たなコミットがない状態で他のブランチの作業結果をマージすると、その結果が取り込まれて作業後に
HEADが移動する - このようなマージをfast-forwardという(単に他のブランチの作業後まで早送りしているイメージ)
準備
確認用にbranchリポジトリーを作ってclone。
|
1 2 3 4 5 6 |
[vagrant@localhost git]$ git clone git@github.com:taustation/branch branch Cloning into 'branch'... remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (3/3), done. |
ワーキングディレクトリーに移動してログを確認。
|
1 2 3 4 5 6 |
[vagrant@localhost branch]$ git log commit ad931b08026b6a1ba68350176ae892bcb115a23e (HEAD -> main, origin/main, origin/HEAD) Author: taustation <88479749+taustation@users.noreply.github.com> Date: Sun Aug 8 22:40:29 2021 +0900 Initial commit |
mainブランチでイニシャルコミットが実行された状態。これを図にすると以下のような状態。
|
1 2 |
initial commit <main> ○ HEAD |
新たなブランチの作成
以下のコマンドで、新たなブランチtopicを作成する。
git branch ブランチ名
また以下のコマンドでブランチの状態を表示させて、作成結果を確認。
git brranch
|
1 2 3 4 |
[vagrant@localhost branch]$ git branch topic [vagrant@localhost branch]$ git branch * main topic |
新たにtopicブランチが作成されているが、現在はmainブランチにいることが確認できる(mainに*が付いている)。
ブランチ作成直後のログは以下の通り。
|
1 2 3 4 5 6 |
[vagrant@localhost branch]$ git log commit ad931b08026b6a1ba68350176ae892bcb115a23e (HEAD -> main, origin/main, origin/HEAD, topic) Author: taustation <88479749+taustation@users.noreply.github.com> Date: Sun Aug 8 22:40:29 2021 +0900 Initial commit |
内容は変わっていないが、HEADがmainなどに加えて新たにtopicも指している。
|
1 2 3 4 5 |
mainブランチでイニシャルコミットが実行された状態。 initial commit <main> ○ HEAD <topic> |
ブランチの移動
作成したtopicブランチに移動する。指定したブランチに移動するには以下のコマンドを使う。
git checkout ブランチ名
get branchでtopicブランチに移動したことがわかる。
|
1 2 3 4 5 |
[vagrant@localhost branch]$ git checkout topic Switched to branch 'topic' [vagrant@localhost branch]$ git branch main * topic |
topicブランチでログを見ると、mainの内容が引き継がれていることがわかる。
|
1 2 3 4 5 6 |
[vagrant@localhost branch]$ git log commit ad931b08026b6a1ba68350176ae892bcb115a23e (HEAD -> topic, origin/main, origin/HEAD, main) Author: taustation <88479749+taustation@users.noreply.github.com> Date: Sun Aug 8 22:40:29 2021 +0900 Initial commit |
ブランチ内での変更と独立性
ブランチ内での変更
現在いるtopicブランチで、topic.txtファイルを新規作成してコミットする。
|
1 2 3 4 5 6 7 8 |
[vagrant@localhost branch]$ vi topic.txt [vagrant@localhost branch]$ cat topic.txt topicブランチで新規作成 [vagrant@localhost branch]$ git add . [vagrant@localhost branch]$ git commit -m "topic-1" [topic e3e9099] topic-1 1 file changed, 1 insertion(+) create mode 100644 topic.txt |
ログを確認。
|
1 2 3 4 5 6 7 8 9 10 11 12 |
[vagrant@localhost branch]$ git log commit e3e90995d709c3882771856815791ffa23d920c2 (HEAD -> topic) Author: taustation <taustation@gmail.com> Date: Tue Aug 10 08:36:41 2021 +0900 topic-1 commit ad931b08026b6a1ba68350176ae892bcb115a23e (origin/main, origin/HEAD, main) Author: taustation <88479749+taustation@users.noreply.github.com> Date: Sun Aug 8 22:40:29 2021 +0900 Initial commit |
作成したファイルに、さらに変更を加えてコミット。
|
1 2 3 4 5 6 7 8 |
[vagrant@localhost branch]$ vi topic.txt [vagrant@localhost branch]$ cat topic.txt topicブランチで新規作成 topicブランチで内容を更新 [vagrant@localhost branch]$ git add . [vagrant@localhost branch]$ git commit -m "topic-2" [topic 8ec3439] topic-2 1 file changed, 1 insertion(+) |
ログを確認すると、2つのコミットが反映されている。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[vagrant@localhost branch]$ git log commit 8ec3439f5e00b14902662d89b2c7314c8423a4da (HEAD -> topic) Author: taustation <taustation@gmail.com> Date: Tue Aug 10 08:37:55 2021 +0900 topic-2 commit e3e90995d709c3882771856815791ffa23d920c2 Author: taustation <taustation@gmail.com> Date: Tue Aug 10 08:36:41 2021 +0900 topic-1 commit ad931b08026b6a1ba68350176ae892bcb115a23e (origin/main, origin/HEAD, main) Author: taustation <88479749+taustation@users.noreply.github.com> Date: Sun Aug 8 22:40:29 2021 +0900 Initial commit |
topic.txtの内容も2つのコミットの内容が反映されている。
|
1 2 3 |
[vagrant@localhost branch]$ cat topic.txt topicブランチで新規作成 topicブランチで内容を更新 |
ブランチの変更の独立性
ここでmainブランチに戻ってみると、まだtopicブランチで作成・更新したtopic.txtは反映されていない。
|
1 2 3 4 5 |
[vagrant@localhost branch]$ git checkout main Switched to branch 'main' Your branch is up to date with 'origin/main'. [vagrant@localhost branch]$ ls README.md |
mainブランチのログも初期状態のまま。
|
1 2 3 4 5 6 |
[vagrant@localhost branch]$ git log commit ad931b08026b6a1ba68350176ae892bcb115a23e (HEAD -> main, origin/main, origin/HEAD) Author: taustation <88479749+taustation@users.noreply.github.com> Date: Sun Aug 8 22:40:29 2021 +0900 Initial commit |
ここまでの状態は以下のようになる。mainブランチの状態は変わらず、topicブランチでコミットされてHEADが移っている。
|
1 2 3 4 5 |
initial commit <main> ○ HEAD <topic> ○──────△────△ HEAD initial commit topic-1 topic-2 |
mainブランチでのマージ
以下のコマンドは、現在いるブランチに指定したブランチの作業内容をマージする。
git merge ブランチ名
mainブランチにいる状態で、topicブランチでのコミットをmainブランチにマージする。
|
1 2 3 4 5 6 |
[vagrant@localhost branch]$ git merge topic Updating ad931b0..243b877 Fast-forward topic.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 topic.txt |
1つのファイルが更新され(新規作成を含む)、挿入が2件あることが示されている。
mainブランチでtopic.txtを見てみると、topicブランチでの新規作成・更新の作業が反映されている。
|
1 2 3 4 5 |
[vagrant@localhost branch]$ ls README.md topic.txt [vagrant@localhost branch]$ cat topic.txt topicブランチで新規作成 topicブランチで内容を更新 |
ログは以下の通り。なお、この内容はtopicブランチで確認しても全く同じになっている。
topicブランチでの作業がそのままmainブランチに適用されているHEADはmain、topicの両方のブランチの最後尾を指している
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[vagrant@localhost branch]$ git log commit 8ec3439f5e00b14902662d89b2c7314c8423a4da (HEAD -> main, topic) Author: taustation <taustation@gmail.com> Date: Tue Aug 10 08:37:55 2021 +0900 topic-2 commit e3e90995d709c3882771856815791ffa23d920c2 Author: taustation <taustation@gmail.com> Date: Tue Aug 10 08:36:41 2021 +0900 topic-1 commit ad931b08026b6a1ba68350176ae892bcb115a23e (origin/main, origin/HEAD) Author: taustation <88479749+taustation@users.noreply.github.com> Date: Sun Aug 8 22:40:29 2021 +0900 Initial commit |
ここまでの状態は以下のようになる。mainブランチにtopicブランチのコミットが吸収されて、HEADの位置が最後尾に移っている。
|
1 2 3 4 5 |
initial commit <main> ○ │ <topic> └──────△────△ HEAD initial commit topic-1 topic-2 |
fast-forward
上記のマージでは、topicブランチでの作業結果がそのままmainブランチに取り込まれ、HEADがその最後尾に進んでいる。
このようなマージをfast-forward (早送り)と呼ぶ。
マージの取り消し
ここで、mainでマージしたtopicの内容を取り消すことを考える。
取り消しできないコマンド例
git merge --abortは競合が発生したときのみ機能するが、今回は競合が発生していないので取り消しできない。
|
1 2 |
[vagrant@localhost branch]$ git merge --abort fatal: There is no merge to abort (MERGE_HEAD missing). |
get reset --hard HEADは、マージが競合なく進んでHEADがマージ後の最新の位置に移動しているのでマージを取り消しできない。
|
1 2 |
[vagrant@localhost branch]$ git reset --hard HEAD HEAD is now at 8ec3439 topic-2 |
コミット番号を指定したマージの取り消し
git reset --hard コミット番号で1つ前のtopic-1のコミット番号を指定すると、そのtopic-1の適用直後まで戻すことができる。作業結果も巻き戻されている。
|
1 2 3 4 |
[vagrant@localhost branch]$ git reset --hard e3e90995d709c3882771856815791ffa23d920c2 HEAD is now at e3e9099 topic-1 [vagrant@localhost branch]$ cat topic.txt topicブランチで新規作成 |
ログの確認。HEADが1つ前のtopic-1の終了時点となっている。
|
1 2 3 4 5 6 7 8 9 10 11 12 |
[vagrant@localhost branch]$ git log commit e3e90995d709c3882771856815791ffa23d920c2 (HEAD -> main) Author: taustation <taustation@gmail.com> Date: Tue Aug 10 08:36:41 2021 +0900 topic-1 commit ad931b08026b6a1ba68350176ae892bcb115a23e (origin/main, origin/HEAD) Author: taustation <88479749+taustation@users.noreply.github.com> Date: Sun Aug 8 22:40:29 2021 +0900 Initial commit |
ここでtopicブランチに移ってログを確認すると、その内容は変わっておらず2つのコミット結果が残っている。
ここでの状態は以下のようになる。マージされた1つのコミットが2つに分かれている。
|
1 2 3 4 5 |
initial commit topic-1 <main> ○──────△ HEAD <topic> ○──────△────△ HEAD initial commit topic-1 topic-2 |
さらにmainブランチで、イニシャルコミットの直後までリセットする。ファイルの作成前まで巻き戻されている。
|
1 2 3 4 |
[vagrant@localhost branch]$ git reset --hard origin/main HEAD is now at ad931b0 Initial commit [vagrant@localhost branch]$ ls README.md |
ログもマージ前に戻る。
|
1 2 3 4 5 6 |
[vagrant@localhost branch]$ git log commit ad931b08026b6a1ba68350176ae892bcb115a23e (HEAD -> main, origin/main, origin/HEAD) Author: taustation <88479749+taustation@users.noreply.github.com> Date: Sun Aug 8 22:40:29 2021 +0900 Initial commit |
ここでもtopicブランチでのコミット結果は変化しておらず、状態は以下のようになる。
|
1 2 3 4 5 |
initial commit <main> ○ HEAD <topic> ○──────△────△ HEAD initial commit topic-1 topic-2 |
再マージ
topicブランチのマージを再度実行。この場合はtopicブランチでの2回のコミット結果が一挙に反映される。
|
1 2 3 4 5 6 7 8 9 10 11 12 |
[vagrant@localhost branch]$ git branch * main topic [vagrant@localhost branch]$ git merge topic Updating ad931b0..8ec3439 Fast-forward topic.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 topic.txt [vagrant@localhost branch]$ cat topic.txt topicブランチで新規作成 topicブランチで内容を更新 |
mainブランチのログにも2度のコミットの記録が残る。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[vagrant@localhost branch]$ git log commit 8ec3439f5e00b14902662d89b2c7314c8423a4da (HEAD -> main, topic) Author: taustation <taustation@gmail.com> Date: Tue Aug 10 08:37:55 2021 +0900 topic-2 commit e3e90995d709c3882771856815791ffa23d920c2 Author: taustation <taustation@gmail.com> Date: Tue Aug 10 08:36:41 2021 +0900 topic-1 commit ad931b08026b6a1ba68350176ae892bcb115a23e (origin/main, origin/HEAD) Author: taustation <88479749+taustation@users.noreply.github.com> Date: Sun Aug 8 22:40:29 2021 +0900 Initial commit |
以下の状態に戻る。
|
1 2 3 4 5 |
initial commit <main> ○ │ <topic> └──────△────△ HEAD initial commit topic-1 topic-2 |
ブランチの削除
ブランチでの作業履歴が不要になった場合は、以下のコマンドでブランチを削除できる。
git branch --delete ブランチ名
git branch -d ブランチ名
|
1 2 3 4 |
[vagrant@localhost branch]$ git branch --delete topic Deleted branch topic (was 8ec3439). [vagrant@localhost branch]$ git branch * main |
|
1 2 |
initial commit topic-1 topic-2 <main> ○──────△────△ HEAD |