[英]Creating aliases in PowerShell for git commands?
我了解如何在 PowerShell 中为 cmdlet 创建别名,但我想在 PowerShell 中为gs
git status
和git pull origin master
as 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的 OS 项目感兴趣,该项目旨在为 git 命令提供 powershell 环境。 用 PS 类型函数包装 git 命令,并提供一个新的提示符,在提示符中显示状态和分支。
编辑:忘记添加如何了解如何使用 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
当您重新启动 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
我不知道 PowerShell,但您可以直接在 Git 中设置别名。
我创建了posh-git-alias ,您可以将其添加到您的 PowerShell $PROFILE
中。
您需要创建一个 profile.ps1 文件,将其放在我的文档中调用 WindowsPowerShell 的文件夹中
然后在 profile.ps1 中放入这样的一行:
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
此命令将在 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
要显示别名命令的内容,请编写以下命令:
Get-Content $PROFILE
如果您使用PowerShell ,那么您需要添加函数,包括您要为其创建别名的命令。
例如,你想为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
的链接。
在 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.