如何将本地 git 分支与其远程分支进行比较?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1800783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:24:50  来源:igfitidea点击:

How to compare a local git branch with its remote branch?

git

提问by mrblah

How can I see the diffbetween a local branch and a remote branch?

如何查看diff本地分支和远程分支之间的关系?

采纳答案by meder omuraliev

To update remote-tracking branches, you need to type git fetchfirst and then :

要更新远程跟踪分支,您需要先输入git fetch,然后输入:

git diff <masterbranch_path> <remotebranch_path>

You can git branch -ato list all branches (local and remote) then choose branch name from list (just remove remotes/from remote branch name.

您可以git branch -a列出所有分支(本地和远程),然后从列表中选择分支名称(只需remotes/从远程分支名称中删除。

Example: git diff master origin/master(where "master" is local master branch and "origin/master" is a remote namely origin and master branch.)

示例:(git diff master origin/master其中“master”是本地 master 分支,“origin/master”是远程,即 origin 和 master 分支。)

回答by Jakub Nar?bski

git diff <local branch> <remote>/<remote branch>

For example git diff master origin/master, or git diff featureA origin/next

例如git diff master origin/master,或git diff featureA origin/next

Of course to havesaid remote-tracking branchyou need to git fetchfirst; and you need it to have up to date information about branches in remote repository.

当然,到说,远程追踪分支,你需要git fetch先; 并且您需要它来获取有关远程存储库中分支的最新信息。

回答by YSN

First type

第一种

git branch -a

to get the list of available branches. On the output you may see something like

获取可用分支列表。在输出中,您可能会看到类似

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

Then show the diff

然后显示差异

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

回答by Andrew Grimm

If you're on a given branch, and you want to compare it with an upstream branch you're tracking, use

如果您在给定的分支上,并且想将其与正在跟踪的上游分支进行比较,请使用

git diff @{upstream}

if your upstream isn't set (commonly the case, thanks Arijoonin comments)

如果您的上游未设置(通常情况下,感谢Arijoon在评论中)

git diff @{push}

Courtesy of this answer, the git documentation for specifying revisionshas:

感谢这个答案指定修订的 git 文档有:

<branchname>@{upstream}, e.g. master@{upstream}, @{u}
The suffix @{upstream}to a branchname (short form <branchname>@{u}) refers to the branch that the branch specified by branchnameis set to build on top of (configured with branch.<name>.remoteand branch.<name>.merge). A missing branchnamedefaults to the current one.

<branchname>@{upstream},例如master@{upstream},分支名称@{u}
的后缀 @{upstream}(简称<branchname>@{u})是指指定的分支branchname设置为构建在其之上(使用branch.<name>.remote和配置branch.<name>.merge)的分支。缺少的branchname默认为当前的。

回答by manfer

I understand much better the output of:

我对以下输出的理解要好得多:

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

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

that shows me what is going to be dropped and what is going to be added if I push the local branch. Of course it is the same, just the inverse, but for me is more readable and I'm more confortable looking at what is going to happen.

这向我展示了将要删除的内容以及如果我推送本地分支将添加的内容。当然它是一样的,只是相反,但对我来说更具可读性,我更舒服地看待将要发生的事情。

回答by Brent Faust

The easy way:

简单的方法:

git fetch
git log -p HEAD..FETCH_HEAD

This will first fetch the changes from your default remote (origin). This will be created automatically when you clone a repo. You can also be explicit: git fetch origin master.

这将首先从您的默认远程(来源)获取更改。这将在您克隆存储库时自动创建。您也可以明确:git fetch origin master

Then git log is used to compare your current branch with the one just fetched. (The -p(generate patch) option is what shows the differences.)

然后 git log 用于将您当前的分支与刚刚获取的分支进行比较。(-p(生成补丁)选项显示了差异。)

回答by Sahith Vibudhi

This is how I do it.

我就是这样做的。

#To update your local.
git fetch --all

this will fetch everything from the remote, so when you check difference, it will compare the difference with the remote branch.

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

#to list all branches
git branch -a

the above command will display all the branches.

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

#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

Now, you can check difference as follows.

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

git diff origin/<branch_name>

this will compare your local branch with the remote branch

这会将您的本地分支与远程分支进行比较

回答by user2314737

tl;dr: git diff <local branch> <remote branch>

tl;博士git diff <local branch> <remote branch>

When using git on the shell, I like to first orient myself by looking around. Here's a command to show all branches

在 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

Here I have two local branches (my-branchand master) and 4 remote (some-branch, some-other-branch, master, and my-branch).

在这里,我有两个本地分支 ( my-branchand master) 和 4 个远程分支( some-branch, some-other-branch, master, and my-branch)。

Also, the asterisk next to my-branchsignals the fact that I'm currently in that branch (you would also know that by using the command git statusthat would output: On branch my-branch.).

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

Note: the remote branches in the git bash shell are shown in red while the local ones are shown in green.

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

If you just want to show remote branches:

如果您只想显示远程分支

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

To show just local branches you might be tempted to use git branch -lbut that's a completely different command.To show local branchesuse git branchwith no options

要仅显示本地分支,您可能会尝试使用,git branch -l但这是一个完全不同的命令。为了显示当地分支机构使用git branch不带任何选项

$ git branch
* my-branch 
  master

To complete a review of the basic branch options there's the --listthat contrary to what you might expect is there to allow filtering. Use it with a pattern like this:

要完成对基本分支选项的--list,与您可能期望的相反的是允许过滤。将它与这样的模式一起使用:

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

You can also combine --listwith the options -aand -rbut make sure to adapt your pattern accordingly (remember: remote branches start with "remotes"). Example:

您还可以--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

Docs: https://git-scm.com/docs/git-branch

文档:https: //git-scm.com/docs/git-branch

Now you can compare any two branchesfrom all the available ones (you can also compare two locals or two remotes).

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

Here I'm comparing the local with the remote my-branch, they're synchronized so I don't get any output:

在这里,我将本地与远程进行比较my-branch,它们是同步的,所以我没有得到任何输出:

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

Note: you have to give the full names of the branches with no quotation marks.

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

I can also compare the local my-branchto the remote master. Here I get some output because the remote my-branchhasn't been merged into the master branch.

我还可以将本地my-branch与远程进行比较master。在这里我得到了一些输出,因为远程my-branch尚未合并到主分支中。

$ 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
[ . . . ]

回答by Ratna Halder

Let your working branch is development and want to differentiate between local development branch and remote development branch, that case, syntax should be like git diff remotes/origin/development..development
or

让你的工作分支是 development 并且想要区分本地开发分支和远程开发分支,这种情况下,语法应该像git diff remotes/origin/development..development

git fetch origin git diff origin/development

git fetch origin git diff origin/development

回答by Sarvesh Kesharwani

If you want to see the difference as just the names of the files changed then use:

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

else this would show all differences between the two branches:

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

如果您只想查看文件名称更改时的差异,请使用:

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

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

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