Aliasing Git Commands in Powershell

1 minute read

The git commandline tooling is awesome. I absolutely love it - it’s just about the only way I interact with git. However, have you ever noticed that you frequently type the exact same commands for git day in and day out? Wouldn’t it be nice if you could type fewer keystrokes, or had templates for the common commands that for some reason you generally mistype (I’m looking at you --amend).

What if instead you could type gcm "message" instead of git commit -m "message" or gm instead of git checkout main && git pull ? Would that be worth something to you?

Well, Good News, Everyone! We can. These instructions will be for Powershell, but they are generally portable to any shell environment.

Step 1: Open your Profile.ps1

code $profile

Step 2: Remove default commands that you likely never used anyway

# Remove Defaults
rename-item alias:\gc gk -force
rename-item alias:\gcm gkm -force
rename-item alias:\gl gll -force
rename-item alias:\gsn gsnn -force
rename-item alias:\gm gmm -force

Step 3: Create wrapper functions and the shortened aliases

# Git
function git-status { git status }
Set-Alias -Name gs -Value git-status

function git-addall { git add -A }
Set-Alias -Name gaa -Value git-addall

function git-branch { git checkout -b $args }
Set-Alias -Name gb -Value git-branch
function git-commit-m { git commit -m $args }
Set-Alias -Name gcm -Value git-commit-m

function git-commit-a { git commit --amend }
Set-Alias -Name gca -Value git-commit-a

function git-checkout { git checkout $args }
Set-Alias -Name gco -Value git-checkout

function git-log { git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit }
Set-Alias -Name gl -Value git-log

function git-main { git checkout main && git pull }
Set-Alias -Name gm -Value git-main

Step 4: Save your file and reload your profile. You’re done.

I will admit it will take a little bit of time to adjust to using these new aliases, however, once you do you’ll wonder how you ever lived without them.

Leave a Comment