Skip to content

Instantly share code, notes, and snippets.

@cubedtear
Last active January 31, 2025 11:35
Show Gist options
  • Save cubedtear/508b63c5336879a2680a0f3fd076afa7 to your computer and use it in GitHub Desktop.
Save cubedtear/508b63c5336879a2680a0f3fd076afa7 to your computer and use it in GitHub Desktop.
Powershell snippets to add git aliases and autocompletion using https://github.com/dahlbyk/posh-git - Add it to your profile at "code $profile"
Remove-Alias gcm -Force -ErrorAction SilentlyContinue
Remove-Alias gp -Force -ErrorAction SilentlyContinue
function Git-gs { git status -sb $args }
Set-Alias gs Git-gs
function Git-g { git status -sb $args }
Set-Alias g Git-g
function Git-ga { git add $args }
Set-Alias ga Git-ga
function Git-gaa { git add . $args }
Set-Alias gaa Git-gaa
function Git-gap { git add -p $args }
Set-Alias gap Git-gap
function Git-gp { git pull $args }
Set-Alias gp Git-gp
function Git-gcm { git commit -m $args }
Set-Alias gcm Git-gcm
function Git-gca { git commit --amend $args }
Set-Alias gca Git-gca
function Git-gcane { git commit --amend --no-edit $args }
Set-Alias gcane Git-gcane
function Git-gsu { git submodule update --init --recursive $args }
Set-Alias gsu Git-gsu
function Git-gd { git diff -w -b $args }
Set-Alias gd Git-gd
function Git-gdc { git diff --cached $args }
Set-Alias gdc Git-gdc
function Git-gdw { git diff --color-words $args }
Set-Alias gdw Git-gdw
function Git-gdcw { git diff --cached --color-words $args }
Set-Alias gdcw Git-gdcw
function Git-gsf { git submodule foreach --recursive $args }
Set-Alias gsf Git-gsf
function Git-grhh { git reset HEAD --hard $args }
Set-Alias grhh Git-grhh
function Git-gcfdx { git clean -ffdx $args }
Set-Alias gcfdx Git-gcfdx
function Git-gco { git checkout $args }
Set-Alias gco Git-gco
function Git-gbranch { git for-each-ref --sort=-committerdate refs/heads/ refs/remotes/origin/ --format='%(HEAD) %(color:#ff9ef3)%(refname:short)%(color:reset) - %(color:#00FFFF)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))' $args }
Set-Alias gbranch Git-gbranch
function Git-glg { git lg $args }
Set-Alias glg Git-glg
function Git-glgh { git lg --color=always | head $args }
Set-Alias glgh Git-glgh
function Git-gb { git branch $args }
Set-Alias gb Git-gb
try {
$ErrorActionPreference = 'Stop'
# Setup Git Posh with arguments
# [bool]$ForcePoshGitPrompt,
# [bool]$UseLegacyTabExpansion,
# [bool]$EnableProxyFunctionExpansion
Import-Module posh-git -arg 0,0,1
} catch {
Write-Host "$([char]27)[1;31mError: $([char]27)[0mCannot import module posh-git. Install it with the following command: $([char]27)[1;33mInstall-Module posh-git -Scope CurrentUser$([char]27)[0m"
}
$GitPromptSettings.EnableFileStatus = $false
$GitPromptSettings.DefaultPromptPrefix.Text = '$(Get-Date -f "HH:mm:ss") '
$GitPromptSettings.DefaultPromptPrefix.ForegroundColor = [ConsoleColor]::Magenta
$GitPromptSettings.DefaultPromptSuffix = '$ '
function global:PromptWriteErrorInfo() {
if ($global:GitPromptValues.DollarQuestion) { return }
if ($global:GitPromptValues.LastExitCode) {
"`e[31m(" + $global:GitPromptValues.LastExitCode + ") `e[0m"
}
else {
"`e[31m!`e[0m"
}
}
$global:GitPromptSettings.DefaultPromptBeforeSuffix.Text = ' $(PromptWriteErrorInfo)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment