我知道如何在PowerShell中为cmdlet创建别名,但我想为像git status
这样的命令创建一个简短的别名 gs
,以及把git pull origin master
命令创建成gpm
。请问有谁能指点一下我吗?
我知道如何在PowerShell中为cmdlet创建别名,但我想为像git status
这样的命令创建一个简短的别名 gs
,以及把git pull origin master
命令创建成gpm
。请问有谁能指点一下我吗?
你需要先创建一个函数,其中包含你的命令。然后创建该函数的别名。
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环境。将git命令封装为PS类型函数,并提供一个新的提示符,显示提示符中的状态和分支。
编辑:忘记添加如何使用Powershell进行此操作。
PS C:\Users\jpogran\code\git\scripts> get-help set-alias -examples
这将向您展示使用set-alias创建带有参数、管道等命令别名的示例(最后一个适用于此处)。
我为自己创建了一些快捷方式,想要分享一下:
创建一个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
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
s
,然后直接调用它作为命令即可。 - Daniel WernerGet-GitStatus
已经存在(至少在我的Win11/VisualStudio机器上是这样)。那个Get-GitStatus
是另一回事,它将被上面的代码替换,既有好处也有坏处。 - LosManos我创建了posh-git-alias,你只需将其添加到你的PowerShell $PROFILE
中即可。
我不熟悉PowerShell,但你可以在Git中直接设置别名。
set-alias wit 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\witadmin.exe'
您可以创建 PowerShell 配置文件,以便在打开 PowerShell 时自动运行。首先打开 PowerShell 并创建 PowerShell 配置文件(如果您还没有):
New-Item -Type file -Path $PROFILE -Force
此命令将在“文档”中创建名为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
要显示别名命令的内容,请使用以下命令:
Get-Content $PROFILE
git fetch -all
创建一个别名,则需要在文件 Microsoft.PowerShell_profile
中添加以下内容:function gfa{git fetch --all}
如果您在PowerShell或WindowsTerminal中运行gfa
,则会执行git fetch --all
。
注意:要在记事本中打开Microsoft.PowerShell_profile
文件,请在PowerShell或WindowsTerminal中运行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}
这个GitHub Gist是我的链接Microsoft.PowerShell_profile
。
也许这个问题来得有点晚了,但对于那些仍然有类似疑问的人,请安装这个powershell模块:
PS> Install-Module -Name git-aliases
这是一个 PowerShell 模块,提供了来自 Oh My Zsh 的 Git 插件的部分别名。
.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
Get-GitStatus
函数,您将覆盖 posh-git 的Get-GitStatus
函数,后者用于获取结构化状态信息以构建自定义提示符。 - Andrew Spencer