Introduction
Git is a powerful version control system, but typing long commands repeatedly can slow you down. Fortunately, Git allows you to create custom shortcuts (aliases) using git config --global alias
. These aliases help streamline your workflow, reduce typing errors, and boost productivity.
In this guide, we'll explore how to create Git aliases, common use cases, and some pro tips to make the most of them.
How to Use Git Aliases
Git aliases are configured using the git config
command. You can set them globally (for all repositories) or locally (for a single repository).
Basic Syntax
git config --global alias.<shortcut> "<full-command>"
Example: Shortening git status
git config --global alias.st "status"
Now, instead of git status
, you can simply run:
git st
Common Use Cases for Git Aliases
1. Shortening Frequently Used Commands
Instead of typing long commands, create short aliases:
git config --global alias.co "checkout"
git config --global alias.br "branch"
git config --global alias.ci "commit"
Now, you can use:
-
git co
instead ofgit checkout
-
git br
instead ofgit branch
-
git ci
instead ofgit commit
2. Creating Advanced Aliases
You can combine multiple Git commands into a single alias:
git config --global alias.ac "!git add -A && git commit -m"
Usage:
git ac "Your commit message"
This adds all changes and commits them in one step.
3. Log Formatting
The default git log
can be verbose. Simplify it with:
git config --global alias.lg "log --oneline --graph --decorate --all"
Now, git lg
gives a clean, condensed history.
4. Undoing Changes
Create shortcuts for common undo operations:
git config --global alias.unstage "reset HEAD --"
git config --global alias.undo-commit "reset --soft HEAD~1"
Usage:
-
git unstage file.txt
(unstages a file) -
git undo-commit
(undoes the last commit while keeping changes)
Tips and Tricks
1. Using External Commands in Aliases
Prefix with !
to run shell commands:
git config --global alias.hello "!echo 'Hello, Git!'"
Running git hello
prints:
Hello, Git!
2. Listing All Aliases
To see all configured aliases, run:
git config --global --list | grep alias
3. Editing Aliases Directly in .gitconfig
Open your global Git config file:
git config --global --edit
Then add aliases under the [alias]
section:
[alias]
st = status
co = checkout
lg = log --oneline --graph --decorate --all
4. Overriding Existing Commands
You can even override default Git commands (use with caution!):
git config --global alias.log "log --pretty=format:'%h - %an, %ar : %s'"
Now, git log
uses your custom format.
Conclusion
Git aliases (git config --global alias
) are a game-changer for developers. They save time, reduce repetitive typing, and make complex commands easier to remember. Whether you're shortening basic commands or creating advanced workflow shortcuts, aliases help you work more efficiently.
Want to know some cool aliases I created for automating my workflow? Checkout here
Up Next in the Series: git grep
– Search for text across commits/branches
Daily advance GIT tips in your inbox---worth starting? Respond to my poll here🚀
For more useful and innovative tips and tricks, Let's connect on Medium