在PowerShell中为git命令创建别名? [英] Creating aliases in PowerShell for git commands?

查看:307
本文介绍了在PowerShell中为git命令创建别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在PowerShell中为cmdlet创建别名,但我想在PowerShell中为git status创建别名,例如gs和git pull origin master作为gpm,任何人都可以指向我在正确的方向吗?

解决方案

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

  PS C:\Users\jpogran\code\git\scripts> ;函数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.

这篇关于在PowerShell中为git命令创建别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