Dreamer Dreamer
首页
  • 分类
  • 标签
  • 归档
关于
GitHub (opens new window)

lycpan233

白日梦想家
首页
  • 分类
  • 标签
  • 归档
关于
GitHub (opens new window)
  • System

    • ZSH-Git 命令-tab 补全命令时候选项过多问题
    • Windows 实现 ZSH Git 命令缩写(alias)
    • Windows PowerShell Posh-Git 配置命令缩写(alias)
      • 背景
      • 实现步骤
    • wsl2 卡顿、Tab补全卡顿问题解决
    • vscode pwsh 输入时突然一片空白
  • 博客

  • IDE

  • OAuth

  • 大杂烩
  • System
lycpan233
2023-11-26
目录

Windows PowerShell Posh-Git 配置命令缩写(alias)

# 背景

笔者尝试为 Git 设置 alias,采用了 config 、 aliases.sh 等方式。达到的效果均是在 Git bash 中生效,而在 PowerShell 中没有效果,经过排查,发现在 PowerShell 中使用了 Posh-Git,尝试了多种方式,没能直接打通,最后采用了为 PowerShell 设定 alias 的方式。

# 实现步骤

  1. 打开 PowerShell 的配置文件
notepad $profile
1

获取命令后打开相应文件即可。 下面是我的配置信息。若你没有这个文件,可以自己百度一下。= = 话说你要没这个文件,怎么安装的 Posh-Git 啊???

oh-my-posh init powershell --config "$env:POSH_THEMES_PATH\robbyrussell.omp.json" | Invoke-Expression

# 移除已有别名(如果存在)
if (Get-Alias gp -ErrorAction SilentlyContinue) { Remove-Item alias:gp -Force }
if (Get-Alias gl -ErrorAction SilentlyContinue) { Remove-Item alias:gl -Force }
if (Get-Alias gm -ErrorAction SilentlyContinue) { Remove-Item alias:gm -Force }

# Git Helper Functions
function git_current_branch {
    $branch = git symbolic-ref --short HEAD 2> $null
    if ($LASTEXITCODE -ne 0) {
        $branch = git rev-parse --short HEAD 2> $null
    }
    echo $branch
}

function git_main_branch {
    $ref = git symbolic-ref refs/remotes/origin/HEAD 2> $null
    if ($ref -match "refs/remotes/origin/(.*)") {
        return $matches[1]
    } else {
        # Default to 'main' or check for 'master'
        $branches = git branch --list main master
        if ($branches -match "main") {
            return "main"
        } elseif ($branches -match "master") {
            return "master"
        } else {
            return "main" # Default fallback
        }
    }
}

function git_develop_branch {
    $branches = git branch --list develop dev
    if ($branches -match "develop") {
        return "develop"
    } elseif ($branches -match "dev") {
        return "dev"
    } else {
        return "develop" # Default fallback
    }
}

# End of Git Helper Functions

