Git Aliases


Photo by Peppy Toad on Unsplash

Are you constantly repeating the same Git command or sequence of commands? Do you wish that it would be faster and easier to type those commands? With Git Aliases you can do it.

In this short blog post, I’ll introduce Git Aliases, show you how to configure them, and give you a list of my commonly used aliases.

What are Git Aliases?

Git aliases allow you to reduce the amount of text you write when trying to execute certain commands or a chain of commands. For example, imagine you are doing you are running the push —force-with-lease command. This can get really repetitive sometimes. With Git aliases, you can create an alias for this command, like pfwl, and reduce the amount of text you write every time.

Configuring Git Aliases

Git Aliases can be added in two ways.

Adding aliases by the console

On your console, you can type git config --global followed by alias.<aliasToAdd> <command to add alias to>

For creating an alias to the push --force-with-lease command, this would translate to the following:

git config --global alias.pfwl 'push --force-with-lease'

After running this line in your console, now you can run git pfwl.

Adding aliases by editing your .gitconfig

By opening your .gitconfig file and adding it a [alias] section, you can define all your aliases there.

For creating an alias to the push --force-with-lease command, you need to open your .gitconfig file and add the following:

[alias]
	pfwl = push --force-with-lease

After saving, you should be able to run your new command.

My aliases

Here’s a dump of my current .gitconfig aliases

[alias]
	pfwl = push --force-with-lease
	br = branch
	st = status
	ci = commit
	co = checkout
	last = log -1 HEAD
	logg = log --oneline --graph --decorate
	undo = reset --hard HEAD~1
	aliases = !git config -l | grep alias | cut -c 7-
	cl = clone
	fwl = --force-with-lease
	rbm = rebase master
	rbim = rebase -i master
	cm = commit -m
	cob = checkout -b
	acm = !git add . && git commit -m
	amend = !git add -A && git commit --amend --no-edit
	rba = rebase --abort
	rbc = rebase --continue
	rb = rebase
	rbi = rebase -i

Most of them should be simple to understand and self-explanatory.

Final considerations

As you have been able to see, Git Aliases are simple to configure and improve your experience when using Git.

I hope you enjoyed this blog post and, stay tuned for the next ones!