如何区分本地分支和远程分支?


当前回答

在我的情况下,我有一个名为heroku的第二个远程,这不是起源,因为它不同步,我在试图运行git diff master heroku/master时得到了这个错误:

致命:模糊的参数“heroku/master”:未知的修订或路径不在工作树中。

或者当尝试其他方法git diff master. heroku/master:

致命:糟糕的修订'master..heroku/master'

解决方案是在运行git diff之前显式地在git fetch中提到远程名称,在我的情况下:

$ git fetch heroku
$ git diff master heroku/master

其他回答

如果你想看到的区别只是文件名的改变,那么使用:

git diff --name-status <remote-branch> <local-branch>

否则,这将显示两个分支之间的所有差异:

git diff <remote-branch> <local-branch>

设置

Git配置别名。Udiff 'diff @{u}'

带HEAD@{upstream}的差动HEAD

git fetch  # Do this if you want to compare with the network state of upstream; if the current local state is enough, you can skip this
git udiff

用任意远端分支差分

这回答了你标题中的问题(“它是远程的”);如果您希望区别于“远程”(未配置为分支的上游),则需要直接将其作为目标。您可以使用以下命令查看所有远程分支:

Git branch -r

您可以通过以下命令查看所有已配置的遥控器:

Git远程显示

你可以看到单个远程(例如origin)的分支/跟踪配置如下:

Git远程显示来源

一旦你确定了适当的起源分支,只需做一个正常的diff:)

git diff [MY_LOCAL] MY_REMOTE_BRANCH

我更理解输出:

Git diff <远程跟踪分支> <本地分支>

它告诉我,如果我推本地分支,什么将被删除,什么将被添加。当然,它是一样的,只是相反,但对我来说,它更可读,我更舒服地看着将要发生的事情。

这很简单。你可以使用:git diff remote/my_topic_branch my_topic_branch

其中my_topic_branch是您的主题分支。

我就是这么做的。

# To update your local.
git fetch --all

这将从远程获取所有内容,因此当您检查difference时,它将与远程分支进行比较。

# To list all branches
git branch -a

上面的命令将显示所有的分支。

# To go to the branch you want to check difference
git checkout <branch_name>
# To check on which branch you are in, use
git branch
    (or)
git status

现在,您可以检查如下差异。

git diff origin/<branch_name>

这将比较您的本地分支和远程分支。