Skip to content

Instantly share code, notes, and snippets.

@wellington1993
Created July 24, 2025 19:14
Show Gist options
  • Save wellington1993/caed795228c72d08acf9d9a8f95d11e1 to your computer and use it in GitHub Desktop.
Save wellington1993/caed795228c72d08acf9d9a8f95d11e1 to your computer and use it in GitHub Desktop.
Impede push de branches derivados de develop se estiverem desatualizados ou em conflito com ele.
#!/bin/bash
# Branch base do qual outras branches devem derivar
BASE_BRANCH="develop"
REMOTE="origin"
# Nome do branch atual
CURRENT_BRANCH=$(git symbolic-ref --short HEAD)
# Se estiver no próprio develop, não faz validação
[ "$CURRENT_BRANCH" = "$BASE_BRANCH" ] && exit 0
# Atualiza a referência do develop remoto
git fetch $REMOTE $BASE_BRANCH -q
# Verifica se o branch atual deriva de develop
if git merge-base --is-ancestor $REMOTE/$BASE_BRANCH HEAD; then
# Verifica se o branch está desatualizado em relação ao develop
if [ "$(git merge-base HEAD $REMOTE/$BASE_BRANCH)" != "$(git rev-parse $REMOTE/$BASE_BRANCH)" ] ||
! git merge $REMOTE/$BASE_BRANCH --no-commit --no-ff >/dev/null 2>&1; then
echo "Erro: seu branch '$CURRENT_BRANCH' está desatualizado em relação a '$BASE_BRANCH'."
echo "Antes de fazer push, atualize com: git pull $REMOTE $BASE_BRANCH"
git merge --abort >/dev/null 2>&1
exit 1
fi
# Aborta a simulação de merge para manter o repositório limpo
git merge --abort >/dev/null 2>&1
fi
# Tudo certo, libera o push
exit 0
@GabrielEsquilage
Copy link

GabrielEsquilage commented Jul 24, 2025

Acho válido no cenário atual, porém em um ambiente onte costuma-se criar branchs orfãs... seria "skippado" dessa validação, mas dentro de um ambiente comum de trabalho (git checkout -b feature develop por exempĺo) é bem vindo para evitar "perda" de codigo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment