異なるブランチに2つの異なるファイルがあります。1つのコマンドでそれらを比較するにはどうすればよいですか?
何かのようなもの
# git diff branch1/foo.txt branch2/foo-another.txt
他のファイルをチェックアウトして差分を取り、復元することもできますが、それはかなり汚い解決策です。
git diff branch1:full/path/to/foo.txt branch2:full/path/to/foo-another.txt
相対パスを使用することもできます。
git diff branch1:./relative/path/to/foo.txt branch2:./relative/path/to/foo-another.txt
./
補足:フルパスは必要ありません。相対パスから始めることができます。時々便利です。
git diff branch1:./relative/path/to/foo.txt branch2:./relative/path/to/foo-another.txt
トピック外の回答-コメントを参照
追加するだけで、非常に単純な構文であることがわかります。
git diff <branch1> <branch2> <filepath>
たとえば、次のような相対参照でも機能します。
# compare the previous committed state from HEAD with the state branch1 was 3 commits ago
git diff HEAD^ <branch1>~3 <filepath>
2つの異なるブランチからのファイルを比較する方法はたくさんあります。例えば:
名前が同じまたは異なる場合:
git diff branch1:file branch2:file
例:
git diff branch1:full/path/to/foo.txt branch2:full/path/to/foo-another.txt
名前が同じで、現在の作業ディレクトリをいくつかのブランチと比較する場合のみ:
git diff ..someBranch path/to/file
例:
git diff ..branch2 full/path/to/foo.txt
この例では、実際のブランチのファイルをマスターブランチのファイルと比較しています。
この応答を確認できます。
適用する開始と範囲を指定できますgit diff
。範囲は..
表記で示されます。
branch1=somebranch
branch2=someotherbranch
git diff ${branch1}..${branch2} -- file_path