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


当前回答

我就是这么做的。

# 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>

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

其他回答

第一个类型

git branch -a

获取可用分支的列表。在输出中,您可能会看到如下内容

* master
  remotes/main/master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/mt
  remotes/upstream/master
  remotes/upstream/mt

然后显示差异

git diff --stat --color remotes/main/master..origin/master
git diff remotes/main/master..origin/master

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

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

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

git diff <remote-branch> <local-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>

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

在我的情况下,我有一个名为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 difftool <commit> .

这将比较您想要的提交和您的本地文件。不要忘记最后的点(本地)。

例如,比较你的本地文件和一些提交:

git difftool 1db1ef2490733c1877ad0fb5e8536d2935566341 .

(并且你不需要git fetch,除非需要与new commit进行比较)