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

时间:2022-11-18 16:57:53

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?

我理解如何在PowerShell中为cmdlet创建别名,但是我想在PowerShell中创建一个别名,比如“git status”就像“gs”和“git pull origin master”那样“gpm”可以让任何人指出我的权利方向?

5 个解决方案

#1


46  

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.

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

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

编辑:忘了添加如何使用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.

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

#2


19  

Just created some shortcuts for myself and wanted to share:

刚刚为自己创建了一些快捷方式,并希望分享:

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

创建PowerShell配置文件(如果您还没有):

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.:

重新启动PowerShell会话时,您也应该能够将参数传递给别名。例如。:

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

#3


6  

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

我不知道PowerShell,但您可以直接在Git中设置别名。

#4


5  

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

我创建了posh-git-alias,您可以将其添加到PowerShell $ PROFILE中。

#5


2  

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

您需要创建一个profile.ps1文件,将其放在文件夹中,在我的文档中调用WindowsPowerShell

Then put in profile.ps1 a line like this:

然后在profile.ps1中放入一行如下:

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

#1


46  

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.

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

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

编辑:忘了添加如何使用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.

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

#2


19  

Just created some shortcuts for myself and wanted to share:

刚刚为自己创建了一些快捷方式,并希望分享:

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

创建PowerShell配置文件(如果您还没有):

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.:

重新启动PowerShell会话时,您也应该能够将参数传递给别名。例如。:

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

#3


6  

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

我不知道PowerShell,但您可以直接在Git中设置别名。

#4


5  

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

我创建了posh-git-alias,您可以将其添加到PowerShell $ PROFILE中。

#5


2  

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

您需要创建一个profile.ps1文件,将其放在文件夹中,在我的文档中调用WindowsPowerShell

Then put in profile.ps1 a line like this:

然后在profile.ps1中放入一行如下:

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