This guide will cover the concepts and detailed commands necessary to compare local and remote branches using git diff
.
Understanding the difference between local and remote branches
Before diving into the Git commands, it's important to clarify the difference between local and remote branches in Git:
- Local branches are branches that exist in your local repository, on your local machine. These branches can be created, modified, and deleted without affecting the remote repository until changes are explicitly pushed.
- Remote branches represent references to the state of branches in a remote repository at the last time you fetched from them. They are prefixed with the name of the remote, typically
origin
, followed by the branch name, likeorigin/master
.
Performing diffs between local and remote branches
1. Fetch upstream changes
Before you look at the diff between a local and remote branch you should make sure you run git fetch
first. This ensures you have all of the most recent changes from the remote branch.
2. Git diff between local and remote
To see the differences between your local branch and its upstream branch (the remote branch it tracks), you can use:
git diff <local-branch> origin/<remote-branch>
This command will show you the differences between your local branch and the remote branch. If you want to directly compare the branch you are currently on to its remote counterpart.
As an example let's say we have two versions of file1.txt, one locally, and one stored in a remote repository. We'll assume that local-branch
is ahead of origin/remote-branch
with some changes:
$ git diff feature origin/featurediff --git a/file1.txt b/file1.txtindex 7b18d64..4ac9e17 100644--- a/file1.txt+++ b/file1.txt@@ -1,3 +1,3 @@-Hello, World!+Hello, Earth!This is an example file.It has a few lines of text.
diff --git a/file1.txt b/file1.txt
:- This line shows the file that has differences between the two branches. The format is
a/<filename> b/<filename>
, indicating the comparison froma
(left side orlocal-branch
) tob
(right side ororigin/remote-branch
).
- This line shows the file that has differences between the two branches. The format is
index 7b18d64..4ac9e17 100644
:- This line shows the blob identifiers (SHA-1 hashes) for the file before and after the changes.
100644
is the file mode, indicating a normal non-executable file in both branches.
- This line shows the blob identifiers (SHA-1 hashes) for the file before and after the changes.
--- a/file1.txt
and+++ b/file1.txt
:- These lines indicate the start of the changes in the file from
local-branch
toorigin/remote-branch
.--- a/file1.txt
represents the original file inlocal-branch
, and+++ b/file1.txt
represents the file inorigin/remote-branch
.
- These lines indicate the start of the changes in the file from
@@ -1,3 +1,3 @@
:- This line, known as a "hunk header", shows the lines of context around the changes. The numbers show the starting line and the number of lines displayed from each version of the file.
-1,3
shows that the original chunk starts at line 1 and spans 3 lines.+1,3
shows the same for the changed file.
- This line, known as a "hunk header", shows the lines of context around the changes. The numbers show the starting line and the number of lines displayed from each version of the file.
Lines beginning with
-
and+
:- Lines that start with
-
indicate lines that exist in the local branch but were changed or removed in the comparison branch (here,origin/remote-branch
). Lines that start with+
indicate lines that are added or changed in theorigin/remote-branch
as compared tolocal-branch
. - In the example:
-Hello, World!
shows that "Hello, World!" was the original line inlocal-branch
but changed.+Hello, Earth!
shows that inorigin/remote-branch
, it has been changed to "Hello, Earth!".
- Lines that start with
3. Comparing two remote branches
To compare two remote branches, first, ensure your local references are up to date using git fetch
, then run:
git diff origin/<branch-one> origin/<branch-two>
This will show you the difference between two remote branches using the same diff notation as above.
4. Git diff with remote using range
If you want to see the changes introduced by the remote branch since the last common ancestor, you can use:
git fetch origingit diff ...origin/<remote-branch>
The three-dot syntax tells Git to use the merge base of the branches as the starting point for the diff.
4. Git diff against remote commit
Sometimes, you may need to compare your current branch with a specific commit from a remote branch:
git fetch origingit diff <commit-hash>
This will show differences from your current branch to the specified commit from the fetched remote data.
5. Git diff specific remote and local file
If you need to compare a specific file between your local branch and a remote branch, use:
git diff <local-branch>:<file-path> origin/<remote-branch>:<file-path>
This allows for a file-by-file comparison between local and remote branches.
For further reading on the git diff
command, see the official Git documentation.