チュートリアル1 ブランチを使ってみよう! 7. rebaseでマージする

issue3ブランチをマージするとき、issue3ブランチをあらかじめrebaseしていれば履歴を一本にすることもできました。

一旦、さきほどのマージを取り消します。

$ git reset --hard HEAD~

issue3ブランチをチェックアウトしてから、masterに対してrebaseを実行します。

$ git checkout issue3
Switched to branch 'issue3'
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: pullの説明を追加
Using index info to reconstruct a base tree...
<stdin>:13: new blank line at EOF.
+
warning: 1 line adds whitespace errors.
Falling back to patching base and 3-way merge...
Auto-merging myfile.txt
CONFLICT (content): Merge conflict in myfile.txt
Failed to merge in the changes.
Patch failed at 0001 pullの説明を追加

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To check out the original branch and stop rebasing run "git rebase --abort".

mergeの時と同じように、myfile.txtで競合が発生するので修正しましょう。

サル先生のGitコマンド
add 変更をインデックスに登録する
<<<<<<< HEAD
commit インデックスの状態を記録する
=======
pull リモートリポジトリの内容を取得する
>>>>>>> issue3

rebaseの場合、競合箇所を修正した後はコミットではなく、rebaseコマンドに --continue オプションを指定して実行します。もし、rebase自体を取り消す場合は --abort オプションを指定します。

$ git add myfile.txt
$ git rebase --continue
Applying: pullの説明を追加

これで、masterブランチはissue3ブランチをfast-forwardマージできるようになりました。masterブランチをチェックアウトしてからマージを実行してみましょう。

$ git checkout master
Switched to branch 'master'
$ git merge issue3
Updating 8f7aa27..96a0ff0
Fast-forward
myfile.txt | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)

myfile.txtの最終的な内容はmergeと同じですが、履歴はこのようになります。