How to compare a local Git branch with its remote branch
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.
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.)
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
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 bybranchname
is set to build on top of (configured withbranch.<name>.remote
andbranch.<name>.merge
). A missingbranchname
defaults to the current one.
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.