Git allows you to maintain local as well as remote copies of your code. It also allows you to easily find difference between two repositories, branches, and even files. Sometimes you may need to find difference between local and remote branches in your git repository. In this article, we will learn several simple commands to help you easily get diff between local & remote branch in git.
Git Diff Between Local & Remote Branch
Here is the simple command to see the difference between local & remote branch.
$ git diff <local branch> <remote>/<remote branch>
For example, if you want to view the difference between local branch abc and remote branch def, then here is the command to do so.
git diff abc origin/def
Here is another example where “main” is the local main branch and “origin/main” is a remote, namely the origin and main branch.
git diff main origin/main
You can also swap local and remote branch in the above command, to see what the push from local to remote will do.
git diff <remote>/<remote branch> <local branch>
Now let us look at some shortcuts for the same. If you are on a given branch, and you want to compare your working copy with the upstream branch you’re tracking.
git diff @{upstream}
If you want to compare your current HEAD with upstream branch, use the following command.
git diff @ @{upstream}
If your upstream branch is not set, you can use the following command to get a diff against branch that you are about to push.
In this article, we have learnt how to do git diff between local & remote branch. We have also mentioned some simple use cases and their shortcuts for your convenience.
Also read:
Bash Loop Through Content of File
How to Update Row Based on Previous Row
How to Disable Commands in Linux
How to Install Printer in Ubuntu via GUI
How to Increase SSH Connection Limit
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.