Skip to content

Instantly share code, notes, and snippets.

@weskerty
Last active May 16, 2026 06:19
Show Gist options
  • Select an option

  • Save weskerty/20cf0454c13a460771a5b706b6120cb2 to your computer and use it in GitHub Desktop.

Select an option

Save weskerty/20cf0454c13a460771a5b706b6120cb2 to your computer and use it in GitHub Desktop.
Sincronizacion con Reppositorio, Preferencia por local Siempre sobre escribiendo Remoto en caso de colisión. Límite 1GB, después de eso reinicia el repositorio (borra comits)
#!/usr/bin/env bash
cd "${1:-$(pwd)}" || exit 1
shift 1 2>/dev/null
R='\033[31m';G='\033[32m';Y='\033[33m';B='\033[34m';C='\033[36m';BB='\033[1m';N='\033[0m'
L1=950
L2=900
MSG="$*"
r(){ git "$@" 2>&1; return $?; }
n(){
git ls-remote --exit-code origin HEAD >/dev/null 2>&1 &
P=$!
(sleep 8 && kill $P >/dev/null 2>&1) &
W=$!
wait $P 2>/dev/null
S=$?
kill $W >/dev/null 2>&1
return $S
}
s(){
du -sm . 2>/dev/null | awk '{print $1}'
}
g(){
echo -e "${B}Comprimiendo historial...${N}"
git reflog expire --expire=now --all >/dev/null 2>&1
git gc --aggressive --prune=now >/dev/null 2>&1
git repack -a -d --depth=250 --window=250 >/dev/null 2>&1
git prune --expire=now >/dev/null 2>&1
echo -e "${G} OK${N}\n"
}
d(){
git reflog expire --expire=now --all >/dev/null 2>&1
git gc --prune=now --aggressive >/dev/null 2>&1
git repack -a -d --depth=250 --window=250 >/dev/null 2>&1
git prune --expire=now >/dev/null 2>&1
}
h(){
BR="$1"
echo -e "${R}Proyecto supera ${L1}MB — reiniciando historial...${N}"
git checkout --orphan _tmp_reset >/dev/null 2>&1
git add -A
TS=$(date +"%d/%m/%Y %H:%M")
P=$(uname -o)
[ -z "$MSG" ] && \
git commit -m "$P reset $TS" >/dev/null 2>&1 || \
git commit -m "$P reset $TS - $MSG" >/dev/null 2>&1
git branch -D "$BR" >/dev/null 2>&1
git branch -m "$BR"
git push --force origin "$BR" >/dev/null 2>&1
d
echo -e "${G} Historial reiniciado${N}\n"
}
echo -e "\n=== GIT SYNC ===\n"
[ ! -d .git ] && {
echo -e "${R}No es un repositorio git${N}"
exit 1
}
BR=$(git rev-parse --abbrev-ref HEAD)
echo -e "${C}Rama: ${BB}${BR}${N}\n"
echo -e "${B}Verificando conexion...${N}"
n || {
echo -e "${R}Sin conexion al remoto, abortando${N}\n"
exit 0
}
echo -e "${G} OK${N}\n"
A=0
M=0
D=0
echo -e "${B}Guardando cambios locales...${N}"
git stash push --include-untracked -m sync_auto >/dev/null 2>&1
echo -e "${B}Sincronizando remoto...${N}"
git fetch origin >/dev/null 2>&1
P=$(git pull --ff-only origin "$BR" 2>&1)
if [ $? -ne 0 ]; then
echo "$P" | grep -E "non-fast-forward|diverged|overwrite|rejected" >/dev/null && {
echo -e "${Y} Historial remoto reescrito — forzando sincronizacion...${N}"
git reset --hard origin/"$BR" >/dev/null 2>&1
} || echo " $P"
fi
echo
git stash pop >/dev/null 2>&1
git add -A
ST=$(git diff --cached --name-status)
if [ -n "$ST" ]; then
echo -e "${B}Cambios detectados:${N}"
while read -r L1X; do
S1=$(echo "$L1X" | cut -f1)
[ "$S1" = "A" ] && A=$((A+1))
[ "$S1" = "M" ] && M=$((M+1))
[ "$S1" = "D" ] && D=$((D+1))
[[ "$S1" == R* ]] && M=$((M+1))
done <<< "$ST"
echo -e "${G} + Nuevos: $A${N}"
echo -e "${Y} ~ Modificados: $M${N}"
echo -e "${R} - Eliminados: $D${N}\n"
TS=$(date +"%d/%m/%Y %H:%M")
P=$(uname -o)
[ -z "$MSG" ] && \
git commit -m "$P $TS" >/dev/null 2>&1 || \
git commit -m "$P $TS - $MSG" >/dev/null 2>&1
fi
U=$(git rev-list origin/"$BR"..HEAD --count 2>/dev/null)
[ -z "$U" ] && U=0
if [ "$U" -gt 0 ]; then
echo -e "${B}Subiendo al remoto ($U commit(s) pendiente(s))...${N}"
git push --force-with-lease origin "$BR" >/dev/null 2>&1 || \
echo -e "${R}Push fallido${N}"
elif [ -z "$ST" ]; then
echo -e "${G} Sin cambios locales para subir${N}"
fi
MB=$(s)
echo -e "${C}Proyecto: ${MB}MB${N}\n"
if [ "$MB" -ge "$L1" ]; then
h "$BR"
elif [ "$MB" -ge "$L2" ]; then
g
fi
echo "=== RESUMEN ==="
echo -e "${G}+ Agregados: $A${N}"
echo -e "${Y}~ Modificados: $M${N}"
echo -e "${R}- Eliminados: $D${N}\n"
echo -e "${C}Tamano total: ${MB}MB${N}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment