Git Alias – Create Shortcuts for Common Git Commands
Introduction – Why Git Aliases Save Time
Tired of typing git commit -m or git status over and over? Git aliases let you create shortcuts for long or frequently used Git commands, helping you work faster and smarter. It’s a simple way to boost productivity, especially for repetitive tasks.
In this guide, you’ll learn:
- How to create and manage Git aliases
- Examples of useful built-in and custom Git shortcuts
- Best practices and advanced aliasing tricks
What Is a Git Alias?
A Git alias is a custom shortcut that maps to an existing Git command (or even a chain of commands). Rather than typing out the full command every time, you can use your alias instead.
Think of it like a personal nickname for Git commands.
How to Create a Git Alias (Syntax & Examples)
You use the git config command to define an alias. Here’s the basic syntax:
git config --global alias.<shortcut> "<git-command>"
Examples:
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
Now:
git st=git statusgit co=git checkoutgit br=git branchgit ci=git commit
Most Commonly Used Git Aliases
Here are some powerful and popular Git aliases developers use daily:
| Alias | Expands To | Description |
|---|---|---|
st | status | Shows current working tree status |
ci | commit | Shortcut for committing changes |
co | checkout | Switch branches or restore files |
br | branch | List, create, delete branches |
lg | log --oneline --graph --decorate | Pretty Git log visualization |
unstage | reset HEAD -- | Unstage a file |
last | log -1 HEAD | Show last commit |
Creating Git Aliases for Custom Scripts
Aliases aren’t limited to single commands—you can include full shell commands.
Example: Open a Git log in a graphical format:
git config --global alias.graph "log --oneline --all --graph --decorate"
Or alias for amending a commit:
git config --global alias.amend "commit --amend --no-edit"
Use quotes when defining multi-part commands.
Global vs Local Git Aliases
| Scope | Command | Applies To |
|---|---|---|
| Global | git config --global alias.st status | All repositories |
| Local | git config alias.st status | Current repository |
If you’re working on a shared project and want consistent aliases, define them locally within the project.
Advanced Git Aliases with Parameters
Git aliases can accept additional arguments using shell escaping:
git config --global alias.undo '!git reset --soft HEAD~1'
- The
!tells Git to run the command in the shell. - This alias lets you undo the last commit but keep your changes staged.
Another example:
git config --global alias.unstage '!git restore --staged $1'
To unstage a file:
git unstage filename.txt
Common Issues with Git Aliases
- Alias conflicts: Avoid naming aliases that shadow real Git commands (like
git init,git fetch). - No shell commands without
!: Shell-based aliases (scripts, pipes,$1) require a leading!. - Misconfigured aliases: Forgetting quotes around multi-word commands causes alias errors.
Best Practices for Using Git Aliases
- Use short, memorable abbreviations.
- Create descriptive aliases for advanced tasks (
git cleanup,git squash). - Store and share your alias list in your dotfiles or documentation.
- Test complex aliases thoroughly before using in production workflows.
GUI Alternatives for Managing Git Aliases
Some Git GUI tools allow you to manage aliases or command shortcuts:
- GitKraken
- Sourcetree
- GitHub CLI (gh) — allows command chaining with ease
For VS Code users, you can add terminal snippets or create .bash_aliases for even more control.
Summary Table – Git Alias Examples
| Shortcut | Full Command | Use Case |
|---|---|---|
st | git status | Check repo status |
co | git checkout | Switch branches |
br | git branch | Manage branches |
ci | git commit | Save changes |
lg | git log --oneline --graph --decorate | Visualize commit history |
undo | git reset --soft HEAD~1 | Undo last commit (keep changes) |
unstage | git reset HEAD -- | Unstage file |
amend | git commit --amend --no-edit | Modify last commit without edit |
Summary – Git Alias
Git aliases transform how you interact with Git—faster commands, fewer keystrokes, more productivity. Whether you’re a beginner or Git pro, aliases streamline common tasks and empower power-user workflows.
Key Takeaways:
- Use
git config --global alias.<name>to define aliases - Create aliases for frequent or complex commands
- Share your alias config with your team or dotfiles
Real-World Relevance: In large teams and CI/CD pipelines, consistent use of aliases reduces friction and improves Git fluency.
Git Alias — Frequently Asked Questions (FAQs)
How do I list all my Git aliases?
Use:
git config --get-regexp ^alias\.
Where are Git aliases stored?
They’re saved in your Git config file:
- Global:
~/.gitconfig - Local:
.git/config
How do I delete a Git alias?
Use:
git config --global --unset alias.st
Can Git aliases run shell commands?
Yes, prefix the alias with !:
git config --global alias.cleanup '!git reset --hard && git clean -fd'
What’s the difference between Git and shell aliases?
Git aliases only work with git commands, while shell aliases (e.g., in .bashrc) can alias anything in the terminal.
How do I back up my Git aliases?
Export your aliases with:
git config --global --get-regexp ^alias\. > git-aliases.txt
You can re-import them with a shell script later.
Share Now :
