1
1
お題は不問!Qiita Engineer Festa 2024で記事投稿!
Qiita Engineer Festa20242024年7月17日まで開催中!

Gitで別ブランチの変更を1ファイルだけ持ってくる

Last updated at Posted at 2024-07-03

はじめに

gitで別ブランチの変更をコミット単位で持ってこれる、cherry-pickコマンドはありますが、その変更の全てがほしいわけではなく、一部ファイルの変更だけが欲しい場面がたまにあります。

そういうときに使えるコマンドがあったので、紹介します。

git checkout <欲しい変更があるブランチ> -- <欲しい変更があるファイルの相対パス>

たとえば、featureブランチにある特定ファイルの変更が欲しい場合は下記のようになります。

git checkout feature -- hoge/fuga/main.go

ディレクトリ単位で持ってくることもできます。

git checkout feature -- hoge

追記

restoreコマンドでも同じことができるようです。
むしろ、こちらのほうが推奨かも。

git restore -s feature hoge/fuga/main.go
git restore -s feature hoge

参考

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