Git Aliases
Git aliases are a handy feature of Git that allows you to create shortcuts for commonly used commands. Once you start using them, you will never want to go back to typing the full commands again.
Create Aliases
The command to create a Git alias is as follows:
git config --global alias.<shortcut> <command>
Let’s create aliases for the most commonly used Git commands:
git config --global alias.ci "commit"
git config --global alias.co "checkout"
git config --global alias.st "status"
git config --global alias.br "branch"
git config --global alias.log "log --oneline --graph --decorate --all"
Use Aliases
Now instead of typing the full commands, you can use the aliases in your daily work:
Git Commit
git ci -m "Commit message"
Git Checkout
git co <branch-name>
Git Status
git st
Git Branch
git br
Git Log
git log
Global Alias
We just set up shortcuts for the most commonly used Git commands. But we can also set up an alias for the Git itself. This isn’t a Git feature, rather it is a shell feature.
PowerShell
Open your profile configuration file:
notepad $PROFILE
Then add the following line:
New-Alias -Name g -Value "git"
Save the file and restart your terminal.
Bash / Zsh
Open your profile configuration file:
nano ~/.bashrc or ~/.zshrc
Then add the following line:
alias g="git"
Save the file and restart your terminal.
Use Global Alias
Now you can use the g
command followed by a Git command alias.
g ci -m "Commit message"
Just for reference, if you hadn’t set this up, you’d be typing this instead:
git commit -m "Commit message"
We saved 6 whole letters’ worth of typing time per commit!