How to compare a local Git branch with its remote branch

Viewed 1099381

How can I see the diff between a local branch and a remote branch?

22 Answers

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

TLDR: git diff <local branch> <remote branch>

When using Git in the shell, I like to first orient myself by looking around.

Here's a command to show all branches

$ 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-branch and master) and four remote branches (some-branch, some-other-branch, master, and my-branch).

Also, the asterisk next to my-branch signals the fact that I'm currently in that branch (you would also know that by using the command git status that would output: 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.

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 -l, but that's a completely different command. To show local branches use git branch with no options

$ git branch
* my-branch
  master

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

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

You can also combine --list with the options -a and -r, but make sure to adapt your pattern accordingly (remember: remote branches start with "remotes").

Example:

# 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

Documentation: git-branch

Now you can compare any two branches from 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:

$ 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-branch to the remote master. Here I get some output, because the remote my-branch hasn't been merged into the master 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
[ . . . ]

Try:

git diff origin HEAD

Assuming you want to diff you current local branch's HEAD against the origin. And assuming you are on the local branch. :)

I wonder about if there is any change in my master branch...

  1. Firstly, you need to change your branch (If you are already under this branch, you do not need to do this!):

    git checkout master
    
  2. You can see which file has been modified under your master branch by this command:

    git status
    
  3. List the branches

    git branch -a
    
    • master
      remotes/origin/master
  4. Find the differences

    git diff origin/master
    

This is quite simple. You can use: git diff remote/my_topic_branch my_topic_branch

Where my_topic_branch is your topic branch.

Let's say you have already set up your origin as the remote repository. Then,

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

FWIW you can use the --compact-summary option.

man git diff

Output a condensed summary of extended header information such as file creations or deletions ("new" or "gone", optionally "+l" if it’s a symlink) and mode changes ("+x" or "-x" for adding or removing executable bit respectively) in diffstat. The information is put between the filename part and the graph part. Implies --stat.

e.g.

git diff $(current_branch) origin/$(current_branch) 

I kept seeing this error

git diff main origin/master
fatal: ambiguous argument 'main': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Solution: I had to git push first (since both the remote and local need to be up to date), and then this worked:

git diff master origin/master

Setup

git config alias.udiff 'diff @{u}'

Diffing HEAD with HEAD@{upstream}

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

Diffing with an Arbitrary Remote Branch

This answers the question in your heading ("its remote"); if you want to diff against "a remote" (that isn't configured as the upstream for the branch), you need to target it directly. You can see all remote branches with the following:

git branch -r

You can see all configured remotes with the following:

git remote show

You can see the branch/tracking configuration for a single remote (e.g. origin) as follows:

git remote show origin

Once you determine the appropriate origin branch, just do a normal diff :)

git diff [MY_LOCAL] MY_REMOTE_BRANCH

In Visual Studio 2019, just do fetch. Do not pull code.

This is what I did. I added the below in the .gitconfig file so that I can use Beyond Compare

File location: C:\Users\[username]\.gitconfig

Added below

[diff]
    tool = bc
[difftool "bc"]
    path = c:/Program Files/Beyond Compare 4/bcomp.exe

Open a command prompt and go to the working directory. I gave the below to compare the local dev branch to the remote dev branch:

git difftool dev origin/dev --dir-diff

This will open Beyond Compare and open directories which have files that differ. If there aren't any changes, Beyond Compare will not launch.

Related