09 - Informazioni
Comandi per ottenere informazioni su repository remoti e riferimenti Git.
git ls-remote
Sintassi
git ls-remote [remote]
git ls-remote [opzioni] [remote] [refs]
Spiegazione
Mostra riferimenti (branch, tag) disponibili su repository remoto senza scaricare dati. Utile per vedere cosa c'è sul server prima di fetch/clone.
Esempi pratici
Lista tutti i riferimenti remoti:
git ls-remote origin
Output:
abc123... HEAD
abc123... refs/heads/main
def456... refs/heads/develop
789ghi... refs/heads/feature-api
012jkl... refs/tags/v1.0.0
345mno... refs/tags/v1.1.0
Lista solo branch:
git ls-remote --heads origin
Output:
abc123... refs/heads/main
def456... refs/heads/develop
789ghi... refs/heads/feature-api
Lista solo tag:
git ls-remote --tags origin
Output:
012jkl... refs/tags/v1.0.0
345mno... refs/tags/v1.1.0
678pqr... refs/tags/v2.0.0
Verifica esistenza branch remoto:
git ls-remote --heads origin feature-login
Se output vuoto → branch non esiste
Lista riferimenti da URL (senza clone):
git ls-remote https://git.emanuelegori.uno/emanuele/boilerbot.git
Utile per vedere repository prima di clonarlo.
Verifica HEAD remoto (branch di default):
git ls-remote --symref origin HEAD
Output:
ref: refs/heads/main HEAD
abc123... HEAD
Uso pratico
Script: Verifica se branch esiste prima di pull
if git ls-remote --heads origin feature-test | grep -q feature-test; then
echo "Branch esiste, pull..."
git pull origin feature-test
else
echo "Branch non esiste!"
fi
Confronta hash locale vs remoto:
# Hash locale
git rev-parse main
# Hash remoto
git ls-remote origin main | cut -f1
# Se diversi → serve pull/push
Lista tag remoti per pattern:
git ls-remote --tags origin | grep "v1\."
Opzioni comuni
| Opzione | Descrizione |
|---|---|
--heads |
Solo branch |
--tags |
Solo tag |
--symref |
Mostra ref simbolici |
--refs |
Solo ref (no dereferenziati) |
Note
- Non richiede autenticazione per repository pubblici
- Veloce: non scarica oggetti
- Utile in script di automazione
git describe
Sintassi
git describe [commit]
git describe [opzioni]
Spiegazione
Crea un nome leggibile per un commit basato sul tag più vicino. Utile per versioning automatico e build numbering.
Esempi pratici
Describe commit corrente:
git describe
Output possibili:
v1.2.0 # Esattamente sul tag
v1.2.0-5-g abc123d # 5 commit dopo tag v1.2.0
Formato: <tag>-<n>-g<hash>
<tag>= tag più vicino<n>= numero commit dopo tagg<hash>= hash abbreviato commit
Describe commit specifico:
git describe abc123
Describe anche se non ci sono tag:
git describe --always
Fallback a hash se non trova tag.
Describe con tag non annotati:
git describe --tags
Default: solo tag annotati.
Describe solo se esatto match (su tag):
git describe --exact-match
Fallisce se commit non ha tag.
Describe con più commit count:
git describe --long
Output:
v1.2.0-0-gabc123d # Anche se esattamente su tag
Describe dirty (con modifiche):
git describe --dirty
# oppure
git describe --dirty=-modified
Output:
v1.2.0-5-gabc123d-modified
Indica working directory con modifiche.
Uso pratico - Versioning automatico
Build script:
#!/bin/bash
VERSION=$(git describe --tags --always --dirty)
echo "Building version: $VERSION"
# Compila con versione embedded
gcc -DVERSION=\"$VERSION\" main.c -o app
Package.json automatico:
# Genera versione da Git
VERSION=$(git describe --tags)
echo "{\"version\": \"$VERSION\"}" > version.json
Release naming:
# Tag release
git tag -a v2.0.0 -m "Release 2.0.0"
# Genera nome release
RELEASE=$(git describe)
zip "release-${RELEASE}.zip" build/*
Output in CI/CD
GitHub Actions / GitLab CI:
- name: Get version
run: |
VERSION=$(git describe --tags --always)
echo "VERSION=$VERSION" >> $GITHUB_ENV
- name: Build with version
run: echo "Building $VERSION"
Opzioni comuni
| Opzione | Descrizione |
|---|---|
--tags |
Include tag lightweight |
--always |
Fallback a hash |
--long |
Sempre formato lungo |
--dirty |
Indica modifiche |
--exact-match |
Solo su tag esatti |
--abbrev=<n> |
Lunghezza hash |
Note
- Richiede almeno un tag nel repository
- Utile per automazione build
- Versioning semantico + commit count
git rev-parse
Sintassi
git rev-parse [opzioni] <args>
Spiegazione
Comando "plumbing" (basso livello) che risolve riferimenti Git in hash SHA-1. Utile in script avanzati.
Esempi pratici
Risolvere riferimento in hash:
git rev-parse HEAD
Output:
abc123def456... (hash completo)
Hash abbreviato:
git rev-parse --short HEAD
Output:
abc123d
Verifica se in repository Git:
git rev-parse --git-dir
Output:
.git
Path repository root:
git rev-parse --show-toplevel
Output:
/home/user/progetti/mio-repo
Nome branch corrente:
git rev-parse --abbrev-ref HEAD
Output:
main
Verifica se ref esiste:
git rev-parse --verify feature-test
Ritorna hash se esiste, errore se no.
Risolvere riferimenti relativi:
git rev-parse HEAD~3 # 3 commit indietro
git rev-parse HEAD^ # Genitore di HEAD
git rev-parse main@{yesterday} # main di ieri
Path da root a directory corrente:
git rev-parse --show-prefix
Output:
src/components/
Verifica se inside work tree:
git rev-parse --is-inside-work-tree
Output: true o false
Uso in script
Script: Verifica repository Git
#!/bin/bash
if ! git rev-parse --git-dir > /dev/null 2>&1; then
echo "Non in repository Git!"
exit 1
fi
Script: Get root repository
#!/bin/bash
ROOT=$(git rev-parse --show-toplevel)
cd "$ROOT"
echo "Nella root: $ROOT"
Script: Branch corrente
#!/bin/bash
BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$BRANCH" = "main" ]; then
echo "Su main - ok per deploy"
else
echo "Non su main - skip deploy"
fi
Script: Hash per tagging
#!/bin/bash
HASH=$(git rev-parse --short HEAD)
docker build -t "myapp:$HASH" .
Opzioni comuni
| Opzione | Descrizione |
|---|---|
--short |
Hash abbreviato |
--git-dir |
Path .git directory |
--show-toplevel |
Root repository |
--abbrev-ref |
Nome ref (non hash) |
--verify |
Verifica esistenza ref |
--is-inside-work-tree |
Check se in work tree |
Note
- Comando "plumbing" per script
- Portabile tra sistemi Git
- Fondamentale per automazione
Casi d'Uso Combinati
Build automatico con versioning
#!/bin/bash
# Get version from tag
VERSION=$(git describe --tags --always)
# Get short hash
HASH=$(git rev-parse --short HEAD)
# Check if clean
if git describe --dirty | grep -q dirty; then
echo "Warning: dirty working directory"
fi
echo "Building version: $VERSION (commit: $HASH)"
Sync script con controllo remoto
#!/bin/bash
# Local hash
LOCAL=$(git rev-parse main)
# Remote hash
REMOTE=$(git ls-remote origin main | cut -f1)
if [ "$LOCAL" = "$REMOTE" ]; then
echo "Already in sync"
else
echo "Diverged - pulling..."
git pull origin main
fi
CI/CD version tagging
#!/bin/bash
# Ottieni info commit
BRANCH=$(git rev-parse --abbrev-ref HEAD)
VERSION=$(git describe --tags --always)
COMMIT=$(git rev-parse --short HEAD)
# Build container con tag multipli
docker build -t "app:$VERSION" \
-t "app:$COMMIT" \
-t "app:latest" .
echo "Built: $BRANCH@$VERSION ($COMMIT)"
🔗 Collegamenti
- Precedente: ← Reset e Undo
- Prossimo: Tabella HTTPS vs SSH →
- Indice: ← Torna all'indice
📝 Riepilogo comandi
# LS-REMOTE
git ls-remote origin # Tutti i riferimenti
git ls-remote --heads origin # Solo branch
git ls-remote --tags origin # Solo tag
git ls-remote origin main # Branch specifico
# DESCRIBE
git describe # Versione da tag
git describe --tags --always # Con fallback
git describe --dirty # + stato dirty
git describe --long # Sempre formato lungo
# REV-PARSE
git rev-parse HEAD # Hash completo
git rev-parse --short HEAD # Hash breve
git rev-parse --abbrev-ref HEAD # Nome branch
git rev-parse --show-toplevel # Root repo
git rev-parse --verify ref # Verifica esistenza