nirasan's tech blog

趣味や仕事の覚え書きです。Linux, Perl, PHP, Ruby, Javascript, Android, Cocos2d-x, Unity などに興味があります。

git rebase でコンフリクトしたら

コンフリクト発生

  • するとこんな感じになります
% git rebase master
First, rewinding head to replay your work on top of it...
Applying: edit 1.txt at topic
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging 1.txt
CONFLICT (content): Merge conflict in 1.txt
Failed to merge in the changes.
Patch failed at 0001 edit 1.txt at topic

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore the original branch and stop rebasing run "git rebase --abort".
  • status はこんな感じ
% git status
# Not currently on any branch.
# Unmerged paths:
#   (use "git reset HEAD <file>..." to unstage)
#   (use "git add/rm <file>..." as appropriate to mark resolution)
#
#	both modified:      1.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
  • 1.txt がコンフリクトしていますね

コンフリクト解消

  • diff を取るとコンフリクトしている場所がわかります
% git diff
diff --cc 1.txt
index 0cfbf08,d00491f..0000000
--- a/1.txt
+++ b/1.txt
@@@ -1,1 -1,1 +1,5 @@@
++<<<<<<< HEAD
 +2
++=======
+ 1
++>>>>>>> edit 1.txt at topic
  • '<<<<<<<' と '>>>>>>>' の間をいいかんじに調整してコンフリクトを解消しましょう

次に進む

  • コンフリクトが解消できたら次のコミットのrebaseに進みます
% git add 1.txt
% git rebase --continue
  • またコンフリクトが発生したら同様に `edit file; git add file; git rebase --continue;` で対応しましょう

勢いで `git add file; git commit file;` してしまったら

  • `git rebase --abort` すれば `git rebase master` 実行前に巻き戻せます
  • `git rebase --skip` すればコンフリクトしたコミットを調整したコミットで上書きして次に進むことができます