DEV Community

Cover image for Generating shorter aliases from .gitconfig file
Konstantin
Konstantin

Posted on • Edited on

2 1

Generating shorter aliases from .gitconfig file

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.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay