How to create aliases to use Git CLI more

Jeremie Litzler
  • Web development
  • Git
  • CLI
About 1 min

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 alias.hr "! git reset --hard HEAD"
## Soft reset
git config --global alias.sr "! 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"

What are my aliases

[alias]
	conf = ! notepad /c/Users/Jeremiel/.gitconfig
	ac = ! git add -A && git commit -m
	acc = ! git add $1 && git commit -m "$2"
	alias = ! git config --get-regexp ^alias\\. | sed -e s/^alias\\.// -e s/\\ /\\ =\\ /
	sr = ! git reset --soft HEAD^
	hr = ! git reset --hard HEAD
	ceb = ! git checkout -t
	cb = ! git checkout -b
	rpm = ! git hr && git checkout main && git pull
	mm = ! git merge main
  #For VuePress publishing
	pub = ! npm run docs:build && git push
	dev = ! npm run docs:dev
	build = ! npm run docs:build

Articles I read while researching the topic

https://bitbucket.org/durdn/cfg/src/master/.gitconfig?at=masteropen in new window

https://www.durdn.com/blog/2012/11/22/must-have-git-aliases-advanced-examples/open in new window

https://www.atlassian.com/blog/git/advanced-git-aliasesopen in new window

https://stackoverflow.com/questions/3321492/git-alias-with-positional-parametersopen in new window

https://borntocode.fr/git-alias-etre-un-bon-developpeur-faineant/open in new window