1
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 rebaseとmerge

Posted at

はじめに

git rebaseとmergeについてまとめてみた

git rebase、mergeとは

  • 指定したブランチの最新コミットに追従させる
  • featureブランチでmainブランチの最新を取り込むときなどに利用するコマンド

git rebase

featureブランチに最新のmainを取り込む

rebase前

A---B---C       <-- main
     \
      D---E     <-- feature

rebase後

コミットCを起点として、D'、E'がfeatureブランチに適用される

A---B---C        <-- main
         \
          D'--E' <-- feature

git merge

featureブランチに最新のmainを取り込む

merge前

A---B---C       <-- main
     \
      D---E     <-- feature

merge後

新しいいコミットMが作成される

A---B---C        <-- main
     \    
      D---E---M  <-- feature

rebaseとmergeの違い

https://www.wantedly.com/companies/progrit/post_articles/412697
https://qiita.com/miriwo/items/0a3a6444abbeb48f0fe7

どのようにrebaseされるか

  1. リベース元のぶらっBTのHEADを無名bランチとして作成し、チェックアウト
  2. 無名ブランチにrebase先ブランチのコミットをパッチとして適用
  3. もしコンフリクトなどが発生した場合はリベースは中断され、無名ブランチをチェックアウトしている状態になる
  4. すべてのコミットが適用できるかリベースが完了したとき、無名ブランチをリベース先ブランチに切り替える

rebase時のコンフリクト

a. コンフリクトを修正してコミットする(--continue)
b. コンフリクトを修正せずに中止して元の状態に戻す(--abort)

コンフリクトの修正はマージと同じ。正しい状態を選択して適用していく。

コマンド

# 最新を取り込むブランチを最新化
git pull main

# リベース対象に移動
git checkout feature/xxxxx

# リベース
git rebase main feature/xxxxx

# コンフリクトになった場合、コンフリクトを解消しでadd
# commitはしない
git add .

# 修正してコミット
git rebase --continuea
# コンフリクトになる、解消を繰り返していく
# コンフリクトにならなければsuccessになる

# successになったら
# feature/xxxxブランチのコミット履歴をrebaseする
git rebase -i HEAD~11

# 中断する場合
git rebase --abort

結論

ちょっと自分が実際に体験したものではないので、なんとも言い難いが、調べる限り、rebaseをしたほうが履歴が見やすくなりそう。rebaseを使うほうがよさそうだ。

1
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
1
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?