Git Cheatsheet
Referência rápida dos comandos Git mais usados, com descrição em português.
Configuração
| git config --global user.name "Nome" | Define o nome do usuário globalmente | |
| git config --global user.email "email@exemplo.com" | Define o e-mail globalmente | |
| git config --list | Lista todas as configurações | |
| git config --global core.editor "code --wait" | Define o editor padrão (VS Code) |
Inicializar & Clonar
| git init | Inicia um novo repositório | |
| git clone <url> | Clona um repositório remoto | |
| git clone <url> <pasta> | Clona em uma pasta específica | |
| git clone --depth 1 <url> | Clone superficial (apenas último commit) |
Stage & Commit
| git status | Mostra o estado dos arquivos | |
| git add <arquivo> | Adiciona arquivo ao stage | |
| git add . | Adiciona tudo ao stage | |
| git add -p | Adiciona partes interativamente | |
| git commit -m "mensagem" | Cria um commit com mensagem | |
| git commit --amend | Altera o último commit | |
| git commit --amend --no-edit | Altera o último commit sem mudar a mensagem | |
| git reset HEAD <arquivo> | Remove arquivo do stage | |
| git checkout -- <arquivo> | Descarta alterações no arquivo |
Branches
| git branch | Lista branches locais | |
| git branch -a | Lista todos os branches (local + remoto) | |
| git branch <nome> | Cria um novo branch | |
| git checkout <branch> | Muda para o branch | |
| git checkout -b <branch> | Cria e muda para o branch | |
| git switch <branch> | Muda para o branch (moderno) | |
| git switch -c <branch> | Cria e muda para o branch (moderno) | |
| git branch -d <branch> | Deleta branch (seguro) | |
| git branch -D <branch> | Força exclusão do branch | |
| git branch -m <novo-nome> | Renomeia o branch atual |
Merge & Rebase
| git merge <branch> | Mescla o branch no atual | |
| git merge --no-ff <branch> | Merge sem fast-forward | |
| git merge --squash <branch> | Squash commits ao mesclar | |
| git rebase <branch> | Rebasa o branch atual sobre outro | |
| git rebase -i HEAD~3 | Rebase interativo dos últimos 3 commits | |
| git rebase --abort | Aborta o rebase em curso | |
| git rebase --continue | Continua o rebase após resolver conflitos |
Remoto
| git remote -v | Lista remotes com URLs | |
| git remote add origin <url> | Adiciona remote "origin" | |
| git fetch | Busca alterações sem mesclar | |
| git fetch --prune | Busca e remove branches remotos excluídos | |
| git pull | Busca e mescla do remoto | |
| git pull --rebase | Busca e rebasa em vez de mesclar | |
| git push | Envia commits para o remoto | |
| git push -u origin <branch> | Envia e define o upstream | |
| git push --force-with-lease | Push forçado (seguro) | |
| git push origin --delete <branch> | Deleta branch no remoto |
Log & Histórico
| git log | Mostra histórico de commits | |
| git log --oneline | Histórico resumido (uma linha) | |
| git log --oneline --graph | Histórico com grafo de branches | |
| git log -p <arquivo> | Histórico com diff do arquivo | |
| git log --author="Nome" | Filtra commits por autor | |
| git log --since="2 weeks ago" | Commits nas últimas 2 semanas | |
| git show <commit> | Mostra detalhes de um commit | |
| git diff | Mostra diferenças não staged | |
| git diff --staged | Mostra diferenças staged | |
| git diff <branch1>..<branch2> | Diff entre dois branches | |
| git blame <arquivo> | Mostra autoria linha a linha |
Stash
| git stash | Guarda alterações temporariamente | |
| git stash push -m "descrição" | Stash com descrição | |
| git stash list | Lista todos os stashes | |
| git stash pop | Recupera o último stash | |
| git stash apply stash@{2} | Aplica stash específico | |
| git stash drop stash@{0} | Remove um stash | |
| git stash clear | Remove todos os stashes |
Desfazer
| git revert <commit> | Cria commit que desfaz o commit | |
| git reset --soft HEAD~1 | Desfaz o último commit, mantém no stage | |
| git reset --mixed HEAD~1 | Desfaz o último commit, remove do stage | |
| git reset --hard HEAD~1 | Desfaz o último commit e as alterações | |
| git clean -fd | Remove arquivos não rastreados | |
| git restore <arquivo> | Descarta alterações no arquivo (moderno) | |
| git restore --staged <arquivo> | Remove do stage (moderno) |
Tags
| git tag | Lista todas as tags | |
| git tag <nome> | Cria tag leve | |
| git tag -a v1.0 -m "versão 1.0" | Cria tag anotada | |
| git push origin <tag> | Envia tag ao remoto | |
| git push origin --tags | Envia todas as tags | |
| git tag -d <tag> | Deleta tag local | |
| git push origin --delete <tag> | Deleta tag no remoto |