1180
votes

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

23
This questions was asked again later. It has a nice answer: stackoverflow.com/questions/11935633/…rustybeanstalk
example: git diff master origin/master (where master is local master branch and origin/master is remote master branch).Dung
@klyngbaek, this answer in particular ;-)user8554766
I was originally trying git diff HEAD origin/HEAD which seems to point to a different remote branch than the one I intended. Using the full branch name works as expected.Deanna

23 Answers

730
votes

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

git diff <mainbranch_path> <remotebranch_path>

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

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

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

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

Of course to have said remote-tracking branch you need to git fetch first; and you need it to have up-to-date information about branches in the remote repository.

194
votes

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
184
votes

If you're on a given branch, and you want to compare your working copy with the upstream branch you're tracking, use:

git diff @{upstream}

If you want to compare your current HEAD with the upstream branch (thanks @Arijoon):

git diff @ @{upstream}

If your upstream isn't set, you can use @{push} to get a diff against the branch you are set to push to (also from @Arijoon's comment):

git diff @{push}

Courtesy of this answer, the git documentation for specifying revisions has:

<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 branchname is set to build on top of (configured with branch.<name>.remote and branch.<name>.merge). A missing branchname defaults to the current one.

45
votes

I understand much better the output of:

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 it is more readable, and I'm more comfortable looking at what is going to happen.

32
votes

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 repository. You can also be explicit: 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.)

16
votes

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 the differences as follows.

git diff origin/<branch_name>

This will compare your local branch with the remote branch.

13
votes

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
[ . . . ]
11
votes

Let your working branch be development and you want to differentiate between the local development branch and the remote development branch. In that case, the syntax should be like:

git diff remotes/origin/development..development

Or

git fetch origin
git diff origin/development
11
votes

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>
9
votes

Here is a shorthand answer if you are comparing your current branch and to something you want to git pull.

git fetch
git diff FETCH_HEAD

The first command will figure out which remote branch corresponds to your current branch. An artefact of that calculation in the FETCH_HEAD reference. Then the second command uses that reference to compare versus what your current branch has.

6
votes

In my case I have a second remote called heroku that is not the origin and because it wasn't in sync, I got this error when trying to run the git diff master heroku/master:

fatal: ambiguous argument 'heroku/master': unknown revision or path not in the working tree.

Or this when trying the other approach git diff master..heroku/master:

fatal: bad revision 'master..heroku/master'

The solution was explicitly mentioning the remote name in git fetch before running git diff, in my case:

$ git fetch heroku
$ git diff master heroku/master
5
votes
git difftool <commit> .

This will compare the commit you want with your local files. Don't forget the dot in the end (for local).

For example, to compare your local files with some commit:

git difftool 1db1ef2490733c1877ad0fb5e8536d2935566341 .

(and you don't need git fetch, unless comparing to new commits is needed)

2
votes

Example

git diff 'master' 'testlocalBranch'

If you are using an editor like WebStorm, you can right click on a file, select compare with branch, and type/select your branch.

Enter image description here

Enter image description here

2
votes

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. :)

1
votes

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.

0
votes

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

Where my_topic_branch is your topic branch.

0
votes

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

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

0
votes

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
    
0
votes

In Android Studio, it is possible to see the difference between branches using graphical interface.

Select your remote branch and and "Compare with current" from the list. From then you can select the files tab to see if there are any files that have content difference between both branches. If no file is seen, then both branches are up-to-date with each other.

0
votes

For counting of the different commits from HEAD to origin/master:

git remote update
git rev-list HEAD..origin/master --count

To get the current HEAD commit hash ID:

git rev-parse HEAD

To get the remote origin/master commit hash ID:

git rev-parse origin/master
-1
votes

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

-2
votes

If you use TortoiseGit (it provides a GUI for Git), you can right click your Git repository folder and then click Git Sync.

You can select your branches to compare if not selected. Then you can view the differences commit. You can also right click on any commit and then Compare with previous revision to view differences side by side.

TortoiseGit sync to compare a remote and local branch