83

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?

9 Answers 9

95

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

Aligns with mac aliases commands:

function Get-GitCheckout { & git checkout $args }
New-Alias -Name gco -Value Get-GitCheckout
function Get-GitBranch { & git branch $args }
New-Alias -Name gb -Value Get-GitBranch
function Get-GitPull { & git pull $args }
New-Alias -Name gl -Value Get-GitPull

function Get-GitPush { & git pull $args }
New-Alias -Name gp -Value Get-GitPush

function Get-GitCheckoutPreviousBranch { & git checkout - $args}
New-Alias -Name gcop -Value Get-GitCheckoutPreviousBranch

function Open-AddAliases { code $PROFILE }
New-Alias -Name addAliases -Value Open-AddAliases
Sign up to request clarification or add additional context in comments.

4 Comments

What does the & do?
@MattW The ampersand operator forces PowerShell to execute the following arguments as a command (CMD). It may not always be required, but I found it avoids any unintended effects that could result from arguments injected at the end of the command ($args) being evaluated incorrectly.
You don't actually need aliases anymore in this case. You can just name your function e.g. s and then call it directly as a command.
It should be noted that Get-GitStatus already exists (at least it did in my quite simple Win11/VisualStudio machine. That Get-GitStatus is something different and will, for good and bad, be overwritten by the above code.
86

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.

4 Comments

Bonus points for adding the git functions+aliases in a standalone script, and having them sourced in from your profile.
If you define your own Get-GitStatus function as above, you'll overwrite posh-git's Get-GitStatus which is used to get structured status info to build the custom prompt.
Why another alias when you got a function? Just put a short function name as @alex does with the posh kid ähh git.
13

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

1 Comment

5 years later still up to date. You should rename the list to posh git functions ...
11

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

1 Comment

The above link didn't work for me. this link provides similar info git.wiki.kernel.org/articles/a/l/i/Aliases.html
4

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'

Comments

3

You can create PowerShell profile that autorun when open PowerShell First open PowerShell and create PowerShell profile (If you don't already have one):

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

This command will create directory in Documents call WindowsPowerShell inside this directory will create file call Microsoft.PowerShell_profile.ps1

If you want to know the full path of Microsoft.PowerShell_profile.ps1 write this command:

$PROFILE

Open it from PowerShell with this command

powershell_ise.exe $PROFILE

And write all commands that you want to alias it for example:

function gn{git init}
# @args you can pass multi arguments for example
# ga fileName1 fileName2 
function add{git add @args}
function commit { git commit -m @args }
function gac{git add .;git commit -m @args}
function gpm{git push origin master}
function pull{git pull origin master}
function gl{git log}
function glo{git log --oneline}
function gch{git checkout @args}

# @args is optional to add argument
function gb{git branch @args}
function gs{git status}
function gd{git diff}

Save it And close PowerShell, Open it again and call alias commands by function name for example initialize git repository write this command:

gn

To add files write this commands

add fileName1

See GIF image aliases git commands from PowerShell

To show content of alias commands write this command:

Get-Content $PROFILE

Comments

1

Maybe it's late for this question, but for anyone who still has similar question, install this powershell module:

PS> Install-Module -Name git-aliases

According to it's description,

A PowerShell module that provide partial Git aliases from Oh My Zsh's git plugin.

Comments

0

If you use PowerShell, then you need to add functions including the commands that you want to create the alias for.

For example, you want to create an alias for git fetch -all, then you need to add the following in the file Microsoft.PowerShell_profile:

function gfa{git fetch --all}

Then if you run gfa in your PowerShell or WindowsTerminal, then git fetch --all will be executed.

Note: To open Microsoft.PowerShell_profile file in your notepad, run notepad $profile in your PowerShell or WindowsTerminal. Or you navigate to C:\Users\YOUR_USER\Documents\WindowsPowerShell to get the file.

Here is a list of common functions:


function gs{git status}
function ga{git add .}
function gfa{git fetch --all}

function grman{git rebase -i main}
function grmas{git rebase -i master}
function grdev{git rebase -i develop}

function gitc{ git commit -m @args }
function gac{git add .;git commit -m @args}
function pushmas{git push origin master}
function pushman{git push origin main}
function pushdev{git push origin develop}

function pullman{git pull origin main}
function pullmas{git pull origin main}
function pulldev{git pull origin develop}

function pull{git pull origin master}
function gl{git log}
function glo{git log --oneline}
function gch{git checkout @args}
function gcn{git checkout -b @args}

function gman{git checkout main}
function gmas{git checkout master}
function gdev{git checkout develop}

function gb{git branch @args}
function gs{git status}
function gd{git diff}

This GitHub Gist is the link to my own Microsoft.PowerShell_profile.

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.