Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two Git branches without cloning

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
like image 374
Ganesh Avatar asked May 17 '26 09:05

Ganesh


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.

like image 194
phd Avatar answered May 20 '26 00:05

phd


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
like image 45
Maroun Avatar answered May 20 '26 01:05

Maroun