Skip to content

Instantly share code, notes, and snippets.

@stephenbaidu
Created February 17, 2022 08:50
Show Gist options
  • Save stephenbaidu/5af55da1bde7ec82052ffca55c811fbd to your computer and use it in GitHub Desktop.
Save stephenbaidu/5af55da1bde7ec82052ffca55c811fbd to your computer and use it in GitHub Desktop.
# Git Helpers
function fn_git_branch { & git branch }
New-Alias -Name gb -Value fn_git_branch -Force -Option AllScope
function fn_git_checkout_master { & git checkout master }
New-Alias -Name master -Value fn_git_checkout_master -Force -Option AllScope
New-Alias -Name gb -Value fn_git_branch -Force -Option AllScope
function fn_git_checkout_main { & git checkout main }
New-Alias -Name main -Value fn_git_checkout_main -Force -Option AllScope
function fn_git_status { & git status }
New-Alias -Name gs -Value fn_git_status -Force -Option AllScope
New-Alias -Name ss -Value fn_git_status -Force -Option AllScope
function fn_git_pull { & git pull }
New-Alias -Name ll -Value fn_git_pull -Force -Option AllScope
function fn_git_push { & git push }
New-Alias -Name gp -Value fn_git_push -Force -Option AllScope
function fn_git_push_force { & git push --force }
New-Alias -Name gpf -Value fn_git_push_force -Force -Option AllScope
function fn_git_add_all { & git add . }
New-Alias -Name gaa -Value fn_git_add_all -Force -Option AllScope
function fn_git_commit_amend { & git commit --amend }
New-Alias -Name gca -Value fn_git_commit_amend -Force -Option AllScope
function fn_git_commit_amend_noedit { & git commit --amend --no-edit }
New-Alias -Name gcan -Value fn_git_commit_amend_noedit -Force -Option AllScope
function fn_git_add_all_commit_amend { & git add .; git commit --amend }
New-Alias -Name guc -Value fn_git_add_all_commit_amend -Force -Option AllScope
function fn_git_rebase_continue { & git rebase --continue }
New-Alias -Name grc -Value fn_git_rebase_continue -Force -Option AllScope
function fn_git_add_all_rebase_continue { & git add .; git rebase --continue }
New-Alias -Name garc -Value fn_git_add_all_rebase_continue -Force -Option AllScope
function fn_git_merge_continue { & git merge --continue }
New-Alias -Name gmc -Value fn_git_merge_continue -Force -Option AllScope
function fn_git_stash { & git stash save "Quick stash - $(Get-Date -Format FileDateTimeUniversal)" }
New-Alias -Name gss -Value fn_git_stash -Force -Option AllScope
function fn_git_rebase_skip { & git rebase --skip }
New-Alias -Name grs -Value fn_git_rebase_skip -Force -Option AllScope
function fn_git_rebase_head1 { & git rebase -i HEAD~1 }
New-Alias -Name gr1 -Value fn_git_rebase_head1 -Force -Option AllScope
function fn_git_rebase_head2 { & git rebase -i HEAD~2 }
New-Alias -Name gr2 -Value fn_git_rebase_head2 -Force -Option AllScope
function fn_git_rebase_head3 { & git rebase -i HEAD~3 }
New-Alias -Name gr3 -Value fn_git_rebase_head3 -Force -Option AllScope
function fn_git_rebase_head4 { & git rebase -i HEAD~4 }
New-Alias -Name gr4 -Value fn_git_rebase_head4 -Force -Option AllScope
function fn_git_rebase_head5 { & git rebase -i HEAD~5 }
New-Alias -Name gr5 -Value fn_git_rebase_head5 -Force -Option AllScope
function fn_git_rebase_head6 { & git rebase -i HEAD~6 }
New-Alias -Name gr6 -Value fn_git_rebase_head6 -Force -Option AllScope
function fn_git_log_oneline { & git log --oneline }
New-Alias -Name gl -Value fn_git_log_oneline -Force -Option AllScope
New-Alias -Name gh -Value fn_git_log_oneline -Force -Option AllScope
New-Alias -Name allcommits -Value fn_git_log_oneline -Force -Option AllScope
function fn_git_log_my_commits { & git log --oneline --author="$env:USERNAME" }
New-Alias -Name commits -Value fn_git_log_my_commits -Force -Option AllScope
function fn_git_log_graph_oneline_decorate { & git log --graph --oneline --decorate }
New-Alias -Name glg -Value fn_git_log_graph_oneline_decorate -Force -Option AllScope
New-Alias -Name ghg -Value fn_git_log_graph_oneline_decorate -Force -Option AllScope
function fn_git_submodules { Get-Content .gitmodules }
New-Alias -Name gmods -Value fn_git_submodules -Force -Option AllScope
function fn_git_submodules_names { Get-Content .gitmodules }
New-Alias -Name gsubs -Value fn_git_submodules_names -Force -Option AllScope
New-Alias -Name gmods -Value fn_git_submodules -Force -Option AllScope
function fn_git_submodule_update { git submodule update }
New-Alias -Name gsubu -Value fn_git_submodule_update -Force -Option AllScope
New-Alias -Name gmods -Value fn_git_submodules -Force -Option AllScope
New-Alias -Name grr -Value fn_git_repo_rinse -Force -Option AllScope
function fn_git_list_branches {
param (
[Parameter(Mandatory=$false, Position=0)]
[string]
$RegEx
)
[string[]]$refs = @()
if ($RegEx) {
$refs = git for-each-ref --format='%(refname:short)' refs/heads | Where-Object {$_ -match $RegEx}
} else {
$refs = git for-each-ref --format='%(refname:short)' refs/heads
}
[string[]]$branches = $refs | Where-Object {$_ -ne "master"}
return $branches
}
function fn_git_select_branch {
param (
[Parameter(Mandatory=$false, Position=0)]
[string]
$RegEx
)
Write-Host "Fetching branches..." -ForegroundColor Blue
[string[]]$branches = fn_git_list_branches $RegEx
if ($null -eq $branches) {
Write-Host "No branches found for pattern $RegEx." -ForegroundColor Yellow
break
}
Write-Host "Select branch or <Enter> to ignore" -ForegroundColor Blue
(1..($branches.count)) | ForEach-Object {
$index = $_ - 1
$branch = $branches[$index]
Write-Host " $_. $branch" -ForegroundColor Blue
}
[uint16]$choice = Read-Host
if ($choice -in 1..$branches.Count) {
return $branches[$choice - 1]
} else {
Write-Host "No branch selected" -ForegroundColor Blue
return $null
}
}
function fn_git_select_branches {
param (
[Parameter(Mandatory=$false, Position=0)]
[string]
$RegEx
)
Write-Host "Fetching branches..." -ForegroundColor Blue
[string[]]$branches = fn_git_list_branches $RegEx
if ($null -eq $branches) {
Write-Host "No branches found for pattern $RegEx." -ForegroundColor Yellow
return $null
}
Write-Host "Select branches:" -ForegroundColor Blue
(1..($branches.count)) | ForEach-Object {
$index = $_ - 1
$branch = $branches[$index]
Write-Host " $_. $branch" -ForegroundColor Blue
}
Write-Host "Enter numbers with comma eg; 2,4,3: " -ForegroundColor Blue
[string]$branchNumbersAsCommaSeparated = Read-Host
if ($null -eq $branchNumbersAsCommaSeparated) {
Write-Host "No branch selected" -ForegroundColor Blue
return $null
}
[uint16[]]$choices = $branchNumbersAsCommaSeparated -split ","
[string[]]$seletedBranches = @()
if (($choices.Count -eq 1) -and ($choices[0] -eq 0)) {
Write-Host "No branch selected" -ForegroundColor Blue
return $null
}
$choices | ForEach-Object {
if ($_ -in 1..$branches.Count) {
$seletedBranches += $branches[$_ - 1]
} else {
Write-Host "Invalid number entered: $_" -ForegroundColor Blue
return $null
}
}
return $seletedBranches
}
function fn_git_keep_branch {
$branch = git rev-parse --abbrev-ref HEAD
git checkout -b "$($branch)-keep-$((Get-Date).ToUniversalTime().toString(`"yyyyMMddHHmmss`"))"
}
function fn_git_checkout_branch {
param (
[Parameter(Mandatory=$false, Position=0)]
[string]
$RegEx
)
$branch = fn_git_select_branch $RegEx
if ($null -ne $branch) {
git checkout $branch
}
}
function fn_git_delete_branch {
param (
[Parameter(Mandatory=$false, Position=0)]
[string]
$RegEx
)
$branch = fn_git_select_branch $RegEx
if ($null -ne $branch) {
git branch -D $branch
}
}
function fn_git_delete_branches {
param (
[Parameter(Mandatory=$false, Position=0)]
[string]
$RegEx
)
[string[]]$branches = fn_git_select_branches $RegEx
if ($null -ne $branches) {
$branches | ForEach-Object { git branch -D $_ }
}
}
function fn_git_fetch_tag {
param (
[Parameter(Mandatory=$true, Position=0)]
[string]
$Tag
)
fn_write_host "Fetching tag $Tag ..."
git fetch origin $Tag
git tag $Tag FETCH_HEAD
}
function fn_git_repo_rinse {
git clean -xfd
git submodule foreach --recursive git clean -xfd
git reset --hard
git submodule foreach --recursive git reset --hard
git submodule update --init --recursive
}
function _git {
$commands = @(
"master: git checkout master",
"main: git checkout main",
"gb: git branch",
"gs/ss: git status",
"ll: git pull",
"gp: git push",
"gpf: git push --force",
"gaa: git add .",
"gca: git commit --amend",
"gcan: git commit --amend --no-edit",
"guc: git add .; git commit --amend",
"gr: git rebase --continue",
"garc: git add .; git rebase --continue",
"gmc: git merge --continue",
"gss: git stash save 'Quick stash - <FileDateTimeUniversal>'",
"grs: git rebase --skip",
"gr1 ->6: git rebase -i HEAD~1 -> git rebase -i HEAD~6",
"gl/gh: git log --oneline",
"glg/ghg: git log --graph --oneline --decorate",
"commits: git log --oneline --author='$($env:USERNAME)'",
"allcommits: git log --oneline"
"gmods: cat .gitmodules"
"gsubs: git config --file .gitmodules --name-only --get-regexp path"
"gsubu: git submodule update"
"grr: Repo rinse"
)
Write-Host "Git Helpers" -ForegroundColor Blue
foreach ($command in $commands) {
Write-Host " $command"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment