Elenco comandi Git

CampoValore
TipoDispensa/Pillola
AutoreAlessandro Santucci
Data2026-07-09

Concetto

Raccolta dei comandi Git essenziali per la gestione quotidiana di un repository: dalla configurazione iniziale al branching, dal salvataggio delle modifiche alla sincronizzazione con il remoto.


Configurazione iniziale

git config --global user.name "Nome Cognome"
git config --global user.email "email@esempio.com"
git config --list                    # verifica configurazione attiva

Inizializzare e clonare

git init                             # inizializza repo locale
git clone <url>                      # clona repo remoto
git clone <url> --branch <branch>    # clona un branch specifico

Stato e log

git status                           # stato della working tree
git log --oneline --graph            # log compatto con grafo
git diff                             # differenze non staged
git diff --staged                    # differenze in staging area

Staging e commit

git add <file>                       # aggiungi file specifico
git add .                            # aggiungi tutto
git add -p                           # aggiungi in modo interattivo (hunk per hunk)
git commit -m "messaggio"            # commit con messaggio
git commit --amend                   # modifica ultimo commit (messaggio o file)

Branch

git branch                           # lista branch locali
git branch -a                        # lista branch locali + remoti
git branch <nome>                    # crea branch
git switch <nome>                    # passa a branch esistente
git switch -c <nome>                 # crea e passa a nuovo branch
git branch -d <nome>                 # elimina branch (sicuro)
git branch -D <nome>                 # elimina branch forzato

Merge e Rebase

git merge <branch>                   # merge di un branch nel corrente
git merge --no-ff <branch>           # merge con commit di merge esplicito
git rebase <branch>                  # rebase del corrente su un altro branch
git rebase -i HEAD~N                 # rebase interattivo degli ultimi N commit

Remoto

git remote -v                        # lista remote configurati
git remote add origin <url>          # aggiungi remote
git fetch                            # scarica aggiornamenti senza merge
git pull                             # fetch + merge
git pull --rebase                    # fetch + rebase
git push origin <branch>             # push branch sul remote
git push -u origin <branch>          # push e imposta upstream
git push --force-with-lease          # push forzato sicuro

Stash

git stash                            # salva temporaneamente le modifiche
git stash list                       # lista degli stash
git stash pop                        # ripristina ultimo stash e lo rimuove
git stash apply stash@{N}            # applica uno stash specifico
git stash drop stash@{N}             # elimina uno stash specifico

Tag

git tag                              # lista tag
git tag v1.0.0                       # crea tag leggero
git tag -a v1.0.0 -m "Release 1.0"  # crea tag annotato
git push origin --tags               # push di tutti i tag

Undo e reset

git restore <file>                   # scarta modifiche nel working tree
git restore --staged <file>          # togli dal staging senza perdere modifiche
git reset --soft HEAD~1              # annulla ultimo commit, mantieni staged
git reset --mixed HEAD~1             # annulla ultimo commit, mantieni modifiche
git reset --hard HEAD~1              # annulla ultimo commit, perdi modifiche
git revert <hash>                    # crea commit inverso (sicuro su remoto)

Utilità

git cherry-pick <hash>               # applica un commit specifico al branch corrente
git bisect start                     # avvia ricerca binaria di un bug
git shortlog -sn                     # autori ordinati per numero di commit
git blame <file>                     # mostra autore per ogni riga del file
git clean -fd                        # rimuove file/directory non tracciati

Esempio

Flusso tipico su un feature branch:

git switch -c feature/nuova-funzione
# ... lavoro ...
git add .
git commit -m "feat: aggiungi nuova funzione"
git push -u origin feature/nuova-funzione
# apri Pull Request, review, merge
git switch main
git pull
git branch -d feature/nuova-funzione

Workflow Git per progetti Conventional Commits GitHub PR Workflow