且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在PowerShell中为git命令创建别名?

更新时间:2022-11-02 21:52:09

你必须先创建一个函数,它有你的命令。然后为该函数创建一个别名。

  PS C:\Users\jpogran\code\git\scripts&gt ;函数get-gitstatus {git status} 

PS C:\Users\jpogran\code\git\scripts> get-gitstatus
#在分支master上
没有提交(工作目录干净)

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

PS C:\Users\jpogran\code\git\scripts> gs
#分支大师
无需提交(工作目录清理)

您可能还对称为 posh-git 的操作系统项目感兴趣,该项目旨在为PowerShell环境git命令。使用PS类型函数包装git命令,并提供一个新提示,显示提示中的状态和分支。

编辑:忘了添加如何找出如何执行这使用Powershell。
  PS C:\Users\jpogran\code\git\scripts> get-help set-alias -examples 

这会向您展示示例(最后一个适用于此处)如何使用set-alias为带有参数,管线等的命令创建别名。


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?

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.