How to Compare Local and Remote Git Branches
The local branch exists on the local machine and can be seen only by the local user. The remote branch is a branch on a remote location. Sometimes, you want to know what files are changed between the local and remote repositories. In this tutorial, we will show the simplest way of comparing local and remote branches.
Steps to comparing local and remote branches
Here are the three steps you should take to find the differences between local and remote branches easily:
Updating remote-tracking branches
First, type git fetch in the terminal to update the remote-tracking branches:
Listing local and remote branches
Then, you can run the git branch with -a option to list both the local and remote branches:
git branch -a
The output may look like this (suppose, the name of remote is origin, which is by default) :
* master remotes/main/master remotes/origin/HEAD -> origin/master remotes/origin/master remotes/origin/mt remotes/upstream/master remotes/upstream/mt
The asterisk next to the "master" branch indicates that you're currently on that branch.
If you want to show the remote branches then type the git branch with -roption:
git branch -r
Comparing local and remote branches
The second step is running the git diff command to show the difference between the branches:
git diff <local-branch-name> <remote-branch-name>
The git branch command creates, lists, and deletes branches. Branches represent an isolated line of development. Checking a local branch out from a remote one automatically generates a tracking branch, which is a local branch that has a direct relationship to a remote branch. A remote-tracking branch is a local copy of a remote branch. The -r option with git branch causes the remote-tracking branches to be listed, and the -a option shows both local and remote branches.
When you’re working on a new feature or bug fix, having the chance to see how branches differ helps to avoid lots of problems beforehand. The git diff is used to compare changes committed in Git. This command allows you to view the differences between the two versions. It takes two input data sets and output the modifications between them. While executing, this command runs a diff function on Git data sources, including commits, branches, files, etc.