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


当前回答

如果你正在比较当前分支和你想要git拉的东西,这里有一个简单的答案。

git fetch
git diff FETCH_HEAD

第一个命令将找出与当前分支对应的远程分支。FETCH_HEAD引用中该计算的人工产物。然后,第二个命令使用该引用与当前分支的引用进行比较。

其他回答

我更理解输出:

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

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

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

TLDR: git diff <本地分支> <远程分支>

当在shell中使用Git时,我喜欢首先通过四处查看来确定自己的方向。

下面是一个显示所有分支的命令

$ git branch -a  # (or git branch --all)
* my-branch
  master
  remotes/origin/some-branch
  remotes/origin/HEAD -> origin/master
  remotes/origin/my-branch
  remotes/origin/some-other-branch
  remotes/origin/master

这里有两个本地分支(my-branch和master)和四个远程分支(some-branch、some-other-branch、master和my-branch)。

此外,my-branch旁边的星号表示我目前在该分支中(您也可以通过使用命令git status来知道这一点,该命令将输出:On branch my-branch.)。

注意:Git Bash shell中的远程分支显示为红色,而本地分支显示为绿色。

如果你只想显示远程分支:

$ git branch -r # (or git branch --remotes)
  origin/some-branch
  origin/HEAD -> origin/master
  origin/my-branch
  origin/some-other-branch
  origin/master

为了只显示本地分支,您可能会使用git branch -l,但这是一个完全不同的命令。要显示本地分支,请使用没有选项的git branch

$ git branch
* my-branch
  master

为了完成对基本分支选项的回顾,有一个——列表,与您可能期望的相反,它允许过滤。使用它的模式如下:

$ git branch --list 'my*'
* my-branch

您还可以将——list与选项-a和-r结合使用,但请确保相应调整您的模式(记住:远程分支以“remotes”开头)。

例子:

# This will show all branches (local & remote) that start with my
$ git branch --list 'my*' -a
* my-branch

# Better: the pattern includes the remote
$ git branch --list '*my*' -a
* my-branch
  remotes/origin/my-branch

文档:git分支

现在您可以比较所有可用分支中的任意两个分支(还可以比较两个局部分支或两个远程分支)。

这里我比较了本地的和远程的my-branch。它们是同步的,所以我没有得到任何输出:

$ git diff my-branch remotes/origin/my-branch

注意:您必须给出分支的全名,不带引号。

我还可以将本地my-分支与远程主服务器进行比较。这里我得到了一些输出,因为远程my-分支还没有合并到主分支中。

$ git diff my-branch remotes/origin/master
diff --git a/src/controllers/call.controller.js b/src/controllers/call.controller.js
index fd79b98..df3d798 100644
--- a/src/controllers/call.controller.js
+++ b/src/controllers/call.controller.js
@@ -261,7 +261,7 @@ function callController() {
   /*
    *  Function: doCall
[ . . . ]

我就是这么做的。

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

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