Creating aliases in PowerShell for git commands?

GitPowershell

Git Problem Overview


I understand how to create aliases in PowerShell for cmdlets fine, but I want to create an alias in PowerShell for things like git status as just gs, and git pull origin master as gpm. Can anyone point me in the right direction?

Git Solutions


Solution 1 - Git

You will have to create a function first, that has your command in it. Then create an alias to that function.

PS C:\Users\jpogran\code\git\scripts> function get-gitstatus { git status }

PS C:\Users\jpogran\code\git\scripts> get-gitstatus
# On branch master
nothing to commit (working directory clean)

PS C:\Users\jpogran\code\git\scripts> Set-Alias -Name gs -Value get-gitstatus

PS C:\Users\jpogran\code\git\scripts> gs
# On branch master
nothing to commit (working directory clean)

You might also be interested in the OS project called posh-git that aims to provide a powershell environment for git commands. Wraps git commands with PS type functions and also provides a new prompt that shows the status and branch in your prompt.

EDIT: Forgot to add how to find out how to do this using Powershell.

PS C:\Users\jpogran\code\git\scripts> get-help set-alias -examples

This will show you examples (the last one applies here) of how to use set-alias to create aliases to commands with paramaters, pipelines, etc.

Solution 2 - Git

Just created some shortcuts for myself and wanted to share:

Create a PowerShell profile (if you don't already have one):

New-Item -Type file -Path $PROFILE -Force

Open it to edit:

notepad $PROFILE

Add the following functions and aliases:

function Get-GitStatus { & git status $args }
New-Alias -Name s -Value Get-GitStatus
function Set-GitCommit { & git commit -am $args }
New-Alias -Name c -Value Set-GitCommit

When you restart your PowerShell session, you should be able to pass arguments to the aliases as well. e.g.:

c "This is a commit message"

Update:

Here are some more of my frequently-used shortcuts:

function Get-GitStatus { & git status -sb $args }
New-Alias -Name s -Value Get-GitStatus -Force -Option AllScope
function Get-GitCommit { & git commit -ev $args }
New-Alias -Name c -Value Get-GitCommit -Force -Option AllScope
function Get-GitAdd { & git add --all $args }
New-Alias -Name ga -Value Get-GitAdd -Force -Option AllScope
function Get-GitTree { & git log --graph --oneline --decorate $args }
New-Alias -Name t -Value Get-GitTree -Force -Option AllScope
function Get-GitPush { & git push $args }
New-Alias -Name gps -Value Get-GitPush -Force -Option AllScope
function Get-GitPull { & git pull $args }
New-Alias -Name gpl -Value Get-GitPull -Force -Option AllScope
function Get-GitFetch { & git fetch $args }
New-Alias -Name f -Value Get-GitFetch -Force -Option AllScope
function Get-GitCheckout { & git checkout $args }
New-Alias -Name co -Value Get-GitCheckout -Force -Option AllScope
function Get-GitBranch { & git branch $args }
New-Alias -Name b -Value Get-GitBranch -Force -Option AllScope
function Get-GitRemote { & git remote -v $args }
New-Alias -Name r -Value Get-GitRemote -Force -Option AllScope

Solution 3 - Git

I don't know PowerShell, but you can setup aliases directly in Git.

Solution 4 - Git

I created posh-git-alias which you can just add to your PowerShell $PROFILE.

Solution 5 - Git

You need to create a profile.ps1 file put it in a folder call WindowsPowerShell in my documents

Then put in profile.ps1 a line like this:

set-alias wit 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\witadmin.exe'

Solution 6 - Git

On Windows, I use the following content in a .gitconfig file located in the user home folder:

[alias]
  co = checkout
  ci = commit
  st = status
  sw = switch
  d  = diff
  hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short
  type = cat-file -t
  dump = cat-file -p

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRichardView Question on Stackoverflow
Solution 1 - GitJames PogranView Answer on Stackoverflow
Solution 2 - GitAaron TribouView Answer on Stackoverflow
Solution 3 - GitnicoulajView Answer on Stackoverflow
Solution 4 - GitAlexander ZeitlerView Answer on Stackoverflow
Solution 5 - GitAlexView Answer on Stackoverflow
Solution 6 - GitmanasouzaView Answer on Stackoverflow