How to Compare Local and Remote Branches
- Steps to comparing 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.
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:
1. Updating remote-tracking branches
First, type git fetch in the terminal to update the remote-tracking branches:
2. Comparing local and remote branches
The second step is running the git diff command to show the difference between the branches:
git diff <masterbranch_path> <remotebranch_path>
3. Listing local and remote branches
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 -roption:
git branch -r
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.