Compare two Git branches without cloning

Viewed 1312

With Git CLI can we compare two git branches without cloning anyone of them? (same repo)

e.g. branches:

  1. master
  2. test_branch1
  3. test_branch2

I want a command with the help of which I can compare something like:

git diff test_branch1 test_branch2
2 Answers

You cannot run git diff locally without cloning or fetching commits. If you want to avoid git clone/fetch there are two options left.

If you have SSH access to the remote repository you can run git diff directly on the remote server:

ssh <user@origin-URL> """
    cd /path/to/repository &&
    git diff test_branch1 test_branch2
"""

If you don't have SSH access but there the repository is hosted at a server with web interface (you used tag [gitlab] in your question) then you can use the web interface to compare commits. For example: https://gitlab.com/sqlobject/sqlobject/compare/ed64be0ed032055b0a6613fe3051d83a74ded566...bebfdf9512ca6cdf94a3724dc2625a2288246945

If you have neither SSH nor web access your only option is to clone/fetch and run git diff locally.

You don't clone branches, you clone the repository itself. If you want to compare remote branches, you can do:

git fetch
git diff origin/branch_1 origin/branch_2

If you don't want to get the files from the repo, you can do the following:

mkdir example
git init
git remote add origin <remote URL>
git fetch
git diff origin/branch_1 origin/branch_2
Related