If you’re a programmer these days, you probably spend a large part of your day in git. If you’re a command line zealot like me, you realize the holy ways of using your Terminal app for everything and aren’t seduced by fancy GUIs that only stand to dissuade you from pure unbridled productivity.

With that in mind, one typically finds themselves in a position where they have a few files that have changed liked so:

git showing changed files

If I had to add 2 out of those 3 files1, typing the commands start to get a little tedious:

:> git add images/content/git-0.png
:> git add images/content/git-1.png

Sure, if you happen to use fish (one of the nicest shells out there) the autocompletion helps reduce the tediousness. But it still doesn’t beat being able to add files just by a number! What if you were presented with a git status like this:

git-number showing changed files

and then simply added the files by their reference number, like so:

:> g add 4
:> g add 5

Wouldn’t that be better? I’ve been doing this for years and trust me, it is! You probably won’t see the difference with just a few files, but if you had 10 images being commited, then being able to simply type a range makes it a breeze to commit a bunch of files!

:> g add 4-10

Installation

So, how do you achieve something similar? Install git-number and 💥:

:> brew install git-number
:> git-number -s

For years, I used a bunch of hand wrangled ruby files, that would help me achieve something similar but I discovered @holygeek already went through the trouble of building a much more mature version of this in Perl. Now I’m just using that version (and have gladly deleted all those scripts I had to grudgingly maintain).

Notes

Remember to git-number first

It’s important to make sure you run git-number first before running your git add <numeral> commands. I’ve noticed that git-number gets a little confused and tends to try issue commands on the wrong files from previous runs, if you don’t (yikes).

You probably want to run git-number anyway to see the status of changed files. This replaces git status which you probably would have otherwise run to see what’s changed.

Works for most git commands

I demostrated the ease of using numbers with git add but git-number isn’t limited to just add. It also works for most other git commands where you have to reference a filename like checkout, reset etc.

Aliases and conveniences

The keenly observant would notice in the examples above that I use g add <number> instead of git-number add <number>. This is because I almost always want to use git-number instead of git. So I have an alias for conveniences:

alias g = git-number

The good thing with git-number is that it works even if i’m not referencing a file number, so this will technically also work:

:> git-number commit -m "This is a diligently formed commit 😇"

But what about my auto-completions?

The only trouble with replacing git with git-number altogether, is you might lose your auto-completions:

:> git-number checkout kg/feat/myBr<Tab>

Typically when you hit the tab key (where it says <Tab> above) you would expect the different branch names to be autocompleted but since most shells are configured to autocomplete only if when they see a “git” command being used, this can pose a problem.

If you’re using fish 2 though, this is easily possible if you use a function3 :

function g \
  --description "abbreviation for git-number w/auto completion" \
  --wraps git
    switch $argv[1]
    case "reb*"
        git $argv
    case "*"
        git number $argv
    end
end

Now you can go about your merry way, using g as your git command alias, have autocompletion and use git-number to boot.

Give git-number a try. You won’t regret it.


  1. be a good git citizen and make meaningful commits ↩︎

  2. and really why aren’t you? it’s the best shell out there ↩︎

  3. git-number isn’t happy playing with commands like rebase though, so best to avoid them ↩︎