How to Compare Local and Remote Branches in Git

Abdul Jabbar Feb 02, 2024
How to Compare Local and Remote Branches in Git

Git is a free platform and open-source distributed version control system designed to control everything from tiny to huge projects with speed and efficiency.

The local branch can be found on the local personal station. It can only be seen by the local user who is currently logged in, and on the other hand, the remote branch is a type of branch that can be found on a remote location, which can be accessed by various users who have rights to it.

Sometimes, we need to know which files are changed on the local repository and compare it with the remote branch. So, in this case, we will show you the simplest way of comparing local and remote branches.

Let’s start by assuming that the following remote branch is mapped to the local branch, and we are already working on it.

git checkout -b local branch origin/Remotebranch

In this case, the above-mentioned branch local branch is a local branch while the origin/Remote branch is a remote branch on the server.

Compare Two Git Branches

The command git fetch will fetch all the changes in the origin branch into the local branch. Furthermore, the git diff command will tell us the different changes between our working tree local branch and the remote ones.

Git fetch command will tell the repository to fetch the desired branch data from the remote branch specified in the command. While this git fetch command will not influence the files in our working directory, it will not try to merge changes as git pull does. The simplest way of comparing two branches is by using git diff.

git diff <local branch path> <remote branch path>

We run the following command when we want to know what is changed in a particular file in the local environment.

# git diff --name-only Remotebranch

We can use the following command when we want to find the difference between two specific files from the local branch to the remote branch.

git diff local branch/README.md origin/Remotebranch/README.md

If we want to know what files are changed between the local and remote branches and what those changes exactly are, we can use the following command.

git difbashf --name-status origin/Remotebranch

If we want to Ignore some files, often there are some files that we don’t want to show in the difference of both local and remote files; we can ignore the difference in files by using the following command.

git diff --ignore-submodules origin/Remotebranch
Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn