Tuesday, 24 January 2017

Using beyond compare with Git on Linux (and windows)

EDIT - See footer for more info and an alternative to the main body of this post.

I'm a massive fan of Beyond compare, I use it a lot. It's brilliant for all the tasks related to file and folder diffs, either on the file system or in source control when viewing changes between revisions or branches.

Using git diff is ok when you are looking at a small set of changes but if there are many changes across many files I much prefer to use beyond compare to visualise the change set.

Git / Beyond Compare integration

The following code snippets are taken from a centos linux build, but there is no reason that with a little tweak this wont work on a windows machine. (In fact the little tweak is shown in the appendix at the bottom).

Edit your ~/.gitconfig (c:/users/UserName on windows) by adding the following:

  tool = bcompare
[difftool "bcompare"]
  cmd = bcompare \"$LOCAL\" \"$REMOTE\"
  prompt = false
  tool = bcompare
[mergetool "bcompare"]
  cmd = bcompare \"$LOCAL\" \"$REMOTE\" \"$BASE\" \"$MERGED\"
  keepBackup = false
  trustExitCode = false
  diffdir = difftool -dir-diff -tool=bcompare -no-prompt

The above assumes that you have installed beyond compare and it is now accessible from the terminal as bcompare. In fact you can see how I install beyond compare in my vagrant bash scripts here: https://github.com/DamianStanger/centos-node-developer-build/blob/master/scripts/beyondcompare.sh

Usage - launching beyond compare from the git command line:

#View all un-staged differences one by one
git difftool

#View all committed differences between the current HEAD and the penultimate commit one by one
git difftool HEAD HEAD~1

#View all differences between master branch and bugfix branch in beyond compares directory view
git diffdir master bugfix

#View all differences between two change sets (SHA-1 ) in beyond compares directory view
git diffdir 6b6aa87 4f7d15d

#View the diff of one un-staged file, note the use of -no-prompt to stop the prompt on the terminal
git difftool -no-prompt myfile.txt

#View all changes both staged and modified one by one.
#As soon as you close beyond compare the next change will pop open (due to the -no-prompt flag)
git difftool HEAD -no-prompt

You might notice that when you use the diffdir command that the files are not editable, where as if you use the more interactive difftool command that the local files are available for changing and saving in beyond compare. This is because behind the scenes when you use the -dir-diff flag, git copies the modified files to a temporary location and then performs a directory diff on them. So if you do want to modify the files as you do a comparison you must use the more interactive difftool command.




On windows its just as easy. The users gitconfig is located at c:\users\UserName\.gitconfig

Just replace the difftool and mergetool commands from the above code snippets with the following:
cmd = "'c:\\Program Files\\Beyond Compare 4\\BCompare.exe' $LOCAL $REMOTE"
cmd = "'c:\\Program Files\\Beyond Compare 4\\BCompare.exe' $LOCAL $REMOTE $BASE $MERGED"


Since writing this post I’ve done more playing with git, difftool and beyond compare and have made the following revelations. Which I've only tested on linux centos 7.0 with git
  • Out of the box you can use beyond compare as your difftool
    • Use 'git difftool -tool-help' to show all the options you have, do you see bc3 in the list? if so you are set to go.
    • Use 'git difftool -tool bc3' to open your diffs in beyond compare
  • Same with the mergetool
    • git mergetool --tool-help
    • git mergetool --tool bc3
I’ve also found that you can actually edit and save files whilst using the diffdir commaand, here’s how/why:

As I said before when using the -dir-diff command git makes copies of the files into a temp location. Well if one of the sides contains the modified (current working directory) files then the copy is not a hard file copy it’s a simlink to the file in the working directory. So, that means that if you have the option ‘Handling -> follow symbolic links’ in the ‘session settings – folder compare’ dialogue of beyond compare (I recommend that you save this as a session default) then any editing of files that you do WILL be in the working folder of your git repo. Bonza.

So what does all this mean to me? Do I actually need to edit my .gitconfig then?

Well, no, and yes. Beyond compare integration is there out of the box but I would create some alias entries so you don’t have to type in the full command as that would be painful.
  bc3 = difftool -tool=bc3
  bc33 = difftool -no-prompt -tool=bc3
  bc3dir = difftool -dir-diff -tool=bc3

You now can issue the same commands as outlines above but with 'git bc3' or 'git bc3dir' rather than 'git difftool' and 'git diffdir'

Notice that I’ve made a distinction between bc3 and bc33. bc33 will not prompt you for opening the changes and so if you use it with a large change set you will have to cancel through a lot of files until the process ends. But I guess you could always ctrl+c on the terminal to kill the process, either way I find the distinction useful for just one extra character to type.

No comments:

Post a Comment