How to Write and Install a Custom Git Script

Post by on November 5, 2012

Git is amazing. Specifically, its ability to be extended is amazing. Essentially any "git <command>" can be made by naming a file "git-<command>" and placing it in your git-core folder. Let's look at an example.

Assume you are writing a script to give you a list of all stashes and print out the diff of each staff, all in one fell swoop. Assume you want to access this script by typing "git stash-list".

First, name the file "git-stash-list". Then, put this file in your git-core folder. You can find this folder by doing the following:

$ find / -name "git-core"

I'm on OS X 10.6 and found it in /opt/local/libexec/git-core and I believe I'm using the MacPorts version. Then, be sure to chown and chmod the file the same as the other "git-*" commands in that directory. For me, I did:

$ sudo chown root:admin git-stashlist
$ sudo chmod 755 git-stashlist

Thanks to Tim Cunningham for his post on how to "Create your own custom git command!".

Older Posts »