Last active
July 21, 2022 22:24
-
-
Save stephenbaidu/6595ffc2c1a1845298da7cde950c6df6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Git Helpers | |
function _git_url { | |
return $(git config --get remote.origin.url) | |
} | |
function _git_root { | |
$gitDir = git rev-parse --git-dir | |
if ($null -eq $gitDir) { | |
return $null | |
} | |
elseif ($gitDir -eq ".git") { | |
return $(git rev-parse --show-toplevel) | |
} | |
else { | |
return $gitDir.Split("/.git")[0] | |
} | |
} | |
function root { Set-Location $(_git_root) } | |
function _git_branch { & git branch } | |
New-Alias -Name gb -Value _git_branch -Force -Option AllScope | |
function _git_checkout_master { & git checkout master } | |
New-Alias -Name master -Value _git_checkout_master -Force -Option AllScope | |
New-Alias -Name gb -Value _git_branch -Force -Option AllScope | |
function _git_checkout_main { & git checkout main } | |
New-Alias -Name bmain -Value _git_checkout_main -Force -Option AllScope | |
function _git_status { & git status } | |
New-Alias -Name gs -Value _git_status -Force -Option AllScope | |
New-Alias -Name ss -Value _git_status -Force -Option AllScope | |
function _git_pull { & git pull } | |
New-Alias -Name ll -Value _git_pull -Force -Option AllScope | |
function _git_push { & git push } | |
New-Alias -Name gp -Value _git_push -Force -Option AllScope | |
function _git_push_force { & git push --force } | |
New-Alias -Name gpf -Value _git_push_force -Force -Option AllScope | |
function _git_add_all { & git add . } | |
New-Alias -Name gaa -Value _git_add_all -Force -Option AllScope | |
function _git_commit_amend { & git commit --amend } | |
New-Alias -Name gca -Value _git_commit_amend -Force -Option AllScope | |
function _git_commit_amend_noedit { & git commit --amend --no-edit } | |
New-Alias -Name gcan -Value _git_commit_amend_noedit -Force -Option AllScope | |
function _git_add_all_commit_amend { & git add .; git commit --amend } | |
New-Alias -Name guc -Value _git_add_all_commit_amend -Force -Option AllScope | |
function _git_rebase_continue { & git rebase --continue } | |
New-Alias -Name grc -Value _git_rebase_continue -Force -Option AllScope | |
function _git_add_all_rebase_continue { & git add .; git rebase --continue } | |
New-Alias -Name garc -Value _git_add_all_rebase_continue -Force -Option AllScope | |
function _git_merge_continue { & git merge --continue } | |
New-Alias -Name gmc -Value _git_merge_continue -Force -Option AllScope | |
function _git_stash { & git stash save "Quick stash - $(Get-Date -Format FileDateTimeUniversal)" } | |
New-Alias -Name gss -Value _git_stash -Force -Option AllScope | |
function _git_rebase_skip { & git rebase --skip } | |
New-Alias -Name grs -Value _git_rebase_skip -Force -Option AllScope | |
function _git_rebase_head1 { & git rebase -i HEAD~1 } | |
New-Alias -Name gr1 -Value _git_rebase_head1 -Force -Option AllScope | |
function _git_rebase_head2 { & git rebase -i HEAD~2 } | |
New-Alias -Name gr2 -Value _git_rebase_head2 -Force -Option AllScope | |
function _git_rebase_head3 { & git rebase -i HEAD~3 } | |
New-Alias -Name gr3 -Value _git_rebase_head3 -Force -Option AllScope | |
function _git_rebase_head4 { & git rebase -i HEAD~4 } | |
New-Alias -Name gr4 -Value _git_rebase_head4 -Force -Option AllScope | |
function _git_rebase_head5 { & git rebase -i HEAD~5 } | |
New-Alias -Name gr5 -Value _git_rebase_head5 -Force -Option AllScope | |
function _git_rebase_head6 { & git rebase -i HEAD~6 } | |
New-Alias -Name gr6 -Value _git_rebase_head6 -Force -Option AllScope | |
function _git_log_oneline { & git log --oneline } | |
New-Alias -Name gl -Value _git_log_oneline -Force -Option AllScope | |
New-Alias -Name gh -Value _git_log_oneline -Force -Option AllScope | |
function _git_log_my_commits { & git log --oneline --author="$env:USERNAME" } | |
New-Alias -Name commits -Value _git_log_my_commits -Force -Option AllScope | |
function _git_log_graph_oneline_decorate { & git log --graph --oneline --decorate } | |
New-Alias -Name glg -Value _git_log_graph_oneline_decorate -Force -Option AllScope | |
New-Alias -Name ghg -Value _git_log_graph_oneline_decorate -Force -Option AllScope | |
function _git_submodules { Get-Content .gitmodules } | |
New-Alias -Name gmods -Value _git_submodules -Force -Option AllScope | |
function _git_submodules { Get-Content .gitmodules } | |
New-Alias -Name gsm -Value _git_submodules -Force -Option AllScope | |
function _git_submodule_update_recursive { git submodule update --recursive } | |
New-Alias -Name gsmu -Value _git_submodule_update_recursive -Force -Option AllScope | |
New-Alias -Name grinse -Value _git_repo_rinse -Force -Option AllScope | |
function _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 _git_select_branch { | |
param ( | |
[Parameter(Mandatory = $false, Position = 0)] | |
[string] | |
$RegEx | |
) | |
Write-Host "Fetching branches..." -ForegroundColor Blue | |
[string[]]$branches = _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 _git_select_branches { | |
param ( | |
[Parameter(Mandatory = $false, Position = 0)] | |
[string] | |
$RegEx | |
) | |
Write-Host "Fetching branches..." -ForegroundColor Blue | |
[string[]]$branches = _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 _git_keep_branch { | |
$branch = git rev-parse --abbrev-ref HEAD | |
git checkout -b "$($branch)-keep-$((Get-Date).ToUniversalTime().toString(`"yyyyMMddHHmmss`"))" | |
} | |
function _git_checkout_branch { | |
param ( | |
[Parameter(Mandatory = $false, Position = 0)] | |
[string] | |
$RegEx | |
) | |
$branch = _git_select_branch $RegEx | |
if ($null -ne $branch) { | |
git checkout $branch | |
} | |
} | |
function _git_delete_branch { | |
param ( | |
[Parameter(Mandatory = $false, Position = 0)] | |
[string] | |
$RegEx | |
) | |
$branch = _git_select_branch $RegEx | |
if ($null -ne $branch) { | |
git branch -D $branch | |
} | |
} | |
function _git_delete_branches { | |
param ( | |
[Parameter(Mandatory = $false, Position = 0)] | |
[string] | |
$RegEx | |
) | |
[string[]]$branches = _git_select_branches $RegEx | |
if ($null -ne $branches) { | |
$branches | ForEach-Object { git branch -D $_ } | |
} | |
} | |
function _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 _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 = @( | |
"root: git rev-parse --show-toplevel", | |
"master: git checkout master", | |
"bmain: 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)'", | |
"gsm: git config --file .gitmodules --name-only --get-regexp path" | |
"gsmu: git submodule update --recursive" | |
"grinse: 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