No Credit Check Payday Loans

Create custom commands in Git

Let me explain a bit about Git before I get into the subject of this post. click hear if you don’t want to read my opinion on Git

Git is a wonderful thing. It’s fast, provides a great speed boost, and it’s quick. It does merging like I want an SCM to do.

Git is also a terrible thing (for me). It discards most of the command knowledge I have accumulated with SVN, or even Mercurial (my previous DSCM of choice), and sometimes replaces that knowledge with poorly exposed commands or completely different concepts. An example of the latter would be the git-revert command how it compares to the svn revert command. That isn’t so much of a big deal since Git doesn’t (and shouldn’t) let the decisions of a separate project dictate its interface; that said, it doesn’t change the fact that 95% of Git new adopters will be coming from an environment where revert undoes your currently uncommited changes.

The former is more troubling, but thankfully being worked upon. The analog to svn revert is git-checkout -f. I think that this is mostly confusing when perusing the man page, I thought to myself “Hey, there is git-reset, git-revert, there has to be some other command here like that that undoes my uncommitted changes”. The point is that common operations are not so easily learned in the git workflow. There are some good docs such as Everyday Git, but in general the learning process is somewhat painful for Git. Luckily…

Extending git

Git is made up of a hundred or so tiny applications, that are easily chained together to compose custom functionality. These applications expose functionality at high (end-user) level, and more interestingly at a very low-level. An example would be git-knacks that I hacked together yesterday evening in 20 minutes. They provide an easy way to shelve and unshelve code that isn’t ready for a real commit. More on that later…

What I didn’t know, though, was that the git command uses a simple naming convention for sub commands that you can use to inject functionality into the git command for others t use. git reset executes git-reset and git checkout-index runs git-checkout-index. All you have to do is write your own commands in whatever language you choose, name it properly, and presto… git hello-world is now in your toolchain.

I had thought the git- commands where simply for tab-completion support, and were just aliases into the “git blah” style commands. I was exactly oppositely wrong.

The unix-way got me to switch from Mercurial. Sure, I can write scripts that call to the hg command, but there isn’t a way, that I can see, to get something similar to git-merge-base. You would probably have to hack through and extract out some of the internals of Hg.

3 Responses to “Create custom commands in Git”

  1. Making Resolving Conflicts in Git Easier | The Tomes Of Experience Says:

    [...] i knocked up a quick bash script which makes use of git’s ability to create extensions for git commands. This script issues a rebase (i also have one for merge) and will fire off my preferred editor for [...]

  2. Dave Abrahams Says:

    Seems to me that the ability to write git-merge-base has less to do with git's cute command-extensibility feature than with its exposure of plumbing at a command-line level. Did I miss something?

  3. nullstyle Says:

    Setting aside the fact that I wrote this roughly 3 years ago in Git's comparative infancy; The point I believe I was trying to make is that git's 'cute' command extensibility is aided greatly by the exposure of its plumbing.

Leave a Reply