uoz 作業日記

様々な作業の記録を共有するブログです。

いつも止まるgit rebase が止まったときの手順が分かってきた

git rebaseはマージみたいなもんだけど、対象のブランチに一回いってから、それより後のコミットを順に適用していくというもの。

ローカルで作業してて、リモートにブランチをあげてpull requestだしたら、コンフリクトが起きたとき、
統合したい先のブランチをベースにするように、ローカルのブランチをリベースしてやるといい。

でも、このリベースをすると、当然手元でコンフリクトが起こっていちいち止まる。

出てくるメッセージは英語だから、最初何をしたらいいかわかんなかった。
でもわかった来たのでメモする。

rebaseのしかた

git checkout 作業したブランチ名
git rebase ベースにしたいブランチ名

上のように打つだけ。

下のようにコミットの適用がすすんでいって...

git rebase develop
First, rewinding head to replay your work on top of it...
Applying: コミットメッセージ1
Applying: コミットメッセージ2
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
error: エラーメッセージ

なんか止まる。

よくでてくるエラーメッセージ

1 "Your local changes to the following files would be overwritten by merge:"

変更があるのにコミットされてない!上書きされるぞ!というような意味。
ちゃんとrebase前にコミットしていても、リベース先で変更の合ったファイルをこちらで変更を追跡しない設定にしてたりgit管理外にしてるとでてくることがある。

全体はこんな感じ。

error: Your local changes to the following files would be overwritten by merge:
        tekitounadir/hogehoge.rb
Please, commit your changes or stash them before you can merge.
Aborting
Failed to merge in the changes.
Patch failed at 0002 コミットメッセージ2
The copy of the patch that failed is found in:
   xxxxx/.git/rebase-apply/patch
対処
git add .
git commit -m "hogehoge"

ほんとにコミットしていい変更かは自分で考えてくれ。

そのあと、

git rebase --continue

途中で止まったリベースを再開する

2 "CONFLICT (content): Merge conflict in hogehoge/hugahuga.rb"

めちゃくちゃよく出る。
マージの時のコンフリクトと同じ。
リベース元のブランチの変更が、手元でのコミットとぶつかった。

全体

Auto-merging  hogehoge/hugahuga.rb
CONFLICT (content): Merge conflict in hogehoge/hugahuga.rb
Failed to merge in the changes.
Patch failed at 0003 コミットメッセージ
The copy of the patch that failed is found in:
   xxxxx/.git/rebase-apply/patch

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

対処

CONFLICTって言われたファイルを開いて、衝突してるところを修正する。

ファイルをひらくと、下みたいになってる

<<<<<<< HEAD
*リベース元のブランチでの変更
=======
*手元のブランチでの変更
>>>>>>>コミットメッセージ

「<<<<<<<」から「=======」が向こう側(リベース元)の変更、「=======」と「>>>>>>>」の間がこっち側の変更、この2つがぶつかってる。

残したい部分だけ残してファイルを保存したのち、git addで変更を反映。

git add .

最後に

git rebase --continue

3 "No changes - did you forget to use 'git add'?"

たまにしか出ない

rebaseが途中で止まっていろいろやってるうちに、当該のコミットの変更が意味なくなった時に出るんじゃないかと思っている。

全体

No changes - did you forget to use 'git add'?
If there is nothing left to stage, chances are that something else
already introduced the same changes; you might want to skip this patch.

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

意味ないらしいからこのコミットは飛ばす。

以上です。


追記

  1. rebase したらブランチのツリーが結構変わるので、リモートにpushしてた場合は一回リモートを消した方がいい。
  2. ということは、他人がそこからブランチを切るような状況では、そもそもそのブランチをリモートにpushしてはいけない。

おすすめテキスト

本はなくてもいいけど、あったら便利。
ディスプレイを占拠しないので。