Comandos Git essenciais para commits, ramos, merges, rebases, remotos e mais.
git config --global user.name "Name"Configuração e instalaçãoSet global author name
ex. git config --global user.name "Jane Doe"
git config --global user.email "email"Configuração e instalaçãoSet global author email
ex. git config --global user.email "[email protected]"
git config --listConfiguração e instalaçãoList all configuration settings
git config --global core.editor "code"Configuração e instalaçãoSet VS Code as default editor
git config --global alias.<alias> <cmd>Configuração e instalaçãoCreate a command alias
ex. git config --global alias.st status
git initConfiguração e instalaçãoInitialize a new local repository
git init <dir>Configuração e instalaçãoInitialize a repo in a specific directory
ex. git init my-project
git clone <url>Configuração e instalaçãoClone a remote repository
ex. git clone https://github.com/user/repo.git
git clone <url> <dir>Configuração e instalaçãoClone into a specific folder
ex. git clone https://github.com/user/repo.git my-dir
git statusStaging e básicoShow working tree status
git status -sStaging e básicoShort/compact status output
git add <file>Staging e básicoStage a specific file
ex. git add README.md
git add .Staging e básicoStage all changes in current directory
git add -pStaging e básicoInteractively stage hunks of changes
git commit -m "msg"Staging e básicoCommit staged changes with a message
ex. git commit -m "feat: add login page"
git commit -am "msg"Staging e básicoStage tracked files and commit in one step
git commit --amenddestrutivoStaging e básicoModify the most recent commit (message or content)
git diffStaging e básicoShow unstaged changes
git diff --stagedStaging e básicoShow staged changes (vs last commit)
git diff <branch1>..<branch2>Staging e básicoCompare two branches
ex. git diff main..feature
git rm <file>Staging e básicoRemove a file from working tree and index
ex. git rm old-file.txt
git rm --cached <file>Staging e básicoUntrack a file without deleting it
ex. git rm --cached secret.txt
git mv <old> <new>Staging e básicoMove or rename a tracked file
ex. git mv old.js new.js
git branchRamificação e mesclagemList local branches
git branch -aRamificação e mesclagemList all branches (local + remote)
git branch <name>Ramificação e mesclagemCreate a new branch
ex. git branch feature/auth
git checkout <branch>Ramificação e mesclagemSwitch to an existing branch
ex. git checkout main
git checkout -b <branch>Ramificação e mesclagemCreate and switch to a new branch
ex. git checkout -b feature/auth
git switch <branch>Ramificação e mesclagemSwitch branches (modern syntax)
ex. git switch main
git switch -c <branch>Ramificação e mesclagemCreate and switch (modern syntax)
ex. git switch -c feature/auth
git branch -d <branch>Ramificação e mesclagemDelete a merged branch
ex. git branch -d feature/auth
git branch -D <branch>destrutivoRamificação e mesclagemForce delete a branch (unmerged too)
ex. git branch -D feature/old
git branch -m <old> <new>Ramificação e mesclagemRename a branch
ex. git branch -m old-name new-name
git merge <branch>Ramificação e mesclagemMerge a branch into current branch
ex. git merge feature/auth
git merge --no-ff <branch>Ramificação e mesclagemMerge with a merge commit (no fast-forward)
git merge --squash <branch>Ramificação e mesclagemSquash all commits from branch into one staged change
git merge --abortRamificação e mesclagemAbort an in-progress merge
git remote -vOperações remotasList remote connections with URLs
git remote add <name> <url>Operações remotasAdd a new remote
ex. git remote add origin https://github.com/user/repo.git
git remote remove <name>Operações remotasRemove a remote connection
ex. git remote remove origin
git remote rename <old> <new>Operações remotasRename a remote
ex. git remote rename origin upstream
git fetchOperações remotasDownload all changes from remote (no merge)
git fetch <remote>Operações remotasFetch from a specific remote
ex. git fetch origin
git pullOperações remotasFetch and merge changes from remote
git pull --rebaseOperações remotasFetch and rebase onto remote branch
git push <remote> <branch>Operações remotasPush branch to remote
ex. git push origin main
git push -u origin <branch>Operações remotasPush and set upstream tracking
ex. git push -u origin feature/auth
git push --force-with-leasedestrutivoOperações remotasSafe force push (fails if others pushed)
git push --tagsOperações remotasPush all tags to remote
git push origin --delete <branch>destrutivoOperações remotasDelete a remote branch
ex. git push origin --delete feature/old
git restore <file>destrutivoDesfazer alteraçõesDiscard changes in working directory
ex. git restore index.html
git restore --staged <file>Desfazer alteraçõesUnstage a file (keep changes)
ex. git restore --staged index.html
git reset HEAD <file>Desfazer alteraçõesUnstage a file (older syntax)
ex. git reset HEAD index.html
git reset --soft HEAD~1Desfazer alteraçõesUndo last commit, keep changes staged
git reset --mixed HEAD~1Desfazer alteraçõesUndo last commit, unstage changes (default)
git reset --hard HEAD~1destrutivoDesfazer alteraçõesUndo last commit, discard all changes
git reset --hard <commit>destrutivoDesfazer alteraçõesReset to a specific commit, discard everything after
git revert <commit>Desfazer alteraçõesCreate a new commit that undoes a specific commit
ex. git revert a1b2c3d
git revert HEADDesfazer alteraçõesRevert the most recent commit
git clean -fddestrutivoDesfazer alteraçõesRemove untracked files and directories
git clean -nDesfazer alteraçõesDry run: show what would be cleaned
git stashStashStash current working directory changes
git stash push -m "msg"StashStash with a descriptive message
ex. 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
ex. git stash apply stash@{0}
git stash drop stash@{n}StashDelete a specific stash
ex. git stash drop stash@{0}
git stash cleardestrutivoStashRemove all stashes
git stash branch <branch>StashCreate a branch from a stash
ex. git stash branch feature/from-stash
git rebase <branch>Rebase e avançadoRebase current branch onto another
ex. git rebase main
git rebase -i HEAD~nRebase e avançadoInteractive rebase for last n commits
ex. git rebase -i HEAD~3
git rebase --continueRebase e avançadoContinue rebase after resolving conflicts
git rebase --abortRebase e avançadoAbort an in-progress rebase
git cherry-pick <commit>Rebase e avançadoApply a specific commit to current branch
ex. git cherry-pick a1b2c3d
git cherry-pick <c1>..<c2>Rebase e avançadoApply a range of commits
ex. git cherry-pick a1b2c3d..f6e5d4c
git bisect startRebase e avançadoStart binary search for a bug
git bisect good <commit>Rebase e avançadoMark a commit as good in bisect
git bisect bad <commit>Rebase e avançadoMark a commit as bad in bisect
git bisect resetRebase e avançadoEnd bisect session
git worktree add <path> <branch>Rebase e avançadoCheck out a branch into a separate directory
git logInspeção e registoShow commit history
git log --onelineInspeção e registoCompact one-line log
git log --oneline --graph --allInspeção e registoVisual branch graph in terminal
git log -n <count>Inspeção e registoShow last N commits
ex. git log -n 5
git log --author="name"Inspeção e registoFilter commits by author
ex. git log --author="Jane Doe"
git log --since="2 weeks ago"Inspeção e registoShow commits since a date
git log -p <file>Inspeção e registoShow changes to a specific file over time
ex. git log -p README.md
git log --follow <file>Inspeção e registoFollow renames of a file in history
git show <commit>Inspeção e registoShow details of a specific commit
ex. git show a1b2c3d
git blame <file>Inspeção e registoShow who last modified each line
ex. git blame src/app.tsx
git shortlog -snInspeção e registoSummarize commits by author
git reflogInspeção e registoShow history of HEAD movements (recovery tool)
git grep "pattern"Inspeção e registoSearch working directory for a pattern
ex. git grep "TODO"
git tagTagsList all tags
git tag <name>TagsCreate a lightweight tag
ex. git tag v1.0.0
git tag -a <name> -m "msg"TagsCreate an annotated tag
ex. git tag -a v1.0.0 -m "Release v1.0.0"
git tag -a <name> <commit>TagsTag a past commit
ex. git tag -a v0.9.0 a1b2c3d
git show <tag>TagsShow tag metadata and commit
ex. git show v1.0.0
git push origin <tag>TagsPush a specific tag to remote
ex. git push origin v1.0.0
git push origin --tagsTagsPush all tags to remote
git tag -d <tag>TagsDelete a local tag
ex. git tag -d v1.0.0
git push origin --delete <tag>TagsDelete a remote tag
ex. git push origin --delete v1.0.0
git merge combina dois ramos criando um commit de merge, preservando o histórico completo de ambos os ramos. git rebase move commits de um ramo para outro, reescrevendo o histórico para criar um histórico de commits linear e mais limpo. Use merge para ramos públicos e trabalho partilhado; use rebase para limpar ramos de funcionalidades locais antes de fazer o merge.
Use git reset --soft HEAD~1 para desfazer o último commit mantendo as alterações em staging. Use git reset HEAD~1 (ou --mixed) para remover as alterações do staging mas mantê-las no diretório de trabalho. Use git reset --hard HEAD~1 para descartar permanentemente o commit e todas as alterações (operação destrutiva).
git fetch transfere as alterações do repositório remoto mas NÃO as integra no seu ramo atual. Atualiza os seus ramos de rastreamento remoto (origin/main). git pull é git fetch + git merge (ou git rebase com --rebase). Use fetch quando quiser inspecionar as alterações antes de as integrar.
Após um merge/rebase com conflitos, o Git marca os ficheiros conflituosos com os marcadores '<<<<<<', =======, >>>>>>. Abra cada ficheiro conflituoso, escolha manualmente quais as alterações a manter, remova os marcadores, depois adicione os ficheiros resolvidos com git add '<'ficheiro> e conclua com git merge --continue ou git commit.