# Git Aliases
function g { git $args }
function ga { git add $args }
function gaa { git add --all $args }
function gam { git am $args }
function gama { git am --abort $args }
function gamc { git am --continue $args }
function gams { git am --skip $args }
function gamscp { git am --show-current-patch $args }
function gap { git apply $args }
function gapa { git add --patch $args }
function gapt { git apply --3way $args }
function gau { git add --update $args }
function gav { git add --verbose $args }
function gb { git branch $args }
function gbD { git branch --delete --force $args }
function gba { git branch --all $args }
function gbd { git branch --delete $args }
function gbg { $env:LANG = "C"; git branch -vv | Select-String ": gone\]" }
function gbgD {
    $env:LANG = "C"
    git branch --no-color -vv |
    Select-String ": gone\]" |
    ForEach-Object {
        $_.ToString().Substring(2).Split(' ')[0]
    } |
    ForEach-Object {
        git branch -D $_
    }
}
function gbgd {
    $env:LANG = "C"
    git branch --no-color -vv |
    Select-String ": gone\]" |
    ForEach-Object {
        $_.ToString().Substring(2).Split(' ')[0]
    } |
    ForEach-Object {
        git branch -d $_
    }
}
function gbl { git blame -w $args }
function gbm { git branch --move $args }
function gbnm { git branch --no-merged $args }
function gbr { git branch --remote $args }
function gbs { git bisect $args }
function gbsb { git bisect bad $args }
function gbsg { git bisect good $args }
function gbsn { git bisect new $args }
function gbso { git bisect old $args }
function gbsr { git bisect reset $args }
function gbss { git bisect start $args }
function gc { git commit --verbose $args }
function gc! { git commit --verbose --amend $args }
function gcB { git checkout -B $args }
function gca { git commit --verbose --all $args }
function gca! { git commit --verbose --all --amend $args }
function gcam { git commit --all --message $args }
function gcan! { git commit --verbose --all --no-edit --amend $args }
function gcann! { git commit --verbose --all --date=now --no-edit --amend $args }
function gcans! { git commit --verbose --all --signoff --no-edit --amend $args }
function gcas { git commit --all --signoff $args }
function gcasm { git commit --all --signoff --message $args }
function gcb { git checkout -b $args }
function gcd { git checkout $(git_develop_branch) $args }
function gcf { git config --list $args }
function gcl { git clone --recurse-submodules $args }
function gclean { git clean --interactive -d $args }
function gclf { git clone --recursive --shallow-submodules --filter=blob:none --also-filter-submodules $args }
function gcm { git checkout $(git_main_branch) $args }
function gcmsg { git commit --message $args }
function gcn { git commit --verbose --no-edit $args }
function gcn! { git commit --verbose --no-edit --amend $args }
function gco { git checkout $args }
function gcor { git checkout --recurse-submodules $args }
function gcount { git shortlog --summary --numbered $args }
function gcp { git cherry-pick $args }
function gcpa { git cherry-pick --abort $args }
function gcpc { git cherry-pick --continue $args }
function gcs { git commit --gpg-sign $args }
function gcsm { git commit --signoff --message $args }
function gcss { git commit --gpg-sign --signoff $args }
function gcssm { git commit --gpg-sign --signoff --message $args }
function gd { git diff $args }
function gdca { git diff --cached $args }
function gdcw { git diff --cached --word-diff $args }
function gds { git diff --staged $args }
function gdt { git diff-tree --no-commit-id --name-only -r $args }
function gdw { git diff --word-diff $args }
function gf { git fetch $args }
function gfa { git fetch --all --tags --prune --jobs=10 $args }
function gfg { git ls-files | Select-String $args }
function gfo { git fetch origin $args }
function gg { git gui citool $args }
function gga { git gui citool --amend $args }
function ggpull { git pull origin "$(git_current_branch)" $args }
function ggpur { ggu $args }
function ggpush { git push origin "$(git_current_branch)" $args }
function ggsup { git branch --set-upstream-to=origin/$(git_current_branch) $args }
function ghh { git help $args }
function gignore { git update-index --assume-unchanged $args }
function gignored { git ls-files -v | Select-String "^[a-z]" }
function gl { git pull $args }
function glg { git log --stat $args }
function glgg { git log --graph $args }
function glgga { git log --graph --decorate --all $args }
function glgm { git log --graph --max-count=10 $args }
function glgp { git log --stat --patch $args }
function glo { git log --oneline --decorate $args }
function glod { git log --graph --pretty="%C(red)%h%C(reset) -%C(auto)%d%C(reset) %s %C(green)(%ad) %C(bold blue)<%an>%C(reset)" $args }
function glods { git log --graph --pretty="%C(red)%h%C(reset) -%C(auto)%d%C(reset) %s %C(green)(%ad) %C(bold blue)<%an>%C(reset)" --date=short $args }
function glog { git log --oneline --decorate --graph $args }
function gloga { git log --oneline --decorate --graph --all $args }
function glol { git log --graph --pretty="%C(red)%h%C(reset) -%C(auto)%d%C(reset) %s %C(green)(%ar) %C(bold blue)<%an>%C(reset)" $args }
function glola { git log --graph --pretty="%C(red)%h%C(reset) -%C(auto)%d%C(reset) %s %C(green)(%ar) %C(bold blue)<%an>%C(reset)" --all $args }
function glols { git log --graph --pretty="%C(red)%h%C(reset) -%C(auto)%d%C(reset) %s %C(green)(%ar) %C(bold blue)<%an>%C(reset)" --stat $args }
function gm { git merge $args }
function gma { git merge --abort $args }
function gmc { git merge --continue $args }
function gmff { git merge --ff-only $args }
function gmom { git merge origin/$(git_main_branch) $args }
function gms { git merge --squash $args }
function gmtl { git mergetool --no-prompt $args }
function gmtlvim { git mergetool --no-prompt --tool=vimdiff $args }
function gmum { git merge upstream/$(git_main_branch) $args }
function gp { git push $args }
function gpd { git push --dry-run $args }
function gpf { git push --force-with-lease --force-if-includes $args }
function gpf! { git push --force $args }
function gpoat { git push origin --all; git push origin --tags $args }
function gpod { git push origin --delete $args }
function gpr { git pull --rebase $args }
function gpra { git pull --rebase --autostash $args }
function gprav { git pull --rebase --autostash -v $args }
function gpristine { git reset --hard; git clean --force -dfx $args }
function gprom { git pull --rebase origin $(git_main_branch) $args }
function gpromi { git pull --rebase=interactive origin $(git_main_branch) $args }
function gprum { git pull --rebase upstream $(git_main_branch) $args }
function gprumi { git pull --rebase=interactive upstream $(git_main_branch) $args }
function gprv { git pull --rebase -v $args }
function gpsup { git push --set-upstream origin $(git_current_branch) $args }
function gpsupf { git push --set-upstream origin $(git_current_branch) --force-with-lease --force-if-includes $args }
function gpu { git push upstream $args }
function gpv { git push --verbose $args }
function gr { git remote $args }
function gra { git remote add $args }
function grb { git rebase $args }
function grba { git rebase --abort $args }
function grbc { git rebase --continue $args }
function grbd { git rebase $(git_develop_branch) $args }
function grbi { git rebase --interactive $args }
function grbm { git rebase $(git_main_branch) $args }
function grbo { git rebase --onto $args }
function grbom { git rebase origin/$(git_main_branch) $args }
function grbs { git rebase --skip $args }
function grbum { git rebase upstream/$(git_main_branch) $args }
function grep { grep --color $args }
function grev { git revert $args }
function greva { git revert --abort $args }
function grevc { git revert --continue $args }
function grf { git reflog $args }
function grh { git reset $args }
function grhh { git reset --hard $args }
function grhk { git reset --keep $args }
function grhs { git reset --soft $args }
function grm { git rm $args }
function grmc { git rm --cached $args }
function grmv { git remote rename $args }
function groh { git reset origin/$(git_current_branch) --hard $args }
function grrm { git remote remove $args }
function grs { git restore $args }
function grset { git remote set-url $args }
function grss { git restore --source $args }
function grst { git restore --staged $args }
function grt {
    $toplevel = git rev-parse --show-toplevel 2>$null
    if ($LASTEXITCODE -ne 0) {
        Set-Location "."
    } else {
        Set-Location $toplevel
    }
}
function gru { git reset -- $args }
function grup { git remote update $args }
function grv { git remote --verbose $args }
function gsb { git status --short --branch $args }
function gsd { git svn dcommit $args }
function gsh { git show $args }
function gsi { git submodule init $args }
function gsps { git show --pretty=short --show-signature $args }
function gsr { git svn rebase $args }
function gss { git status --short $args }
function gst { git status $args }
function gsta { git stash push $args }
function gstaa { git stash apply $args }
function gstall { git stash --all $args }
function gstc { git stash clear $args }
function gstd { git stash drop $args }
function gstl { git stash list $args }
function gstp { git stash pop $args }
function gsts { git stash show --patch $args }
function gstu { git stash push --include-untracked $args }
function gsu { git submodule update $args }
function gsw { git switch $args }
function gswc { git switch --create $args }
function gswd { git switch $(git_develop_branch) $args }
function gswm { git switch $(git_main_branch) $args }
function gta { git tag --annotate $args }
function gtl { param($pattern="*"); git tag --sort=-v:refname -n --list "$pattern*" }
function gts { git tag --sign $args }
function gtv { git tag | Sort-Object -Property {[version]$_} }
function gunignore { git update-index --no-assume-unchanged $args }
function gunwip {
    $wip = git rev-list --max-count=1 --format="%s" HEAD | Select-String "\--wip--"
    if ($wip) { git reset HEAD~1 }
}
function gwch { git whatchanged -p --abbrev-commit --pretty=medium $args }
function gwip {
    git add -A
    git ls-files --deleted -z | ForEach-Object { if ($_) { git rm $_ } }
    git commit --no-verify --no-gpg-sign --message "--wip-- [skip ci]" $args
}
function gwipe { git reset --hard; git clean --force -df $args }
function gwt { git worktree $args }
function gwta { git worktree add $args }
function gwtls { git worktree list $args }
function gwtmv { git worktree move $args }
function gwtrm { git worktree remove $args }

