Search

Creating your own custom git commands is really easy

When given the choice, I generally prefer a command line option over a GUI. While everyone around me is using Sourcetree, git extensions in VS, or any number of other git plugins or GUIs that do everything for them, I'm actually learning how it all works by doing it manually at the command line. At work, I'm stuck on Windows so I use git-bash and the ubuntu subsystem for as much as possible. At home, I'm using zsh on my Macs or bash on Linux servers.

Besides allowing you to learn much more about the inner workings of the utility, command line options are generally far easier to extend with more scripts (be it Powershell or bash). And I do love to write shell scripts :-)

Git is no different, you can write all the custom git commands you need to make your life easier. One example I have is a git-diffall command. I use Beyond Compare for diffing my changes (yes, the GUI!) but the 'git difftool --no-prompt' command will only open one file at a time. I'd prefer they all open up in individual tabs at once in Beyond Compare. So I wrote a super simple 'git-diffall' command to do exactly that. Below are the details on how I did that.

First things first, you need to create the script file. These examples will focus mostly on macOS but the same can be done on Windows and Linux systems; the only real difference will be the location you store the script. In the end, they simply need to be in your $PATH somewhere. On Windows, run the command "echo %PATH%" to find out where that is. On Linux macOS, use "echo $PATH" to find out. I will be using /usr/local/bin.

Once you have the path, just create the new script file

sudo touch /usr/local/bin/git-diffall


Next, make the script executable
sudo chmod +x /usr/local/bin/git-diffall


Now use your favorite editor to edit the script file.
sudo vim /usr/local/bin/git-diffall


The following code does everything you need for this example so add it to the file, save and exit the editor.

#!/bin/bash
git diff --name-only "$@" | while read filename; do
git difftool "$@" --no-prompt "$filename" &
done


Breaking it down, the script is simply using git-diff --name-only to get a list of files that have been changed but not yet staged. It loops over each of those files, opening each one in your defined difftool.

Now, provided you have already configured beyond compare as your difftool, you simply type "git-diffall" inside your repository directory like you would any other git command.

If you use git commands regularly, you know there are a TON of options here... building packages, deploying, creating release notes, and on and on and on.

For those still trying to figure out how to configure Beyond Compare 3 or 4 as your difftool, read on.

First, you need the beyond compare command line tools installed. In macOS, simply open Beyond Compare, click the Beyond Compare menu across the top of the screen and click the "Install command line Tools..." option. Once that is done, you simply need to run the following command.

 git config --global diff.tool bc3 

If you have any trouble or need to do this for another OS, try this guide at Scooter Software

Note For "Git for Windows" users (aka, Git-Bash), the path to put your custom git commands would be C:\Program Files\Git\mingw64\bin

Most Recent Photos