How to Use Git Aliases for Faster Commands
Use
git config --global alias.name 'command' to create a shortcut for a git command. Then run git name instead of the full command to save time and typing.Syntax
The basic syntax to create a git alias is:
git config --global alias.<alias-name> '<git-command>'--globalmakes the alias available for all repositories on your computer.alias.<alias-name>is the shortcut you want to use.'<git-command>'is the full git command you want to shorten.
bash
git config --global alias.co 'checkout'Example
This example creates an alias co for checkout. After setting it, you can use git co branch-name instead of git checkout branch-name.
bash
git config --global alias.co 'checkout'
git co mainOutput
Switched to branch 'main'
Common Pitfalls
Common mistakes include:
- Not using quotes around the command, which can cause errors.
- Trying to alias commands that require arguments without including them properly.
- Forgetting to use
--globalif you want the alias everywhere.
Always test your alias after creating it.
bash
git config --global alias.st 'status' # Correct usage: git st # Wrong: missing quotes # git config --global alias.st status # This works but if command has spaces, quotes are needed # Correct for complex commands: git config --global alias.lg "log --oneline --graph --decorate"
Quick Reference
| Alias Command | Description | Example Usage |
|---|---|---|
| co | Shortcut for checkout | git co branch-name |
| st | Shortcut for status | git st |
| lg | Shortcut for detailed log | git lg |
| ci | Shortcut for commit | git ci -m 'message' |
Key Takeaways
Create git aliases with 'git config --global alias.name "command"' to save typing.
Use quotes around commands especially if they have spaces or options.
Test aliases immediately to ensure they work as expected.
Global aliases apply to all repositories on your machine.
Aliases can simplify complex or frequently used git commands.