Scheda riepilogativa dei comandi Git
Git registra le modifiche dei file come una serie di commit in un repository. Questi comandi coprono il flusso quotidiano: creare un repo, salvare il lavoro, ramificare, unire e condividere con un remoto. Tieni questa scheda aperta mentre impari.
| Comando | Cosa fa |
|---|---|
| git init | Create a new, empty Git repository in the current directory. |
| git clone | Download a remote repository and its full history to your machine. |
| git status | Show changed, staged and untracked files in the working tree. |
| git add | Stage changes (files) to be included in the next commit. |
| git commit | Record staged changes as a new commit with a message. |
| git log | Display the commit history of the repository. |
| git diff | Show unstaged or staged differences between states. |
| git branch | List, create or delete branches in the repository. |
| git checkout / switch | Switch branches or restore files (switch is the modern command). |
| git merge | Join another branch's history into the current branch. |
| git rebase | Reapply commits on top of another base commit, rewriting history. |
| git pull | Fetch from a remote and merge the changes into the current branch. |
| git push | Upload local commits to a remote repository. |
| git stash | Temporarily shelve uncommitted changes to reapply later. |
| git remote | Manage the set of remote repositories you track. |
Note
- La maggior parte dei comandi accetta flag (come -m per il messaggio o -a per tutti); esegui 'git help <comando>' per l'elenco completo.
- I nomi dei branch costano poco e sono eliminabili — fai commit spesso su un branch di funzionalità, poi unisci in main.
- Prima di comandi distruttivi come rebase o reset, assicurati che il lavoro sia committato o nello stash.
Domande frequenti
- Qual è la differenza tra merge e rebase?
- Merge unisce gli storici con un nuovo commit che ha due genitori. Rebase ripropone i tuoi commit sopra un altro branch, producendo una storia lineare ma riscrivendo gli ID dei commit.
- Come annullo un commit?
- Usa 'git revert <commit>' per aggiungere un nuovo commit che annulla la modifica (sicuro, cronologia condivisa). 'git reset' riscrive la cronologia e non va usato su branch condivisi.
- Qual è la differenza tra git pull e git fetch?
- Fetch scarica commit e riferimenti da un remoto senza toccare il tuo albero di lavoro. Pull fa fetch e poi unisce o fa rebase di quei cambiamenti nel branch corrente.
- Quando dovrei usare git stash?
- Stash accantona temporaneamente le modifiche non committate così puoi cambiare branch o fare pull in pulito, poi le ripristini con 'git stash pop'.