Home > Enterprise >  How to compare two files in the same commit that is in the remote branch without switching the curre
How to compare two files in the same commit that is in the remote branch without switching the curre

Time:07-01

The remote repo has been fetched. How to compare two files in the same commit that is in the remote branch without switching the current local branch? In fact, there is no such corresponding local branch whose upstream branch is the aforementioned remote target branch.

Command-line is appreciated to achieve this goal.

CodePudding user response:

git diff origin/branch:path/to/file1 origin/branch:path/to/file2

# or
git diff <commit sha>:path/to/file1 <commit sha>:path/to/file2

You can actually put anything that points to a revision before the : : branch name, tag name, <sha>, HEAD, master~5, stash ...

You can also choose to compare two different files from two different commits :

git diff origin/deployment:deploy/config.conf origin/migration:.config/cfg.toml

There are other handy shortcuts which you can use to name commits, one I find myself using often is :

  • @{u} : the upstream branch of current branch (e.g: origin/master if I currently am on master)

See git help revisions for an extensive documentation of the many ways to reach a commit.

Another command worth knowing is git difftool :

# open the same diff as above, but in an external diff viewer :
git difftool <commit sha>:path/to/file1 <commit sha>:path/to/file2
git difftool --tool=kdiff3 ...

IMHO, it particularly shines when comparing directories (note: your question did not ask to compare directories, just mentioning it for other occasions) :

# add the '-d' option :
git difftool -d <commit1> <commit2>

# for example: compare HEAD and its upstream branch
git difftool -d HEAD @{u}
  •  Tags:  
  • git
  • Related