Last active
July 12, 2025 19:36
-
-
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 push origin :$_; } | |
| catch{ git branch -D $_; } | |
| } | |
| 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) | |
| # 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."; | |
| } | |
| # Check if the branch exists remotely | |
| if ($remote) { | |
| $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."; | |
| } | |
| } | |
| else { | |
| Write-Host "Remote deletion not requested."; | |
| } | |
| } | |
| 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