Cheatsheet de comandos Git
O Git registra mudanças nos arquivos como uma série de commits em um repositório. Esses comandos cobrem o fluxo diário: criar um repo, salvar trabalho, ramificar, mesclar e compartilhar com um remoto. Deixe esta folha aberta enquanto aprende.
| Comando | O que faz |
|---|---|
| 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. |
Notas
- A maioria dos comandos aceita flags (como -m para mensagem ou -a para todos); rode 'git help <comando>' para a lista completa.
- Nomes de branch são baratos e descartáveis — faça commits frequentes em um branch de recurso e depois mescle em main.
- Antes de comandos destrutivos como rebase ou reset, certifique-se de que seu trabalho está commitado ou no stash.
Perguntas frequentes
- Qual a diferença entre merge e rebase?
- Merge combina históricos com um novo commit que tem dois pais. Rebase reproduz seus commits sobre outra branch, gerando um histórico linear mas reescrevendo os IDs de commit.
- Como desfaço um commit?
- Use 'git revert <commit>' para adicionar um novo commit que desfaz a mudança (seguro, histórico compartilhado). 'git reset' reescreve o histórico e não deve ser usado em branches compartilhados.
- Qual a diferença entre git pull e git fetch?
- Fetch baixa commits e refs de um remoto sem tocar sua árvore de trabalho. Pull faz fetch e então mescla ou faz rebase dessas mudanças na branch atual.
- Quando devo usar git stash?
- Stash guarda temporariamente alterações não commitadas para você trocar de branch ou fazer pull limpo, depois restaure com 'git stash pop'.