“git is not a git command”

By on

It probably happens to me once or twice a week. I try to get the current repository’s status, the log, or the contents of some specific commit when I’m greeted with a familiar error message.

$ git git status
git: 'git' is not a git command. See 'git --help'.

The most similar command is
        init

It’s self-explanitory enough for me to understand what happened immediately. I typed "git" in my terminal. Then I got distracted. I needed to look something up, think about which message I was going to use, or go out to lunch. When I come back I instinctively type "git status", forgetting the first part of the command was already there.

I pause while git’s belittling tone rings in my ears.

'git' is not a git command.

Defeated, I press , hold while double-tapping , then four times and , to get the result I wanted.

$ git status
On branch master
nothing to commit, working tree clean

Enough

$ git config --global alias.git '!git'

Running the git config --global alias.git '!git'-command will write the following alias to your ~/.gitconfig file:

[alias]
        git = !git

By adding git as a subcommand alias, a command is run whenever you execute git git. Using an exclamation mark to the beginning of the alias' value, git will run any command instead of just it’s own subcommands. In this case it’s aliased to !git, meaning git git will be converted to git.

Using this alias, git git status will now automatically convert to git status. Due to the recusive nature of this, the alias will turn git git git status into git git status, which will end up executing git status.

$ git git status
On branch master
nothing to commit, working tree clean

Now, git is a git command.


Any questions, feedback or suggestions? Please respond on Twitter (or via direct message) or send an e-mail.