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.

Atualizado:

ComandoO que faz
git initCreate a new, empty Git repository in the current directory.
git cloneDownload a remote repository and its full history to your machine.
git statusShow changed, staged and untracked files in the working tree.
git addStage changes (files) to be included in the next commit.
git commitRecord staged changes as a new commit with a message.
git logDisplay the commit history of the repository.
git diffShow unstaged or staged differences between states.
git branchList, create or delete branches in the repository.
git checkout / switchSwitch branches or restore files (switch is the modern command).
git mergeJoin another branch's history into the current branch.
git rebaseReapply commits on top of another base commit, rewriting history.
git pullFetch from a remote and merge the changes into the current branch.
git pushUpload local commits to a remote repository.
git stashTemporarily shelve uncommitted changes to reapply later.
git remoteManage the set of remote repositories you track.

Notas

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'.