How to Compare a Local Git Branch With Its Remote Branch
Comparing a local Git branch with its remote counterpart is a common task to understand the differences in commits and changes between the two. Below are several methods to achieve this comparison.
1. Using git diff
To compare the differences between a local branch and its remote branch, you can use the git diff
command.
Compare with Remote Branch
git diff <local-branch>..<remote-branch>
Example:
git diff master..origin/master
This command will show you the differences in the files between your local master
branch and the remote master
branch on origin
.
2. Using git log
To see the commit differences between the two branches, you can use the git log
command.
Commits in Local Branch but Not in Remote
git log <remote-branch>..<local-branch>
Example:
git log origin/master..master
This command lists the commits that are in the local master
branch but not in the remote master
branch.
Commits in Remote Branch but Not in Local
git log <local-branch>..<remote-branch>
Example:
git log master..origin/master
This command lists the commits that are in the remote master
branch but not in the local master
branch.
3. Using git fetch
and git status
First, fetch the latest changes from the remote repository to ensure you have the latest references.
git fetch
Then, check the status to see if your local branch is ahead or behind the remote branch.
git status
4. Using Git GUI Tools
Many Git GUI tools provide visual ways to compare branches, such as GitKraken, SourceTree, and GitHub Desktop.
5. Using git cherry
The git cherry
command can show which commits from your local branch have not yet been applied to the upstream branch.
git cherry -v <upstream> <local-branch>
Example:
git cherry -v origin/master master
Example Scenario
Suppose you are working on a branch called feature-branch
and you want to compare it with its remote counterpart origin/feature-branch
.
Fetch the latest changes from the remote repository.
git fetch
Use
git diff
to see the differences in the files.git diff feature-branch..origin/feature-branch
Use
git log
to see the commits in your local branch that are not in the remote branch.git log origin/feature-branch..feature-branch
Check the status to see if your branch is ahead or behind.
git status
Use
git cherry
to see which commits are on your local branch but not on the remote.git cherry -v origin/feature-branch feature-branch
Conclusion
By using these commands, you can effectively compare your local branch with its remote counterpart to understand the differences in commits and file changes.
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github