0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

画像ファイルの変更を含むPRを安全にrevertするTips

Last updated at Posted at 2025-05-01

はじめに

画像ファイルを含んだPull RequestをSquashマージしたあと、「やっぱり元に戻したい」ということがありました。

GitHubのRevertボタンを押せば済むと思っていたのですが、SquashマージだとRevertボタンが出ないようです。

代わりに git revert <マージコミット> コマンドを使ってみたところ、画像に対してコンフリクトが発生。

# エラーはこんな感じ
## バイナリファイルはマージできない警告
## autoマージを試みたがコンフリクトしたとのこと
warning: Cannot merge binary files: [画像ファイルのパス] (HEAD vs. parent of [コミットID] ([PRのタイトル]))
Auto-merging [画像ファイルのパス]
CONFLICT (content): Merge conflict in [画像ファイルのパス]

revertができない...結局エディタを開いて手動で戻すしかないのかと思ったのですが、git checkout コマンドで無事戻せたので備忘録を残します。


要点

  • SquashマージされたPRにはRevertボタンが表示されない
  • Gitはテキストファイルの差分は自動マージできるが、PNGなどバイナリファイルは差分を理解できないため、マージやrevertの際に競合扱いになるらしい
  • 画像中心のPRなら「元のファイルをcheckoutしてPRを作る」方が安全で確実だった

手順

① revert用でブランチきる

git checkout -b revert-icon

② 過去のマージコミットを参照して画像を過去の状態に戻す

以下のコマンドで特定のマージコミットの特定のファイルを選んで現在のブランチに上書きすることができる

git checkout <過去のマージコミット>^ -- path/to/ic_launcher.png
git checkout <過去のマージコミット>^ -- path/to/ic_foreground.png

差分を確認してコミット

git status
git add .
git commit -m "Revert icon image"
git push origin revert-icon

GitHub上で新しいPRを作成してマージ
これで無事、画像の変更を元に戻すことができました。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?