In previous articles I showed how to configure aliases
in your ~/.gitconfig
file.
As you might have noticed you need to type git alias
which might be inconvenient. Even if you make an alias g=git
you have to remember to type space between g
and alias
.
To make things easier we can automatically generate shell aliases from the .gitconfig
file:
# generate aliases from gitconfig aliases | |
function generateAliasesFromGit() { | |
git config --get-regexp ^alias\\. | while read gitAlias; do | |
local aliasName=$(echo $gitAlias | sed -E 's/^alias\.([a-z_\-]*).*/\1/') | |
# alias only if there is no collision | |
if ! type "g$aliasName" >/dev/null 2>&1; then | |
alias g$aliasName="git $aliasName" | |
fi | |
done | |
} | |
generateAliasesFromGit |
here we grep all the git
config values from alias
section and get the alias
name with sed
. Then we check that alias g
+ alias name doesn't already exist and set it.
Now, if you had for instance alias a = add
you can type both g a
and ga
.
That's it.
Top comments (0)