Last active
December 16, 2025 16:20
-
-
Save GustavoAmerico/dd2da52b6a8b2da5db3df45bb0439d05 to your computer and use it in GitHub Desktop.
Essa função é responsável por centralizar alguns comandos verbosos do git
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
| #Author: Gustavo Américo | |
| #Repository: https://gist.github.com/GustavoAmerico | |
| function Git-Update-All-BranchsFromBase { | |
| #Esse script é responsável por executar um merge automático de uma branch base para todas as outras branchs. Em caso de conflitos o script faz rollback e pula a branch conflitante | |
| param([Parameter(Mandatory = $true)][String]$branchNamePattern, $remoteName = 'origin', $branchBase = 'master') | |
| $branchsToUpdate = (git branch --all | % { $_.ToString().Replace("remotes/$remoteName/", '').Trim() } | Select-String -Pattern $branchNamePattern); | |
| $branchsToUpdate | % { | |
| git checkout $_; | |
| git pull --all; | |
| git merge "$remoteName/$branchBase" --no-ff --no-edit; | |
| if (-not $?) { | |
| git merge --abort; | |
| git reset --hard | |
| }else { | |
| git push | |
| } | |
| } | |
| } | |
| function Git-Remove-Merged-Branchs { | |
| #ATENÇÃO: Esse comando não verifica se as alterações forma mescladas com a branch principal!! O branch vai ser apagado | |
| #Faça checkout na branch que você deseja utilizar como base | |
| param([Parameter(Mandatory = $true)][String]$branchNamePattern, $remoteName = 'origin') | |
| git pull --all; | |
| git branch --all --merged | Select-String -Pattern $branchNamePattern | % { | |
| $_.ToString().Replace("remotes/$remoteName/", '').Trim() | |
| } | % { | |
| try{ | |
| Git-DeleteBranch -BranchName $_ -remoteName $remoteName -Remote | |
| } | |
| catch{ | |
| Write-Host "Fail on delete $_ on remote $remoteName" | |
| } | |
| } | |
| git fetch --prune | |
| } | |
| function Git-DeleteBranch { | |
| <# | |
| .DESCRIPTION | |
| Deletes local and remote branches that match a given pattern and have no changes. | |
| #> | |
| [CmdletBinding()] | |
| param([Parameter(ValueFromPipeline, Mandatory = $true)][String]$BranchName, | |
| [Parameter(Mandatory = $False)][String]$remoteName = 'origin', | |
| [Parameter(Mandatory = $false)][Switch]$Remote) | |
| git fetch --prune; | |
| # Check if the branch exists remotely | |
| if ($remote) { | |
| Git-DeleteRemoteBranch -branchName $branchName -remoteName $remoteName | |
| } | |
| else { | |
| Write-Host "Remote deletion not requested."; | |
| } | |
| # Check if the branch exists locally | |
| Git-DeleteLocalBranch $branchName | |
| } | |
| Function Git-DeleteRemoteBranch{ | |
| [CmdletBinding()] | |
| param([Parameter(ValueFromPipeline, Mandatory = $true)][string]$branchName, | |
| [Parameter(Mandatory = $False)][String]$remoteName = 'origin') | |
| git fetch --prune; | |
| $remoteBranchExists = (git branch -r --list | Select-String -Pattern $branchName -Raw); | |
| if ($remoteBranchExists) { | |
| Write-Host "Deleting remote branch: $branchName"; | |
| git push $remoteName --delete $branchName; | |
| } | |
| else { | |
| Write-Host "Remote branch '$branchName' does not exist."; | |
| } | |
| } | |
| Function Git-DeleteLocalBranch { | |
| [CmdletBinding()] | |
| param([Parameter(ValueFromPipeline, Mandatory = $true)][string]$branchName) | |
| git fetch --prune; | |
| # Check if the branch exists locally | |
| $localBranchExists = git branch --list | Select-String -Pattern $branchName; | |
| if ($localBranchExists) { | |
| Write-Host "Deleting local branch: $branchName"; | |
| git branch -D $branchName; | |
| } | |
| else { | |
| Write-Host "Local branch '$branchName' does not exist."; | |
| } | |
| } | |
| function Git-BranchshWithoutFilesChanges { | |
| <# | |
| .DESCRIPTION | |
| Essa função busca branches que não possuem arquivos alterados em relação ao branch atual. | |
| #> | |
| [CmdletBinding()] | |
| param([Parameter(ValueFromPipeline, Mandatory = $true)][string]$branchNamePattern) | |
| git fetch --prune; | |
| if ( -not $branchNamePattern) { | |
| Write-Error "Branch name pattern is required."; | |
| return; | |
| } | |
| $allBranchs = $(git branch --all -l | % { $_.Trim() } | |
| | Select-String -Pattern $branchNamePattern); | |
| $remoteRef = ("remotes/"); | |
| $bWithoutFiles = $($allBranchs | % { $filesChanged = (git diff --name-only $_); if (-not $filesChanged) { return ("$_" -replace $remoteRef, '') } } | Select-String -Pattern $branchNamePattern -Raw); | |
| return $bWithoutFiles; | |
| } | |
| function Git-CurrentBranch { | |
| return ((git branch | Select-String -Pattern '\*' -Raw) -replace "\*", "").Trim(); | |
| } | |
| function Git-DeleteBranchhWithoutFilesChanges { | |
| <#ATENÇÃO: Esse comando não verifica se as alterações forma mescladas com a branch principal!! O branch vai ser apagado | |
| #Faça checkout na branch que você deseja utilizar como base | |
| #> | |
| [CmdletBinding()] | |
| param([Parameter(Mandatory = $true)][String]$branchNamePattern) | |
| git fetch --prune; | |
| Git-BranchshWithoutFilesChanges -branchNamePattern $branchNamePattern | % { | |
| $remoteName = ("$_" -split '/')[0]; | |
| $branchName = ("$_" -replace "$remoteName/", '') | |
| Git-DeleteBranch -BranchName $branchName -Remote -remoteName $remoteName; | |
| } | |
| git fetch --prune | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment