我在不同的分支中有两个不同的文件。如何在一个命令中区分它们?
就像是
# 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>
有很多方法可以比较来自两个不同分支的文件。例如:
如果名称相同或不同:
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