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

时间:2012-02-02 13:36:30

标签: git

我在主分支中有一个文件foo的仓库。我切换到了bar分支并对foo进行了一些更改。我现在如何在此副本(尚未提交)和主分支副本之间运行git diff

8 个答案:

答案 0 :(得分:119)

以下适用于我:

git diff master:foo foo

过去,它可能是:

git diff foo master:foo

答案 1 :(得分:86)

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

git diff master -- foo

这是来自这种形式的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.

仅供参考,还有--cached(又名--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.

答案 2 :(得分:14)

另外:git diff master..feature foo

由于git diff foo master:foo对我来说不适用于目录。

答案 3 :(得分:9)

git difftool -v tag/branch filename

答案 4 :(得分:8)

git diff mybranch master -- file

也应该有用

答案 5 :(得分:3)

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

git diff .

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

git diff <branch-name> .

查看特定文件的更改

git diff <branch-name> -- <file-path>

确保您首先运行git fetch

答案 6 :(得分:1)

什么也有效:

git diff master ./relative-path-to-foo

答案 7 :(得分:0)

假设您有一个名为 master 的分支和一个名为 feature 的分支,并且您想要检查名为 my_file 的特定文件,然后:

  1. git diff master..feature /Path/to/my_file 显示分支 my_filemasterfeature 的提交版本之间的差异。
  2. git diff master -- /Path/to/my_file 显示了工作目录(未暂存的文件)和 master 的分支 my_file 之间的区别。