How to create aliases to use Git CLI more

Jeremie LitzlerAbout 1 minWeb developmentGitCLI

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"

What are my aliases

	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 in new window in new window in new window in new window in new window