2010-05-18 46 views
34

我知道如何cmdlet的罰款創造PowerShell中的別名,但我想創建PowerShell中的別名,像「混帳地位」爲只是「GS」和「混帳拉出身主」爲「GPM」任何人都可以點我正確的方向?在PowerShell中爲git命令創建別名?

回答

43

您必須首先創建一個功能,即在有你的命令。然後爲該函數創建一個別名。

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) 

您可能也有興趣稱爲posh-git OS項目,旨在爲Git命令提供的PowerShell環境。用PS類型函數包裝git命令,並提供一個新的提示,顯示提示中的狀態和分支。

編輯:忘了補充如何找出如何做到這一點使用PowerShell。

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

這將顯示實例(最後一個在這裏也適用)如何使用設置別名創建別名與PARAMATERS,管道的命令,等

+0

獎勵積分用於在獨立腳本中添加git函數+別名,並讓它們從您的個人資料。 – Goyuix 2010-05-18 15:50:03

+0

http://technet.microsoft.com/library/hh849938.aspx#sectionSection8對於網上的例子:) – Crisfole 2014-08-29 17:58:03

+0

百萬upvotes – thedanotto 2018-03-02 20:11:02

6

我不知道PowerShell的,但你可以setup aliases directly in Git

+0

上面的鏈接不適合我。此鏈接提供了類似的信息https://git.wiki.kernel.org/articles/a/l/i/Aliases.html – 2012-04-18 11:31:11

2

你需要創建一個profile.ps1文件放在一個文件夾中調用WindowsPowerShell在我的文檔

再放入profile.ps1這樣一行:

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

剛剛創建一些捷徑,爲我自己,想牛逼O共有:

創建PowerShell配置文件(如果你不已經有一個):

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

打開它來編輯:

notepad $PROFILE 

添加下列函數和別名:

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 

當您重新啓動PowerShell會話,你應該能夠參數傳遞給別名爲好。例如: -

c "This is a commit message" 

更新:

下面是一些我經常使用的快捷鍵:

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 
+0

什麼是&呢? – 2017-03-30 08:07:58

+0

@MattW和號運算符強制PowerShell將下列參數作爲命令(CMD)執行。它可能並不總是必需的,但我發現它避免了在命令末尾註入的參數($ args)可能導致的意外影響被錯誤地評估。 – 2017-04-01 13:40:06

4

我創建posh-git-alias它你可以添加到您的PowerShell $PROFILE

+0

感謝您的支持!對我非常有用。 – tomd 2016-02-22 11:47:03