Hoja de comandos de Git
Git registra los cambios de tus archivos como una serie de commits en un repositorio. Estos comandos cubren el flujo diario: crear un repo, guardar trabajo, ramificar, fusionar y compartir con un remoto. Mantén esta hoja abierta mientras aprendes.
| Comando | Qué hace |
|---|---|
| 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
- La mayoría de los comandos aceptan opciones (como -m para mensaje o -a para todo); ejecuta 'git help <comando>' para la lista completa.
- Los nombres de rama son baratos y desechables: haz commits a menudo en una rama de función y luego fusiona a main.
- Antes de comandos destructivos como rebase o reset, asegúrate de que tu trabajo esté comprometido o guardado (stash).
Preguntas frecuentes
- ¿Cuál es la diferencia entre merge y rebase?
- Merge combina historiales con un nuevo commit que tiene dos padres. Rebase reproduce tus commits encima de otra rama, dando una historia lineal pero reescribiendo los IDs de commit.
- ¿Cómo deshago un commit?
- Usa 'git revert <commit>' para añadir un nuevo commit que deshace el cambio (seguro, historial compartido). 'git reset' reescribe la historia y no debe usarse en ramas compartidas.
- ¿Qué diferencia hay entre git pull y git fetch?
- Fetch descarga commits y referencias desde un remoto sin tocar tu árbol de trabajo. Pull hace fetch y luego fusiona o hace rebase de esos cambios en tu rama actual.
- ¿Cuándo debo usar git stash?
- Stash aparta temporalmente los cambios sin commitear para que puedas cambiar de rama o hacer pull limpio, y luego los restauras con 'git stash pop'.