Comandi Git essenziali per commit, branch, merge, rebase, remote e altro.
git config --global user.name "Name"Configurazione e installazioneSet global author name
es. git config --global user.name "Jane Doe"
git config --global user.email "email"Configurazione e installazioneSet global author email
es. git config --global user.email "[email protected]"
git config --listConfigurazione e installazioneList all configuration settings
git config --global core.editor "code"Configurazione e installazioneSet VS Code as default editor
git config --global alias.<alias> <cmd>Configurazione e installazioneCreate a command alias
es. git config --global alias.st status
git initConfigurazione e installazioneInitialize a new local repository
git init <dir>Configurazione e installazioneInitialize a repo in a specific directory
es. git init my-project
git clone <url>Configurazione e installazioneClone a remote repository
es. git clone https://github.com/user/repo.git
git clone <url> <dir>Configurazione e installazioneClone into a specific folder
es. git clone https://github.com/user/repo.git my-dir
git statusStaging e basiShow working tree status
git status -sStaging e basiShort/compact status output
git add <file>Staging e basiStage a specific file
es. git add README.md
git add .Staging e basiStage all changes in current directory
git add -pStaging e basiInteractively stage hunks of changes
git commit -m "msg"Staging e basiCommit staged changes with a message
es. git commit -m "feat: add login page"
git commit -am "msg"Staging e basiStage tracked files and commit in one step
git commit --amenddistruttivoStaging e basiModify the most recent commit (message or content)
git diffStaging e basiShow unstaged changes
git diff --stagedStaging e basiShow staged changes (vs last commit)
git diff <branch1>..<branch2>Staging e basiCompare two branches
es. git diff main..feature
git rm <file>Staging e basiRemove a file from working tree and index
es. git rm old-file.txt
git rm --cached <file>Staging e basiUntrack a file without deleting it
es. git rm --cached secret.txt
git mv <old> <new>Staging e basiMove or rename a tracked file
es. git mv old.js new.js
git branchBranching e mergeList local branches
git branch -aBranching e mergeList all branches (local + remote)
git branch <name>Branching e mergeCreate a new branch
es. git branch feature/auth
git checkout <branch>Branching e mergeSwitch to an existing branch
es. git checkout main
git checkout -b <branch>Branching e mergeCreate and switch to a new branch
es. git checkout -b feature/auth
git switch <branch>Branching e mergeSwitch branches (modern syntax)
es. git switch main
git switch -c <branch>Branching e mergeCreate and switch (modern syntax)
es. git switch -c feature/auth
git branch -d <branch>Branching e mergeDelete a merged branch
es. git branch -d feature/auth
git branch -D <branch>distruttivoBranching e mergeForce delete a branch (unmerged too)
es. git branch -D feature/old
git branch -m <old> <new>Branching e mergeRename a branch
es. git branch -m old-name new-name
git merge <branch>Branching e mergeMerge a branch into current branch
es. git merge feature/auth
git merge --no-ff <branch>Branching e mergeMerge with a merge commit (no fast-forward)
git merge --squash <branch>Branching e mergeSquash all commits from branch into one staged change
git merge --abortBranching e mergeAbort an in-progress merge
git remote -vOperazioni remoteList remote connections with URLs
git remote add <name> <url>Operazioni remoteAdd a new remote
es. git remote add origin https://github.com/user/repo.git
git remote remove <name>Operazioni remoteRemove a remote connection
es. git remote remove origin
git remote rename <old> <new>Operazioni remoteRename a remote
es. git remote rename origin upstream
git fetchOperazioni remoteDownload all changes from remote (no merge)
git fetch <remote>Operazioni remoteFetch from a specific remote
es. git fetch origin
git pullOperazioni remoteFetch and merge changes from remote
git pull --rebaseOperazioni remoteFetch and rebase onto remote branch
git push <remote> <branch>Operazioni remotePush branch to remote
es. git push origin main
git push -u origin <branch>Operazioni remotePush and set upstream tracking
es. git push -u origin feature/auth
git push --force-with-leasedistruttivoOperazioni remoteSafe force push (fails if others pushed)
git push --tagsOperazioni remotePush all tags to remote
git push origin --delete <branch>distruttivoOperazioni remoteDelete a remote branch
es. git push origin --delete feature/old
git restore <file>distruttivoAnnullare le modificheDiscard changes in working directory
es. git restore index.html
git restore --staged <file>Annullare le modificheUnstage a file (keep changes)
es. git restore --staged index.html
git reset HEAD <file>Annullare le modificheUnstage a file (older syntax)
es. git reset HEAD index.html
git reset --soft HEAD~1Annullare le modificheUndo last commit, keep changes staged
git reset --mixed HEAD~1Annullare le modificheUndo last commit, unstage changes (default)
git reset --hard HEAD~1distruttivoAnnullare le modificheUndo last commit, discard all changes
git reset --hard <commit>distruttivoAnnullare le modificheReset to a specific commit, discard everything after
git revert <commit>Annullare le modificheCreate a new commit that undoes a specific commit
es. git revert a1b2c3d
git revert HEADAnnullare le modificheRevert the most recent commit
git clean -fddistruttivoAnnullare le modificheRemove untracked files and directories
git clean -nAnnullare le modificheDry run: show what would be cleaned
git stashStashStash current working directory changes
git stash push -m "msg"StashStash with a descriptive message
es. git stash push -m "WIP: auth feature"
git stash listStashList all stashes
git stash popStashApply most recent stash and remove it
git stash apply stash@{n}StashApply a specific stash without removing
es. git stash apply stash@{0}
git stash drop stash@{n}StashDelete a specific stash
es. git stash drop stash@{0}
git stash cleardistruttivoStashRemove all stashes
git stash branch <branch>StashCreate a branch from a stash
es. git stash branch feature/from-stash
git rebase <branch>Rebase e avanzatoRebase current branch onto another
es. git rebase main
git rebase -i HEAD~nRebase e avanzatoInteractive rebase for last n commits
es. git rebase -i HEAD~3
git rebase --continueRebase e avanzatoContinue rebase after resolving conflicts
git rebase --abortRebase e avanzatoAbort an in-progress rebase
git cherry-pick <commit>Rebase e avanzatoApply a specific commit to current branch
es. git cherry-pick a1b2c3d
git cherry-pick <c1>..<c2>Rebase e avanzatoApply a range of commits
es. git cherry-pick a1b2c3d..f6e5d4c
git bisect startRebase e avanzatoStart binary search for a bug
git bisect good <commit>Rebase e avanzatoMark a commit as good in bisect
git bisect bad <commit>Rebase e avanzatoMark a commit as bad in bisect
git bisect resetRebase e avanzatoEnd bisect session
git worktree add <path> <branch>Rebase e avanzatoCheck out a branch into a separate directory
git logIspezione e logShow commit history
git log --onelineIspezione e logCompact one-line log
git log --oneline --graph --allIspezione e logVisual branch graph in terminal
git log -n <count>Ispezione e logShow last N commits
es. git log -n 5
git log --author="name"Ispezione e logFilter commits by author
es. git log --author="Jane Doe"
git log --since="2 weeks ago"Ispezione e logShow commits since a date
git log -p <file>Ispezione e logShow changes to a specific file over time
es. git log -p README.md
git log --follow <file>Ispezione e logFollow renames of a file in history
git show <commit>Ispezione e logShow details of a specific commit
es. git show a1b2c3d
git blame <file>Ispezione e logShow who last modified each line
es. git blame src/app.tsx
git shortlog -snIspezione e logSummarize commits by author
git reflogIspezione e logShow history of HEAD movements (recovery tool)
git grep "pattern"Ispezione e logSearch working directory for a pattern
es. git grep "TODO"
git tagTagList all tags
git tag <name>TagCreate a lightweight tag
es. git tag v1.0.0
git tag -a <name> -m "msg"TagCreate an annotated tag
es. git tag -a v1.0.0 -m "Release v1.0.0"
git tag -a <name> <commit>TagTag a past commit
es. git tag -a v0.9.0 a1b2c3d
git show <tag>TagShow tag metadata and commit
es. git show v1.0.0
git push origin <tag>TagPush a specific tag to remote
es. git push origin v1.0.0
git push origin --tagsTagPush all tags to remote
git tag -d <tag>TagDelete a local tag
es. git tag -d v1.0.0
git push origin --delete <tag>TagDelete a remote tag
es. git push origin --delete v1.0.0
git merge combina due branch creando un commit di merge, preservando la storia completa di entrambi i branch. git rebase sposta i commit da un branch all'altro, riscrivendo la storia per creare una storia di commit lineare e più pulita. Usa merge per branch pubblici e lavoro condiviso; usa rebase per ripulire branch di funzionalità locali prima di fare il merge.
Usa git reset --soft HEAD~1 per annullare l'ultimo commit mantenendo le modifiche in staging. Usa git reset HEAD~1 (o --mixed) per rimuovere le modifiche dallo staging ma mantenerle nella directory di lavoro. Usa git reset --hard HEAD~1 per scartare definitivamente il commit e tutte le modifiche (operazione distruttiva).
git fetch scarica le modifiche dal remoto ma NON le unisce nel branch corrente. Aggiorna i branch di tracciamento remoto (origin/main). git pull è git fetch + git merge (o git rebase con --rebase). Usa fetch quando vuoi ispezionare le modifiche prima di integrarle.
Dopo un merge/rebase conflittuale, Git contrassegna i file in conflitto con i marcatori '<<<<<<', =======, >>>>>>. Apri ogni file in conflitto, scegli manualmente quali modifiche mantenere, rimuovi i marcatori, poi aggiungi i file risolti con git add '<'file> e completa con git merge --continue o git commit.