Photo by Praveen Thirumurugan on Unsplash
Git is a powerful version control system that is widely used by developers around the world. While it has a robust set of command-line tools, it can sometimes be time-consuming to type out long commands every time you want to perform a specific task. Fortunately, Git allows you to create custom shortcuts and aliases that can make your workflow more efficient.
There are two main ways to create custom Git shortcuts:
- through the .gitconfig file and
- through your shell’s configuration file (e.g. .bashrc, .zshrc).
Let’s now take a look at how to create custom Git shortcuts in each of these files.
Long command vs alias
Same git move, fewer keystrokes
Win: .gitconfig aliases and shell shortcuts cut the slog without changing git itself.
Creating Git Shortcuts in .gitconfig
The .gitconfig file is used to store settings for your Git configuration. You can use it to create aliases for Git commands.
To create a new alias, open your .gitconfig file (located in your home directory or in your repo’s root) in a text editor and add a new alias entry. For example, to create an alias for the git log command, you could add the following:
[alias]
ac = !git add -A && git commit -m
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
This creates an alias called lg that will execute the git log command with the specified options. To use this alias, simply type git lg instead of git log with all those options.
You can create as many aliases as you like in your .gitconfig file. Some of the common ones are co for checkout, br for branch, and st for status.
You can even combine multiple commands, like the first one.
Creating Git Shortcuts in Your Shell Configuration File
In addition to creating aliases in your .gitconfig file, you can also create custom shortcuts in your shell’s configuration file (e.g. .bashrc, .zshrc). These shortcuts will be available every time you open a new terminal window.
To create a new shortcut, open your shell’s configuration file in a text editor and add a new alias entry.
For example, to create a shortcut for the git log command, you could add the following line:
alias gl='git log'
This creates an alias called gl that will execute the git log command when you type gl in the terminal. You can even add the above mentioned formatting options if you like.
You can create as many shortcuts as you like in your shell’s configuration file. I prefer to do it this way and here are some shortcuts that I use on a daily basis.
alias gs='git status'
alias gd='git diff'
alias gb='git branch'
alias gchm='git checkout master'
alias grbm='git pull origin master --rebase'
alias gch='git checkout'
alias gchb='git checkout -b'
alias gac='git add . && git commit -m'
alias gpl='git pull origin'
alias gps='git push origin'
alias gpsf='git push origin --force'
Wrapping Up
Creating custom Git shortcuts can save you a lot of time and typing when working with Git. Whether you prefer to create aliases in your .gitconfig file or in your shell’s configuration file, the process is quick and easy, and it can greatly improve your workflow.
Try it out and let me know what you think.
Have fun!
Level Up Coding
Thanks for being a part of our community! Before you go:
- 👏 Clap for the story and follow the author 👉
- 📰 View more content in the Level Up Coding publication
- 💰 Free coding interview course ⇒ View Course
- 🔔 Follow us: Twitter | LinkedIn | Newsletter
🚀👉 ****Join the Level Up talent collective and find an amazing job
Originally published on Medium.