Using Git Aliases to Speed Up Workflow
Summary: Create custom Git commands using aliases.
Efficiently managing source code is pivotal for developers, and Git is the industry-standard tool for version control. But typing long or repetitive Git commands can slow down your workflow. What if you could compress these verbose commands into short, easy-to-remember shortcuts? That’s where Git aliases come in.
In this article, we’ll explore how to create and manage Git aliases to help you speed up your workflow and keep your command line experience productive.
What are Git Aliases?
Git aliases are custom shortcuts for Git commands. They allow you to substitute long or frequently used commands with shorter versions, reducing typing time and minimizing errors. Once set, these aliases can be invoked as if they were actual Git commands.
For example, instead of typing:
git status
You could simply type:
git st
Why Use Git Aliases?
- Speed: Save time by shortening commands.
- Consistency: Create standardized shortcuts for your team.
- Simplicity: Reduce complexity in your workflow.
- Customization: Tailor Git to your workflow preferences.
How to Create Git Aliases
Aliases are defined in your Git configuration file. You can set them either globally (affecting all your repositories) or locally (per project).
Setting a Global Git Alias
Use the following syntax:
git config --global alias.<shortcut> '<git-command>'
Example:
git config --global alias.st status
Now, running git st
will display your repository’s status.
Setting a Local Git Alias
If you want an alias only for a specific repository (not global):
git config alias.<shortcut> '<git-command>'
Useful Git Aliases
Here’s a set of popular and handy aliases you can add to your configuration:
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.last 'log -1 HEAD'
git config --global alias.unstage 'reset HEAD --'
git config --global alias.hist 'log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short'
Let’s break down a few:
git co
: Shortcut forgit checkout
git br
: Shortcut forgit branch
git ci
: Shortcut forgit commit
git last
: Shows the last commitgit hist
: Displays a graphical, formatted log of commits
Aliases with Arguments
You can create aliases that accept parameters by adding the !
character and writing the command as a shell command.
Example: Reverting files with an alias
git config --global alias.rv '!git revert "$@"'
Now, git rv <commit>
will work just like git revert <commit>
.
Viewing and Editing Your Aliases
To list all your existing aliases, run:
git config --get-regexp alias
To edit your global Git configuration and tweak aliases manually:
git config --global --edit
This will open your configuration file in your default editor.
Removing a Git Alias
To delete an alias:
git config --global --unset alias.<shortcut>
For example:
git config --global --unset alias.rv
Pro Tips
- Prefix advanced or potentially destructive commands with
!
(exclamation), which tells Git to execute the following command in the shell. - Aliases can chain multiple Git commands together, e.g., for cleanup or deployment steps.
- Sync aliases across machines by copying your
~/.gitconfig
file.
Conclusion
Git aliases are a powerful way to streamline your version control workflow. By crafting your own set of shortcuts, you’ll spend less time typing and more time coding. Start with the basics, experiment with more advanced aliases, and share them with your team for improved productivity.
Ready to supercharge your Git workflow? Try adding your first alias today!
Further Reading:
Happy Gitting!