How to Compare Local and Remote 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 which files changed between the local and remote repositories. In this tutorial, we will show the simplest way of comparing local and remote branches.
First, type git fetch in the terminal to update the remote-tracking branches:
The second step is running the git diff command to show the difference between the branches:
git diff <masterbranch_path> <remotebranch_path>
Then, you can run the git branch with -a option to list all the local and remote branches:
git branch -a
The output may look like this:
* 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 in that branch.
If you want to show the remote branches then type the git branch with -r option:
git branch -r
Then will show the difference:
git diff master origin/master
Here, "master" is the local master branch, and "origin/master" is a remote, name origin and master branch.
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.