LoginSignup
3
2

More than 3 years have passed since last update.

git revertでコンフリクトした記録

Posted at

エラー

以下のようなエラーが出ました。

revert.sh

$ git revert HEAD~4
error: could not revert 54ea783... Adjustment plugin
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

status.sh

 $git status
On branch feature
You are currently reverting commit 54ea783.
  (fix conflicts and run "git revert --continue")
  (use "git revert --abort" to cancel the revert operation)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   example.php

Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add/rm <file>..." as appropriate to mark resolution)

Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add/rm <file>..." as appropriate to mark resolution)

        deleted by them: example2.php
        both modified:   example3.css
        both modified:   example4.css

状況

featureブランチを進めていて
ローカルでadd → commit
リモートへ向けてプッシュ
github上でマスターにマージして
ローカルでrevertしようとしたら前述のエラーが出ました。

最初心当たりがなく、コンフリクトしている状況というのも?という状態でしたが
よくよく確認してみるとテスト用に作成したtestブランチが残っており
(git branch -D testしたのですがなぜか消えていなかった)
featureで編集していたファイルと被っていて
testブランチはマージされていない状態っぽいことがわかりました。

コンフリクト解消

特にphpstorm上でgitの設定をした覚えはないのですが
example.php
example2.php
example3.css
example4.css
この4つのファイルがphpstorm上で赤く表記されており
以下のように変更内容のどちらを残すかという形になっておりました。
phpstormに助けられました。IDE,エディタの設定や選定も重要ですね。

example.php

<<<<<<< HEAD
# featureブランチで行った変更内容
echo "feature";
=======
# testブランチで行った変更内容
echo "test";
>>>>>>> develop

今回は以下のようにfeatureブランチの内容を残して上書き保存しました。

example.php

echo "feature";

参考にさせていただきました

上記ページに従ってともかくコンフリクト解消したものを
add → commit → push

してみるとエラー自体が解消されたので
revertする必要もなくなりました。

原因として予想している事

今回消したはずのtestブランチが残っていて
しかもマージされていない状態があったこと。
featureは独自にコミットを進めてしまっていて
リモートプッシュ、リモートでのプルリクマージまで進んでしまったこと。
これらの状況があってrevertする際に
初めてコンフリクトが起こってしまったのではないかと予想しています。

コンフリクトを再現してみました

以下の画像のように2つのブランチが走っていて
1.png
枝分かれ部分のコミットをrevertしようとしたところ
前述したコンフリクトが再現できました。

againconflict.sh

$git revert bfa14cd3f7a3c525b
error: could not revert bfa14cd... Added test1230
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

ただコンフリクトしているファイルtest.htmlを開いてみても
<<<<<<<<<< HEADなどの表記はありませんでした。

よく確認してみるとこの時点でtest1230.htmlが生成されただけの状態で
featureブランチ、testブランチ間での差異はない状態でした。

add → commitでrevertコミットが生成されました。
2.png

コンフリクト再現2

ファイルの差分がある状態を再現できれば
最初のコンフリクトと同じ状態を作れると予想し以下を実行しました。

conflict.sh

$git revert 8c01b7f7cee4393df596758
error: could not revert 8c01b7f... added text tesst1230.html
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

以下はその時のコミットログです。
3.png

test1230.htmlを開くと

test1230.html

test1230
<<<<<<< HEAD

added from feature branch

added from feature branch2
=======
>>>>>>> parent of 8c01b7f... added text tesst1230.html

となっており

test1230.html

test1230

added from feature branch

added from feature branch2

として保存しadd → commit したら
無事revertコミットが作成されました。
スクリーンショット 2020-04-14 13.46.50.png

教訓

コミットを逆にたどるような場合もコンフリクトが発生する。
いらないブランチはすぐに消しておく方が良さそう。

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