Git
非官方测试版翻译
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
功能
当处于 Git 仓库时显示 git 相关信息。此功能在子文件夹中同样有效。为确保最佳兼容性,请保持 git 程序为最新版本(例如当分支或状态信息显示不正确时)。
示例配置
- json
- yaml
- toml
{
"type": "git",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#193549",
"background": "#ffeb3b",
"background_templates": [
"{{ if or (.Working.Changed) (.Staging.Changed) }}#FFEB3B{{ end }}",
"{{ if and (gt .Ahead 0) (gt .Behind 0) }}#FFCC80{{ end }}",
"{{ if gt .Ahead 0 }}#B388FF{{ end }}",
"{{ if gt .Behind 0 }}#B388FB{{ end }}"
],
"template": "{{ .UpstreamIcon }}{{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} {{ .Working.String }}{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Staging.Changed }} {{ .Staging.String }}{{ end }}{{ if gt .StashCount 0 }} {{ .StashCount }}{{ end }}",
"options": {
"fetch_status": true,
"fetch_upstream_icon": true,
"untracked_modes": {
"/Users/user/Projects/oh-my-posh/": "no"
},
"source": "cli",
"mapped_branches": {
"feat/*": "🚀 ",
"bug/*": "🐛 "
}
}
}
type: git
style: powerline
powerline_symbol:
foreground: "#193549"
background: "#ffeb3b"
background_templates:
- "{{ if or (.Working.Changed) (.Staging.Changed) }}#FFEB3B{{ end }}"
- "{{ if and (gt .Ahead 0) (gt .Behind 0) }}#FFCC80{{ end }}"
- "{{ if gt .Ahead 0 }}#B388FF{{ end }}"
- "{{ if gt .Behind 0 }}#B388FB{{ end }}"
template: "{{ .UpstreamIcon }}{{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus
}}{{ end }}{{ if .Working.Changed }} {{ .Working.String }}{{ end }}{{ if and
(.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Staging.Changed }}
{{ .Staging.String }}{{ end }}{{ if gt .StashCount 0 }} {{ .StashCount }}{{
end }}"
options:
fetch_status: true
fetch_upstream_icon: true
untracked_modes:
/Users/user/Projects/oh-my-posh/: no
source: cli
mapped_branches:
feat/*: "🚀 "
bug/*: "🐛 "
type = "git"
style = "powerline"
powerline_symbol = ""
foreground = "#193549"
background = "#ffeb3b"
background_templates = [ "{{ if or (.Working.Changed) (.Staging.Changed) }}#FFEB3B{{ end }}", "{{ if and (gt .Ahead 0) (gt .Behind 0) }}#FFCC80{{ end }}", "{{ if gt .Ahead 0 }}#B388FF{{ end }}", "{{ if gt .Behind 0 }}#B388FB{{ end }}" ]
template = "{{ .UpstreamIcon }}{{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} {{ .Working.String }}{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Staging.Changed }} {{ .Staging.String }}{{ end }}{{ if gt .StashCount 0 }} {{ .StashCount }}{{ end }}"
[options]
fetch_status = true
fetch_upstream_icon = true
source = "cli"
[options.untracked_modes]
"/Users/user/Projects/oh-my-posh/" = "no"
[options.mapped_branches]
"feat/*" = "🚀 "
"bug/*" = "🐛 "
选项
信息获取
由于执行多次 git 调用会降低提示符的响应速度,默认情况下我们不会获取额外信息。
您可将以下选项设为 true 来启用额外信息的获取(并填充模板)。
| Name | Type | Default | Description |
|---|---|---|---|
fetch_status | boolean | false | fetch the local changes |
fetch_push_status | boolean | false | fetch the push-remote ahead/behind information. Requires fetch_status to be enabled |
ignore_status | []string | do not fetch status for these repo's. Uses the repo's root folder and same logic as the exclude_folders property | |
fetch_upstream_icon | boolean | false | fetch upstream icon |
fetch_bare_info | boolean | false | fetch bare repo info |
fetch_user | User | false | fetch the current configured user for the repository |
untracked_modes | map[string]string | map of repo's where to override the default untracked files mode:
"untracked_modes": { "/Users/me/repos/repo1": "no" } - defaults to normal for all repo's. If you want to override for all repo's, use * to set the mode instead of the repo path | |
ignore_submodules | map[string]string | map of repo's where to change the --ignore-submodules flag (none, untracked, dirty or all). For example "ignore_submodules": { "/Users/me/repos/repo1": "all" }. If you want to override for all repo's, use * to set the mode instead of the repo path | |
native_fallback | boolean | false | when set to true and git.exe is not available when inside a WSL2 shared Windows drive, we will fallback to the native git executable to fetch data. Not all information can be displayed in this case |
status_formats | map[string]string | a key, value map allowing to override how individual status items are displayed. For example, "status_formats": { "Added": "Added: %d" } will display the added count as Added: 1 instead of +1. See the Status section for available overrides. | |
source | string | cli |
|
mapped_branches | object | custom glyph/text for specific branches. You can use * at the end as a wildcard character for matching | |
branch_template | string | a template to format that branch name. You can use {{ .Branch }} as reference to the original branch name | |
disable_with_jj | boolean | false | disable the git segment in case of a Jujutsu collocated repository |
图标设置
分支图标
| Name | Type | Default | Description |
|---|---|---|---|
branch_icon | string | \uE0A0 | the icon to use in front of the git branch name |
branch_identical_icon | string | \u2261 | the icon to display when remote and local are identical |
branch_ahead_icon | string | \u2191 | the icon to display when the local branch is ahead of its remote |
branch_behind_icon | string | \u2193 | the icon to display when the local branch is behind its remote |
branch_gone_icon | string | \u2262 | the icon to display when there's no remote branch |
HEAD 图标
| Name | Type | Default | Description |
|---|---|---|---|
commit_icon | string | \uF417 | icon/text to display before the commit context (detached HEAD) |
tag_icon | string | \uF412 | icon/text to display before the tag context |
rebase_icon | string | \uE728 | icon/text to display before the context when in a rebase |
cherry_pick_icon | string | \uE29B | icon/text to display before the context when doing a cherry-pick |
revert_icon | string | \uF0E2 | icon/text to display before the context when doing a revert |
merge_icon | string | \uE727 | icon/text to display before the merge context |
no_commits_icon | string | \uF594 | icon/text to display when there are no commits in the repo |
上游图标
| Name | Type | Default | Description |
|---|---|---|---|
github_icon | string | \uF408 | icon/text to display when the upstream is GitHub |
gitlab_icon | string | \uF296 | icon/text to display when the upstream is GitLab |
bitbucket_icon | string | \uF171 | icon/text to display when the upstream is Bitbucket |
azure_devops_icon | string | \uEBE8 | icon/text to display when the upstream is Azure DevOps |
codecommit_icon | string | \uF270 | icon/text to display when the upstream is AWS CodeCommit |
codeberg_icon | string | \uF330 | icon/text to display when the upstream is Codeberg |
git_icon | string | \uE5FB | icon/text to display when the upstream is not known/mapped |
upstream_icons | map[string]string | a key, value map representing the remote URL (or a part of that URL) and icon to use in case the upstream URL contains the key. These get precedence over the standard icons |
模板 (信息)
默认模板
{{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} \uF044 {{ .Working.String }}{{ end }}{{ if and (.Staging.Changed) (.Working.Changed) }} |{{ end }}{{ if .Staging.Changed }} \uF046 {{ .Staging.String }}{{ end }}
属性
| Name | Type | Description |
|---|---|---|
.RepoName | string | the repo folder name |
.Working | Status | changes in the worktree (see below) |
.Staging | Status | staged changes in the work tree (see below) |
.HEAD | string | the current HEAD context (branch/rebase/merge/...) |
.Ref | string | the current HEAD reference (branch/tag/...) |
.Behind | int | commits behind of upstream |
.Ahead | int | commits ahead of upstream |
.PushBehind | int | commits behind of push remote |
.PushAhead | int | commits ahead of push remote |
.BranchStatus | string | the current branch context (ahead/behind string representation) |
.Upstream | string | the upstream name (remote) |
.UpstreamGone | boolean | whether the upstream is gone (no remote) |
.UpstreamIcon | string | the upstream icon (based on the icons above) |
.UpstreamURL | string | the upstream URL for use in hyperlinks in templates: {{ url .UpstreamIcon .UpstreamURL }} |
.RawUpstreamURL | string | the raw upstream URL (not cleaned up for display) |
.Hash | string | the full commit hash |
.ShortHash | string | the short commit hash (7 characters) |
.StashCount | int | the stash count |
.WorktreeCount | int | the worktree count |
.IsWorkTree | boolean | if in a worktree repo or not |
.IsBare | boolean | if in a bare repo or not, only set when fetch_bare_info is set to true |
.Dir | string | the repository's root directory |
.RelativeDir | string | the current directory relative to the root directory |
.Kraken | string | a link to the current HEAD in GitKraken for use in hyperlinks in templates {{ url .HEAD .Kraken }} |
.Commit | Commit | HEAD commit information (see below) |
.Detached | boolean | true when the head is detached |
.Merge | boolean | true when in a merge |
.Rebase | Rebase | contains the relevant information when in a rebase |
.CherryPick | boolean | true when in a cherry pick |
.Revert | boolean | true when in a revert |
.User | User | the current configured user (requires fetch_user to be enabled) |
.Remotes | map[string]string | a map of remote names to their URLs |
.LatestTag | string | the latest tag name |
状态显示
| Name | Type | Description |
|---|---|---|
.Unmerged | int | number of unmerged changes |
.Deleted | int | number of deleted changes |
.Added | int | number of added changes |
.Modified | int | number of modified changes |
.Untracked | int | number of untracked changes |
.Changed | boolean | if the status contains changes or not |
.String | string | a string representation of the changes above |
本地变更使用以下语法表示:
| Icon | Description |
|---|---|
x | Unmerged |
- | Deleted |
+ | Added |
~ | Modified |
? | Untracked |
提交信息
| Name | Type | Description |
|---|---|---|
.Author | User | the author of the commit (see below) |
.Committer | User | the committer of the commit (see below) |
.Subject | string | the commit subject |
.Timestamp | time.Time | the commit timestamp |
.Sha | string | the commit SHA1 |
.Refs | Refs | the commit references |
用户信息
| Name | Type | Description |
|---|---|---|
.Name | string | the user's name |
.Email | string | the user's email |
引用信息
| Name | Type | Description |
|---|---|---|
.Heads | []string | branches |
.Tags | []string | commit's tags |
.Remotes | []string | remote references |
因这些属性是字符串数组,可通过 join 函数拼接:
{{ join ", " .Commit.Refs.Tags }}
变基状态
| Name | Type | Description |
|---|---|---|
.Current | int | the current rebase step |
.Total | int | the total number of rebase steps |
.HEAD | string | the current HEAD |
.Onto | string | the branch we're rebasing onto |
posh-git 兼容
如需显示默认的 posh-git 输出,请勿使用本段,
而是在初始化 Oh My Posh 后,在 $PROFILE 中添加以下代码片段:
function Set-PoshGitStatus {
$global:GitStatus = Get-GitStatus
$env:POSH_GIT_STRING = Write-GitStatus -Status $global:GitStatus
}
New-Alias -Name 'Set-PoshContext' -Value 'Set-PoshGitStatus' -Scope Global -Force
然后可在 文本段 中使用 POSH_GIT_STRING 环境变量:
- json
- yaml
- toml
{
"template": "{{ if .Env.POSH_GIT_STRING }} {{ .Env.POSH_GIT_STRING }} {{ end }}"
}
template: "{{ if .Env.POSH_GIT_STRING }} {{ .Env.POSH_GIT_STRING }} {{ end }}"
template = "{{ if .Env.POSH_GIT_STRING }} {{ .Env.POSH_GIT_STRING }} {{ end }}"