Directory comparison of Git branches
Asked Answered
T

4

14

One of my favorite workflows with svn is to use Beyond Compare's folder comparison feature to see the net differences between two branches, or a branch and the trunk. Is there a way to do this in git without having to manually create multiple clones of the same repository?

As I ask this question, it occurs to me that I could write a script that would clone the current repo to a temporary directory, checkout the desired branch, and then call BCompare.exe with the two directories as arguments. The folder comparison view is invoked with

BCompare.exe path/to/folder1 path/to/folder2

Does this sound reasonable? Would I be able to delete the extra clone after I'm done with Beyond Compare?

Thorin answered 22/11, 2010 at 18:24 Comment(2)
"Would I be able to delete the extra clone...?" why wouldn't you?Pantelleria
Well, ideally, I'd like to launch beyond compare in the background so that I can continue to use git in the same terminal to view the log, etc. With that, I'm not sure what would detect that I've closed beyond compare and its safe to delete the temporary clone.Thorin
C
2

It sounds like you've already figured out the right answer -- use git clone and git checkout to set up a directory to compare to, then run BCompare.exe. The below script might be a good starting point.

#!/bin/sh
(                              # execute in a subshell so you can continue
                               #   working in the current shell
    set -o xtrace              # bash setting that echos each command before it's executed
    > /tmp/auto_bcompare_log   # truncate existing log file
    BRANCH="$1"                # get branch argument from command line
    TEMPDIR=`mktemp -d`        # get a temp directory
    CWD=`pwd`                  # remember the current directory
    git clone $CWD $TEMPDIR
    cd $TEMPDIR
    git checkout $BRANCH
    cd $CWD
    BCompare.exe $CWD $TEMPDIR
    rm -rf $TEMPDIR
) >> /tmp/auto_bcompare_log 2>&1 < /dev/null & # background and redirect
                                               # stdout/stderr/stdin
Contrapose answered 23/11, 2010 at 23:30 Comment(2)
This more or less worked for me. I took out the logging stuff because I'd rather see it on the command line gimp'd it so it'd work with msysgit, and I made it an alias. The nice thing about it being an alias is that you can in any directory of the repo and it works automagically. I opted not to use () since I can always use & to make it run in the background. Alias itself is in the next comment.Thorin
git config --global alias.ddiff '!sh -c "TWD=/ddifftmp; mkdir $TWD; CWD=pwd; git clone $CWD/.git $TWD; cd $TWD; git checkout $1; cd $CWD; BComp.exe $TWD $CWD; rm -rf $TWD;'Thorin
C
34

This (comparing directories instead of file-by-file) should be available soon:
See [ANNOUNCE] Git 1.7.11.rc1:

"git difftool" learned the "--dir-diff" option to spawn external diff tools that can compare two directory hierarchies at a time after populating two temporary directories, instead of running an instance of the external tool once per a file pair.

See "Patch difftool: teach difftool to handle directory diffs", from this fork of git:

When 'difftool' is called to compare a range of commits that modify more than one file, it opens a separate instance of the diff tool for each file that changed.

The new '--dir-diff' option copies all the modified files to a temporary location and runs a directory diff on them in a single instance of the diff tool.

Coordinate answered 4/6, 2012 at 10:26 Comment(4)
If I could upvote more than once I would. This is perfect and should be the accepted answer.Herzig
OMG This changes my life !Angus
@vonc The link [ANNOUNCE] Git 1.7.11.rc1 is broken. I suggest replacing it with this Webarchive link.Superhighway
@EricBouchut Thank you for the feedback. I have restored the link (with one from the Git mailing list)Coordinate
C
2

It sounds like you've already figured out the right answer -- use git clone and git checkout to set up a directory to compare to, then run BCompare.exe. The below script might be a good starting point.

#!/bin/sh
(                              # execute in a subshell so you can continue
                               #   working in the current shell
    set -o xtrace              # bash setting that echos each command before it's executed
    > /tmp/auto_bcompare_log   # truncate existing log file
    BRANCH="$1"                # get branch argument from command line
    TEMPDIR=`mktemp -d`        # get a temp directory
    CWD=`pwd`                  # remember the current directory
    git clone $CWD $TEMPDIR
    cd $TEMPDIR
    git checkout $BRANCH
    cd $CWD
    BCompare.exe $CWD $TEMPDIR
    rm -rf $TEMPDIR
) >> /tmp/auto_bcompare_log 2>&1 < /dev/null & # background and redirect
                                               # stdout/stderr/stdin
Contrapose answered 23/11, 2010 at 23:30 Comment(2)
This more or less worked for me. I took out the logging stuff because I'd rather see it on the command line gimp'd it so it'd work with msysgit, and I made it an alias. The nice thing about it being an alias is that you can in any directory of the repo and it works automagically. I opted not to use () since I can always use & to make it run in the background. Alias itself is in the next comment.Thorin
git config --global alias.ddiff '!sh -c "TWD=/ddifftmp; mkdir $TWD; CWD=pwd; git clone $CWD/.git $TWD; cd $TWD; git checkout $1; cd $CWD; BComp.exe $TWD $CWD; rm -rf $TWD;'Thorin
L
0

Just git diff with the names or hashes of the branches you want to compare, simple as that. Actually, you can compare any two commits by their hashes.

Leonor answered 22/11, 2010 at 18:27 Comment(3)
And if I don't like the forced linearity of git diff's inline comparisons and arbitrary file ordering then I'm SOL? I know I can also git difftool b1 b2 but that doesn't give me the folder comparison view.Thorin
Hmm, sorry, I didn't get your question, I thought you were just trying to compare two commits. You are using beyondcompare as the diff tool I assume, right? Unfortunately, I'm not familiar with that.Leonor
@drhorrible: So your question is actually how to get this specific "folder comparison view", not just how to see the diff between two branches. Might want to edit to make that a bit clearer.Pantelleria
V
0

First verify you have beyond compare accessible,

you can use: git difftool --tool-help to list the available compare tools. (to set it as default see Git Diff with Beyond Compare)

If you have beyond compare you can use one of those:

git difftool branch1 branch2 file -t bc   // or -d directory instead of file
git difftool hash1   hash2   file -t bc

EDIT: works in git version 2.25

Vercingetorix answered 9/9, 2020 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.