1
3

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 5 years have passed since last update.

git rebase ontoを使う

Last updated at Posted at 2018-04-26

はじめに

rebaseを使いこなせていなくて、余分な手作業をしていたので備忘録。

1. 基本的なrebase

(1) 状態

materからbranchAとbranchBを作成。各ブランチでコーディングを進めている。

master - commit1
        |__ branchA - commit2
        |__ branchB - commit3

(2) リベースコマンド

branchAがmasterにリベースされた後、branchBをmasterにリベースする。
$ git rebase master branchB

(3) リベース前後のコミット履歴

※太字がbranchBの持つコミット履歴

リベース前

commit1 - commit3

リベース後

commit1 - commit2 - commit3

2. ontoを用いるrebase

(1) 状態

masterからbranchAを作成。commit2を作成した後に、branchAからbranchBを作成。

master - commit1
        |__ branchA - commit2 - commit3
                     |__ branchB - commit4

(2) リベースコマンド

branchAがmasterにリベースされた後、--onto optionでbranchBをmasterにリベースする。
$ git rebase --onto master branchA branchB

(3) リベース前後のコミット履歴

※太字がbranchBの持つコミット履歴

リベース前

commit1 - commit2 - commit4

リベース後

commit1 - commit2 - commit3 - commit4

※このケースで$ git rebase master branchBとしてしまうと、リベース後のコミット履歴が、このようなおかしなことになってしまい、不毛なコンフリクト解消手作業が発生してしまう。
commit1 - commit2 - commit3 - commit2 - commit4

補足

・ --onto optionは、git reset --hardと同じ効果(コミット履歴がリセットされる)がある。利用するメリットとしては、余分なコミット履歴を出させないことで、プルリクエストを出す時に履歴が追いやすいことにあるかと思います。
・ 各ブランチを最新の状態にするのを忘れずに・・・。

参照

Git - git-rebase Documentation

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?