Tip of the day: Embed Git commands directly into the shell
June 21, 2010
Git has an aliasing feature that allows you to define aliases and shortcuts for Git commands. For example, the command
git config --global alias.st status
will setup “git st” as a shorthand for “git status”. To delete the alias, use
git config --unset --global alias.st
But that’s not the point of this post. Recently, I have become tired of typing the four letters “git ” at the beginning of each and every Git command. At least for often-used commands which I have abbreviated already with Git aliases, I want to be able to omit the “git “. Of course there’s a shell script to solve my problem.
while read git_line; do
if echo $git_line | grep '^\[.*\]$' &>/dev/null; then
echo $git_category $git_line
done < $HOME/.gitconfig | grep '^\[alias\]' | cut -d' ' -f2- | sed 's/ = / /' | while read git_alias git_command; do
alias $git_alias="git $git_alias"
unset git_line git_category git_alias git_command
Put this into your .bashrc (or equivalent). Please note that the above script was only tested with zsh, but I do not see anything that should not work in bash as well.
Now what does this do? For every global Git alias (like “git st” for “git status”), it defines the alias as a command recognized by the shell, so you need to type only “st” instead of “git status”. Git aliases can also resolve to the same command without entering an infinite loop, so you can define “pull” as an alias for “pull”, and use “pull” as a command on the shell.