How to Create Git Alias: Simple Commands and Examples
You can create a git alias by running
git config --global alias. '' . This lets you use a short name instead of typing the full git command every time.Syntax
The basic syntax to create a git alias is:
git config --global alias.<alias-name> '<git-command>'
Here:
- --global means the alias works for your user on all repositories.
- alias.<alias-name> is the new short name you want to use.
- '<git-command>' is the full git command you want to shorten, enclosed in quotes.
bash
git config --global alias.co 'checkout'Example
This example creates an alias co for checkout. After this, you can type git co instead of git checkout.
bash
git config --global alias.co 'checkout'
git co mainOutput
Switched to branch 'main'
Common Pitfalls
Common mistakes include:
- Not using quotes around the full git command, which can cause errors.
- Forgetting
--globalif you want the alias available in all repos. - Using complex commands without proper escaping.
Example of wrong and right way:
bash
git config --global alias.st 'status'
# Correct usage
git config --global alias.st status
# Wrong usage without quotes (may cause error):
git config --global alias.st statusQuick Reference
| Command | Description |
|---|---|
| git config --global alias.co 'checkout' | Alias 'co' for 'checkout' |
| git config --global alias.st 'status' | Alias 'st' for 'status' |
| git config --global alias.br 'branch' | Alias 'br' for 'branch' |
| git config --global alias.cm 'commit' | Alias 'cm' for 'commit' |
Key Takeaways
Use
git config --global alias.<name> '<command>' to create a git alias.Always put the full git command in quotes to avoid errors.
Add
--global to make the alias available in all your repositories.Test your alias by running
git <alias-name> after creating it.Keep aliases short and easy to remember for faster git usage.