在 Powershell 中为 Git 分支添加选项卡完成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3586722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add tab completion for Git branches in Powershell
提问by Gabe Moothart
When inside a Git repository, is it possible to add tab completion for branches to Powershell? For example:
在 Git 存储库中时,是否可以为 Powershell 的分支添加选项卡完成功能?例如:
PS> git checkout ma
TAB
PS> git checkout ma
TAB
would result in
会导致
PS> git checkout master
采纳答案by fletcher
For that to be possible, a git provider for PowerShell would need to exist.
为此,需要存在 PowerShell 的 git 提供程序。
After a quick search, something similar apparently exists, the bizarre but aptly named posh-git:
快速搜索后,显然存在类似的东西,奇怪但恰如其分地命名为 posh-git:
http://github.com/dahlbyk/posh-git
http://github.com/dahlbyk/posh-git
A set of PowerShell scripts which provide Git/PowerShell integration
- Prompt for Git repositories: The prompt within Git repositories can show the current branch and the state of files (additions, modifications,
deletions) within.- Tab completion: Provides tab completion for common commands when using git. E.g.
git ch<tab>
-->git checkout
Usage
See profile.example.ps1 as to how you can integrate the tab completion and/or git prompt into your own profile. You can also choose whether advanced git commands are shown in the tab expansion or only simple/common commands. Default is simple.
一组提供 Git/PowerShell 集成的 PowerShell 脚本
- Git 存储库提示:Git 存储库中的提示可以显示当前分支和其中文件的状态(添加、修改、
删除)。- Tab 补全:在使用 git 时为常用命令提供 Tab 补全。例如
git ch<tab>
-->git checkout
用法
有关如何将选项卡完成和/或 git 提示集成到您自己的配置文件中的信息,请参阅 profile.example.ps1。您还可以选择是在选项卡扩展中显示高级 git 命令还是仅显示简单/常用命令。默认很简单。
回答by x0n
I wrote a generic provider for PowerShellwhose behaviour can be implemented entirely in powershell script. This would be an ideal starting spot to prototype a GIT provider if one does not exist (or is dead, or insufficent.
我为 PowerShell编写了一个通用提供程序,其行为可以完全在 powershell 脚本中实现。如果一个 GIT 提供者不存在(或者已经死了,或者不足),这将是一个理想的起点。
回答by Yuriy Gettya
I wrote this little PS "gem", if posh-git is too much.
Just put it in your PowerShell profileto be able to type co?
(with a space) and hit Tabto trigger completion and cycle through the list of branches:
我写了这个小小的 PS“gem”,如果 posh-git 太多了。
只需将它放在您的PowerShell 配置文件中即可键入co?
(带空格)并点击Tab以触发完成并在分支列表中循环:
function co
{
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[ArgumentCompleter({
param($pCmd, $pParam, $pWord, $pAst, $pFakes)
$branchList = (git branch --format='%(refname:short)')
if ([string]::IsNullOrWhiteSpace($pWord)) {
return $branchList;
}
$branchList | Select-String "$pWord"
})]
[string] $branch
)
git checkout $branch;
}
UPDATE: refactored to return a list of branches when tab-completion invoked after space and no partial string can be matched. Will return "master" if this is only one branch
更新:重构为在空格后调用制表符完成时返回分支列表,并且无法匹配部分字符串。如果这只是一个分支,将返回“master”
As a bonus, did you know you can call TortoiseGit from shell?
作为奖励,您知道可以从 shell 调用 TortoiseGit 吗?
function dif
{
TortoiseGitProc.exe /command:repostatus
}