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

5cnsuln7  于 2022-12-10  发布在  Git
关注(0)|答案(8)|浏览(154)

我知道如何在PowerShell中为cmdlet创建别名,但我想在PowerShell中为git status创建别名,如gsgit pull origin mastergpm。有人能给我指出正确的方向吗?

zed5wv10

zed5wv101#

你必须先创建一个函数,其中包含你的命令。然后创建该函数的别名。

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的操作系统项目感兴趣,该项目旨在为git命令提供一个powershell环境,用PS类型的函数 Package git命令,并提供一个新的提示符,在提示符中显示状态和分支。
编辑:忘记添加如何找到如何使用Powershell做到这一点。

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

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

o75abkj4

o75abkj42#

我刚刚为自己创建了一些快捷方式,并希望与大家分享:
创建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
myzjeezk

myzjeezk3#

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

jm2pwxwz

jm2pwxwz4#

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

nzrxty8p

nzrxty8p5#

您需要创建一个profile.ps1文件,将其放在我的文档中名为WindowsPowerShell的文件夹中
然后在profile.ps1中放入如下行:

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

2cmtqfgy6#

您可以创建在打开PowerShell时自动运行的PowerShell配置文件首先打开PowerShell并创建PowerShell配置文件(如果您还没有配置文件):
New-Item -Type file -Path $PROFILE -Force
此命令将在Documents中创建目录调用**WindowsPowerShell在此目录内将创建文件调用Microsoft.PowerShell_profile.ps1**
如果要了解**Microsoft.PowerShell_profile.ps1**的完整路径,请编写以下命令:

$PROFILE

使用以下命令从PowerShell打开它

powershell_ise.exe $PROFILE

并编写所有要为其指定别名的命令,例如:

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}

保存它并关闭PowerShell,再次打开它并通过函数名调用别名命令,例如初始化git仓库写入此命令:
gn
要添加文件,请编写以下命令
add fileName1
请参阅GIF图像

要显示别名命令的内容,请编写以下命令:
Get-Content $PROFILE

mhd8tkvw

mhd8tkvw7#

如果使用PowerShell,则需要添加函数,包括要为其创建别名的命令。
例如,要为git fetch -all创建别名,则需要在文件Microsoft.PowerShell_profile中添加以下内容:

function gfa{git fetch --all}

然后,如果您在PowerShellWindowsTerminal中运行gfa,则会执行git fetch --all

注意:要在记事本中打开Microsoft.PowerShell_profile文件,请在PowerShellWindowsTerminal中运行notepad $profile.或者导航到C:\Users\YOUR_USER\Documents\WindowsPowerShell以获取该文件.

以下是常用函数的列表:

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}

这个是这个。

mspsb9vt

mspsb9vt8#

在Windows上,我使用了位于用户个人文件夹中的.gitconfig文件中的以下内容:

[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

相关问题