# Deprecated aliases - shown with warnings and redirected to new commands
function gup {
    Write-Warning "[PowerShell] 'gup' is deprecated, using 'gpr' instead."
    gpr $args
}
function gupa {
    Write-Warning "[PowerShell] 'gupa' is deprecated, using 'gpra' instead."
    gpra $args
}
function gupav {
    Write-Warning "[PowerShell] 'gupav' is deprecated, using 'gprav' instead."
    gprav $args
}
function gupom {
    Write-Warning "[PowerShell] 'gupom' is deprecated, using 'gprom' instead."
    gprom $args
}
function gupomi {
    Write-Warning "[PowerShell] 'gupomi' is deprecated, using 'gpromi' instead."
    gpromi $args
}
function gupv {
    Write-Warning "[PowerShell] 'gupv' is deprecated, using 'gprv' instead."
    gprv $args
}
# End of Git Aliases
Import-Module posh-git # 引入 posh-git

Set-PSReadlineKeyHandler -Key Tab -Function Complete # 设置 Tab 键补全
Set-PSReadLineKeyHandler -Key "Ctrl+d" -Function MenuComplete # 设置 Ctrl+d 为菜单补全和 Intellisense
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo # 设置 Ctrl+z 为撤销
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward # 设置向上键为后向搜索历史记录
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward  # 设置向下键为前向搜索历史记录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308

相关的内容可以看查阅下面的文档:

Posh-Git Github (opens new window)
PowerShell Alias Tab Completion not working #852 (opens new window)

  1. 更新配置
. $profile
1
编辑 (opens new window)
上次更新: 2025/09/01, 01:45:02
Windows 实现 ZSH Git 命令缩写(alias)
wsl2 卡顿、Tab补全卡顿问题解决

← Windows 实现 ZSH Git 命令缩写(alias) wsl2 卡顿、Tab补全卡顿问题解决→

最近更新
01
vscode pwsh 输入时突然一片空白
09-01
02
日志追踪
08-07
03
docker基础概念
02-26
更多文章>
Theme by Vdoing | Copyright © 2023-2025 Dreamer | MIT License
粤ICP备2025379918号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式