diff文件的当前工作副本与另一个分支的已提交副本

Dog*_*ert 135 git

foo在master分支中有一个repo文件.我切换到酒吧分支并进行了一些更改foo.我现在如何git diff在此副本(尚未提交)和主分支副本之间运行?

Mar*_*air 127

以下适用于我:

git diff master:foo foo

在过去,它可能是:

git diff foo master:foo

  • 对我来说恰恰相反 - 我可以做`git diff master:foo foo`但反之亦然.我也不明白.使用git 1.7.9.5/Ubuntu 12.04,我至少可以使用`git diff -R master:foo foo`来获取我真正想要的差异.当我尝试使用msysgit 1.9.4/Windows 7 x64时,我感到"致命:无法读取0000000000000000000000000000000000000000".如果没有`-R`,我会得到与git 1.7.9.5相同的错误消息,但是使用1.9.4我会致命:master:foo:工作树中没有这样的路径. (8认同)
  • 每当我看到你的关于`git diff`的答案时,我总会想到http://stackoverflow.com/questions/5256249/git-diff-doesnt-show-enough/5257065#5257065 (7认同)
  • 您是否尝试使用`--`来将参数与路径参数分开?`git diff - master:foo foo` (2认同)

Jor*_*ugh 97

您正在尝试将工作树与特定分支名称进行比较,因此您需要:

git diff master -- foo
Run Code Online (Sandbox Code Playgroud)

这是从这种形式的git-diff(参见git-diff手册页)

   git diff [--options] <commit> [--] [<path>...]
       This form is to view the changes you have in your working tree
       relative to the named <commit>. You can use HEAD to compare it with
       the latest commit, or a branch name to compare with the tip of a
       different branch.
Run Code Online (Sandbox Code Playgroud)

仅供参考,还有一个--cached(aka --staged)选项可用于查看您上演的内容的差异,而不是工作树中的所有内容:

   git diff [--options] --cached [<commit>] [--] [<path>...]
       This form is to view the changes you staged for the next commit
       relative to the named <commit>.
       ...
       --staged is a synonym of --cached.
Run Code Online (Sandbox Code Playgroud)

  • 谢谢。不知道为什么,但是只有这个答案对Linux中的git 1.8.1有用。 (2认同)

Art*_*BIT 14

也: git diff master..feature foo

因为git diff foo master:foo对我来说不适用于目录.


Adi*_*dir 10

git difftool tag/branch filename
Run Code Online (Sandbox Code Playgroud)


小智 10

还有什么作用:

git diff master ./relative-path-to-foo
Run Code Online (Sandbox Code Playgroud)


Nao*_*den 9

git diff mybranch master -- file
Run Code Online (Sandbox Code Playgroud)

也应该工作

  • 这仅适用于提交到mybranch的文件,而不适用于文件的当前工作副本. (3认同)

kta*_*kta 6

查看与当前分支相比的本地更改

git diff .
Run Code Online (Sandbox Code Playgroud)

与任何其他现有分支相比,查看本地更改

git diff <branch-name> .
Run Code Online (Sandbox Code Playgroud)

查看特定文件的更改

git diff <branch-name> -- <file-path>
Run Code Online (Sandbox Code Playgroud)

确保git fetch一开始就跑。