[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Low cost] Wasabi object storage construction and operation service

[Low cost] Wasabi object storage construction and operation service

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “beSIM”

[Compatible with over 200 countries] Global eSIM “beSIM”

[Compatible with Chinese corporations] Chinese cloud / server construction, operation and maintenance

[Compatible with Chinese corporations] Chinese cloud / server construction, operation and maintenance

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

git diff で作ったパッチはどうやって当てるのか(そもそも git でパッチ

git

こんにちは。
開発チームのワイルド担当、まんだいです。

git には「パッチ」という仕組みがあります。
git stash でうまく切り抜けられない時にたまに使うことがあるにも関わらず、この超絶簡単なコマンドが覚えられません。

なので、忘れてもいいようにブログに残すことにしました。

 

パッチを作成する時

パッチを作成する場合は、 git diff コマンドを使用します。

git diff test.txt > test.patch

 

パッチを作りたいだけ作ったら、後は git reset で元通りですね。
パッチファイルさえあれば、この時点の内容にすることが可能です。

ちなみに diff を取るのは、デフォルトで現状のファイルとそのファイルのインデックスの状態になります。
コミット ID を指定して数世代前から diff を取ることもできます。

使ったことはないですが、コミット ID を2つ指定すると、コミット ID 間の diff を取ることもできるようです。

 

パッチを適用する時

では次は、パッチの内容を反映させてみます。

git apply test.patch

 

これだけです。
ただし、 git apply する時点のファイル内容が、 git diff した時のものと違う場合、適用されません。
こういう場合は、 git diff でチラッと紹介した、コミット ID を指定して diff を取る方法が使えます。

ふと、 patch --dry-run と同じことをする場合、どうしたらいいんだろうか、と思って調べると、 --check オプションというのがそれに当たるようです。

 

まとめ

git を使ったパッチ作成、適用の手順をまとめました。

git diff でコミット ID 同士でパッチを作成できるのは、今回初めて知りましたが git ならではの機能でいつかサラッと使って得意げな顔をしてみたいものです。

git apply の --check オプションについて少し調べる機会ができたのも今回大きな収穫でした。
コードレビューする時の事前確認としても使えそうですね。

 
以上です。

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
1
読み込み中...
1 票, 平均: 1.00 / 11
61,829
X facebook はてなブックマーク pocket
[2024.6.30 CentOS support ended] CentOS server migration solution

[2024.6.30 CentOS support ended] CentOS server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

Yoichi Bandai

My main job is developing web APIs for social games, but I'm also fortunate to be able to do a lot of other work, including marketing.
Furthermore, my portrait rights in Beyond are treated as CC0 by him.