Anyone else have the bad habit of typing in git statsu
or git stauts
? Or maybe you just end up typing git status
all the time at the command line, and wonder if there was a better way. Well, there is.
Aliases
Most command-line shells have the concept of aliasing. This means you type one thing, and it represents an alternative way to call another command, maybe with implicit parameters baked in that you use all the time. So for me, I want to type gs
to perform a git status
call.
PowerShell
As a .Net developer, I use PowerShell as my preferred command line shell. This means when it comes to git, I have PowerShell set up just so, and it can give me all these nice indicators around what branch I’m on etc. But when It comes to aliases, PowerShell requires a native command to attach that alias to (of which git status
isn’t). It also doesn’t allow default arguments when you make these aliases. But it can be done. The trick is to wrap it in a function:
function Get-GitStatus { & git status }
New-Alias -Name gs -Value Get-GitStatus
First we define a function that represents the command we want to execute (git status
in this case), then we call the New-Alias PowerShell command, and provide a name (gs
) and the defined function as the value. Easy.
As a convenience, I added these two lines to my Microsoft.PowerShell_profile.ps1
file (you can find this using the $PROFILE
variable in your PowerShell) so that the alias is always available when PowerShell starts.
BASH
While Bash is not my primary shell, it is the go-to for most developers using. It is easy to get an alias set up in Bash. All you would need is to run the following command:
alias gs='git status'
Now whenever you type gs
it will run the git status
command. Again, you could put this in your .bashrc file (or windows equivalent to make this available whenever you launch the shell.
TIL
So I was watching someone else give an introduction to git today, and found out that for some commands in git have built in, well established aliases already. one of these is the status
command, using st
. So if git status
is giving you trouble, maybe just look at using git st
instead. No custom aliasing required. I’m still going to keep using my gs
though, its still faster.