我有一个回购与文件foo在主分支。我切换到bar分支,并对foo做了一些更改。我现在如何在这个副本(尚未提交)和主分支的副本之间运行git差异?


当前回答

git difftool tag/branch filename

其他回答

git diff mybranch master -- file

应该也有用

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

git diff .

查看与任何其他现有分支相比较的局部更改

git diff <branch-name> .

查看特定文件的更改

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

确保在开始时运行git fetch。

以下是我的工作:

Git diff master:foo foo

在过去,它可能是:

Git diff foo master:foo

你试图比较你的工作树和一个特定的分支名称,所以你想要这样:

git diff master -- foo

它来自于git-diff的这种形式(参见git-diff manpage)

   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 (aka - staging)选项用于查看你已经staging的东西的差异,而不是你工作树中的所有东西:

   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.

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

git diff master..feature /Path/to/my_file显示my_file在master分支和feature分支上的提交版本之间的差异。 git diff master——/Path/to/my_file显示了工作目录(非阶段性文件)和my_file的分支master之间的差异。