Create Shortcuts in Git Using Aliases
Creating shortcuts in Git using aliases can make your Git workflow easier by adding new custom commands. Here’s how:
Open your Git configuration file
In your terminal, type git config --global -e
and press Enter. This command opens the global Git configuration file in your default text editor.
Add an alias
- In the configuration file, look for a section labeled
[alias]
. If it doesn't exist, you can create it by adding[alias]
on a new line. - Below
[alias]
, add your alias like this:co = git checkout
. Here,co
is the shortcut for thegit checkout
command. - After adding all your aliases, save the changes and close the editor.
Now, instead of typing the full command git checkout
, you can simply type git co
. You can create as many aliases as you like by following the same steps.
You can also save it directly from the terminal by running:
git config --global alias.save "your shortcut"
So as a complete example, creating pretty logs as a shortcut:
git config --global alias.lg "log --graph --pretty=format:'%C(red)%h%C(reset) - %C(yellow)%d%C(reset) %s %C(green)(%cr) %C(bold blue)<%an>%C(reset)' --abbrev-commit"
Now you can simply run git lg
.
Pro tip
By using !
at the start of the alias command, you tell Git to run the command in the shell, which allows you to chain multiple Git commands or even mix in other shell commands. It looks something like this:
git config --global alias.save "!git status && git add -A && git commit -m 'chore: commit save point'"
If you've created some useful shortcuts, let me know in the comments. 👀