How to create aliases to use Git CLI more

Jeremie LitzlerDecember 28, 2021Less than 1 minute

What are Git aliases

Have you ever wondered if you could skip typing git add /path/to/filename && git commit "my message" && git push? That's what git aliases are for.

How to create an alias

Using the git config command, we can declare globally the aliases. For example:

git config --global alias.todo "! git grep --extended-regexp -I --line-number --break --heading --color=auto 'TODO|FIXME'"

What are example of aliases that can be helpful

My aliases

Add via direct git config edition

git config --global --edit

Add via CLI

## List existing aliases
git config --global alias.alias "! git config --get-regexp alias"
## Hard reset
git config --global "! git reset --hard HEAD"
## Soft reset
git config --global "! git reset --soft HEAD^"
## Hard reset and pull
# Can't add the following.
# Maybe edit the git config file directly as the alias should work...
# Maybe an escape issue?
git config --global alias.hrp "!f() { git hr && git pull }; f"

Articles I read while researching the topic in new window in new window in new window in new window in new window