Git:如何区分两个不同的文件在不同的分支?

我有两个不同的文件在不同的分支。 我如何在一个命令中区分它们?< / p >

类似的

# git diff branch1/foo.txt branch2/foo-another.txt

我可以检查其他文件,区别它和恢复,但这是相当肮脏的解决方案。

53550 次浏览
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:file branch2:file
    

    < em >例子:< / em >

     git diff branch1:full/path/to/foo.txt branch2:full/path/to/foo-another.txt
    
  • Only if the name is the same and you want to compare your current working directory to some branch:

    git diff ..someBranch path/to/file
    

    < em >例子:< / em >

    git diff ..branch2 full/path/to/foo.txt
    
    在本例中,您将比较来自实际分支的文件 主分支中的文件 李< /引用> < / >

你可以检查这个响应:

比较Git中两个不同分支的文件

跑题答案——在不同的分支中区分相同的文件

只是添加它,因为我发现它是一个非常简单的语法:

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=somebranch
branch2=someotherbranch
git diff ${branch1}..${branch2} -- file_path