Hoja de referencia de Git
Lista con búsqueda de los comandos Git más útiles para configuración, ramas, remotos, deshacer cambios e inspeccionar el historial.
Se ejecuta en tu navegador · no se envía nada
55 comandos
git initCreate a new, empty repository in the current folder.
git initCreate a new, empty repository in the current folder.Creates a hidden .git folder that stores the whole history. Run it once per project, then add files and make a first commit.
Ejemplo
git init -b main
Opciones
-b <name> | Name of the first branch, for example main. |
|---|---|
--bare | Repository without working files, used on servers as a central remote. |
git clone <url> [folder]Copy a remote repository, including its full history.
git clone <url> [folder]Copy a remote repository, including its full history.Downloads all commits and branches, checks out the default branch and sets the remote called origin.
Ejemplo
git clone https://github.com/user/repo.git my-folder
Opciones
--depth 1 | Shallow clone with only the latest commit. Much faster for big repositories and CI. |
|---|---|
-b <branch> | Check out this branch instead of the default one. |
--recurse-submodules | Also fetch submodules. |
git config --global user.name "Your Name"Set the name that appears on your commits.
git config --global user.name "Your Name"Set the name that appears on your commits.Stored in ~/.gitconfig. Leave out --global to set it only for the current repository, for example when you use a different identity for work.
Opciones
--global | For your whole user account. |
|---|---|
--local | Only this repository (default when --global is missing). |
--list --show-origin | Show all settings and which file they come from. |
git config --global user.email "[email protected]"Set the email address on your commits.
git config --global user.email "[email protected]"Set the email address on your commits.Use the same address as your Git hosting account (GitHub, GitLab) so commits are linked to you.
git config --global init.defaultBranch mainUse main as the default name for new repositories.
git config --global init.defaultBranch mainUse main as the default name for new repositories.Applies to new repositories created with git init.
git submodule update --init --recursiveFetch submodules after cloning a repository that uses them.
git submodule update --init --recursiveFetch submodules after cloning a repository that uses them.Submodules are other repositories pinned at a specific commit. A normal clone leaves their folders empty until you run this.
git statusShow which files are changed, staged or untracked.
git statusShow which files are changed, staged or untracked.Run it often. It also tells you which branch you are on and the next useful command.
Opciones
-s | Short output, one line per file. |
|---|---|
-b | Show branch and tracking info in short output. |
git add <file>Stage a file so it is included in the next commit.
git add <file>Stage a file so it is included in the next commit.Staging lets you build a commit from selected changes instead of everything at once.
Ejemplo
git add src/app.js README.md
Opciones
. | Stage everything in the current folder (new, changed and deleted). |
|---|---|
-A | Stage all changes in the whole repository. |
-u | Stage only files Git already tracks. |
-p | Pick changes interactively. |
git add -pStage changes interactively, piece by piece.
git add -pStage changes interactively, piece by piece.Git shows each changed hunk and asks what to do. Good for splitting unrelated changes into separate, clean commits.
Opciones
y / n | Stage or skip this hunk. |
|---|---|
s | Split the hunk into smaller pieces. |
e | Edit the hunk by hand. |
q | Quit. |
git commit -m "message"Save the staged changes as a commit.
git commit -m "message"Save the staged changes as a commit.A commit is a snapshot with a message. Write the message in the imperative: “Fix login timeout”. Without -m an editor opens.
Ejemplo
git commit -m "Add password strength checker"
Opciones
-a | Automatically stage all modified tracked files first (new files are not included). |
|---|---|
-v | Show the diff in the message editor. |
--no-verify | Skip pre-commit hooks. |
git commit --amendChange the most recent commit (message or content).
git commit --amendChange the most recent commit (message or content).Stage extra changes first to add them to the last commit, or run it alone to fix the message. It replaces the commit with a new one.
Ejemplo
git commit --amend --no-edit
Opciones
--no-edit | Keep the existing message. |
|---|---|
-m "new" | Replace the message. |
git diffShow unstaged changes in your working files.
git diffShow unstaged changes in your working files.Compares your working directory with the staging area. Nothing shown means everything changed is already staged.
Ejemplo
git diff main..feature -- src/
Opciones
--stat | Summary of changed files and line counts. |
|---|---|
--name-only | Only list file names. |
<a>..<b> | Compare two commits or branches. |
-- <path> | Limit to a file or folder. |
git diff --stagedShow what is staged and will go into the next commit.
git diff --stagedShow what is staged and will go into the next commit.Review this before committing. Also written as --cached.
git rm <file>Delete a file and stage the deletion.
git rm <file>Delete a file and stage the deletion.Opciones
--cached | Stop tracking the file but keep it on disk (useful after adding it to .gitignore). |
|---|---|
-r | Remove a folder recursively. |
git mv <old> <new>Rename or move a file and stage the change.
git mv <old> <new>Rename or move a file and stage the change.Equivalent to moving the file yourself and running git add. Git detects renames automatically.
git log --oneline --graph --decorateCompact history with a branch graph.
git log --oneline --graph --decorateCompact history with a branch graph.One line per commit, with branch and tag names. The best quick overview of how branches relate.
Ejemplo
git log --oneline --graph --all -n 20
Opciones
--all | Include all branches, not just the current one. |
|---|---|
-n <num> | Limit the number of commits. |
--author="name" | Only commits by this author. |
--since="2 weeks ago" | Only recent commits. |
git log -p <file>History of one file including the changes.
git log -p <file>History of one file including the changes.Opciones
--follow | Keep following the file across renames. |
|---|---|
--stat | Show changed-line counts instead of full diffs. |
git show <commit>Show one commit with its message and changes.
git show <commit>Show one commit with its message and changes.HEAD is the current commit. HEAD~1 is its parent, HEAD~2 the commit before that.
Ejemplo
git show HEAD~2
git blame <file>Show who last changed each line and in which commit.
git blame <file>Show who last changed each line and in which commit.Ejemplo
git blame -L 10,25 src/app.js
Opciones
-L <start>,<end> | Only these lines. |
|---|---|
-w | Ignore whitespace changes. |
git reflogHistory of where HEAD has been. Recovers “lost” commits.
git reflogHistory of where HEAD has been. Recovers “lost” commits.After a bad reset or rebase, find the old commit here and return to it with git reset --hard <hash> or create a branch from it. Entries expire after about 90 days.
Ejemplo
git branch rescue a1b2c3d
git grep "text"Search tracked files for text.
git grep "text"Search tracked files for text.Faster than grep -r because it only looks at files Git tracks and ignores build output.
Opciones
-n | Show line numbers. |
|---|---|
-i | Ignore case. |
-w | Match whole words. |
git shortlog -snCommits per author, most active first.
git shortlog -snCommits per author, most active first.A quick way to see who contributed how much. -s prints counts instead of commit messages and -n sorts by count.
Opciones
-s | Summary: only counts. |
|---|---|
-n | Sort by number of commits. |
-e | Include email addresses. |
git bisect startBinary search for the commit that introduced a bug.
git bisect startBinary search for the commit that introduced a bug.Mark a bad commit and a good one. Git checks out the middle; you test and answer good or bad until it finds the culprit.
Ejemplo
git bisect start git bisect bad git bisect good v1.2.0 # test, then: git bisect good|bad git bisect reset
Opciones
bisect reset | Finish and return to your original branch. |
|---|---|
bisect run <script> | Automate with a script that exits 0 for good and non-zero for bad. |
git branchList local branches. The current one is marked with *.
git branchList local branches. The current one is marked with *.Opciones
-a | Include remote-tracking branches. |
|---|---|
-vv | Show the last commit and upstream of each branch. |
--merged | Only branches already merged into the current one. |
git branch <name>Create a branch at the current commit without switching to it.
git branch <name>Create a branch at the current commit without switching to it.The new branch starts where you are now. Move to it with git switch <name>, or create and switch in one step with git switch -c <name>.
git switch <branch>Switch to another branch.
git switch <branch>Switch to another branch.Updates your working files to that branch. Uncommitted changes that conflict will block the switch; commit or stash them first. Older guides use git checkout for this.
git switch -c <branch>Create a new branch and switch to it.
git switch -c <branch>Create a new branch and switch to it.Ejemplo
git switch -c feature/login
git branch -d <branch>Delete a branch that is already merged.
git branch -d <branch>Delete a branch that is already merged.Opciones
-D | Force delete even if it is not merged. |
|---|
git branch -m <new-name>Rename the current branch.
git branch -m <new-name>Rename the current branch.If the branch was already pushed, push the new name and delete the old one on the remote.
git merge <branch>Merge another branch into the one you are on.
git merge <branch>Merge another branch into the one you are on.Creates a merge commit when histories have diverged. On conflicts, edit the marked files, git add them and run git commit.
Opciones
--no-ff | Always create a merge commit, even when a fast-forward is possible. |
|---|---|
--squash | Combine all changes into one staged change without a merge commit. |
--abort | Cancel a merge that hit conflicts. |
git rebase <branch>Replay your commits on top of another branch for a linear history.
git rebase <branch>Replay your commits on top of another branch for a linear history.Typical use: git rebase main on your feature branch to pick up the latest changes. On conflicts fix the files, git add them and run git rebase --continue.
Opciones
--continue | Continue after resolving conflicts. |
|---|---|
--abort | Cancel and return to how things were. |
-i | Interactive: reorder, squash or edit commits. |
git rebase -i HEAD~3Clean up the last 3 commits: squash, reword, drop or reorder.
git rebase -i HEAD~3Clean up the last 3 commits: squash, reword, drop or reorder.Opens an editor with one line per commit. Change “pick” to squash (s), reword (r), edit (e) or drop (d), save and close.
git cherry-pick <commit>Copy a single commit onto the current branch.
git cherry-pick <commit>Copy a single commit onto the current branch.Useful for applying a hotfix to a release branch without merging everything else.
Opciones
-n | Apply the changes but do not commit. |
|---|---|
-x | Add a line with the original commit hash to the message. |
--abort | Cancel after conflicts. |
git worktree add ../hotfix hotfixCheck out another branch in a separate folder at the same time.
git worktree add ../hotfix hotfixCheck out another branch in a separate folder at the same time.Lets you work on a hotfix without stashing or leaving your current branch. Remove it later with git worktree remove ../hotfix.
git remote -vList the remotes and their URLs.
git remote -vList the remotes and their URLs.Normally shows origin with a fetch and a push URL. Change a URL later with git remote set-url origin <url>.
git remote add origin <url>Connect your local repository to a remote.
git remote add origin <url>Connect your local repository to a remote.Typical after git init: add origin, then git push -u origin main.
git fetchDownload remote changes without changing your files.
git fetchDownload remote changes without changing your files.Safe to run any time. Afterwards compare with git log HEAD..origin/main or merge when ready.
Opciones
--prune | Remove local references to branches deleted on the remote. |
|---|---|
--all | Fetch from all remotes. |
git pullFetch remote changes and merge them into your branch.
git pullFetch remote changes and merge them into your branch.Opciones
--rebase | Rebase your commits on top instead of creating a merge commit. |
|---|---|
--ff-only | Refuse to pull if it would need a merge. |
git pushUpload your commits to the remote branch.
git pushUpload your commits to the remote branch.Rejected? Someone pushed first. Pull (or fetch and rebase), then push again.
git push -u origin <branch>Push a new branch and remember it as the upstream.
git push -u origin <branch>Push a new branch and remember it as the upstream.After this, a plain git push and git pull work for that branch.
git push --force-with-leaseForce push, but only if nobody else pushed in the meantime.
git push --force-with-leaseForce push, but only if nobody else pushed in the meantime.Needed after rebasing or amending a commit that was already pushed to your own branch.
git push origin --delete <branch>Delete a branch on the remote.
git push origin --delete <branch>Delete a branch on the remote.Only removes the branch on the server. Delete your local copy with git branch -d <branch>. Teammates clean up stale references with git fetch --prune.
git restore <file>Discard unstaged changes in a file and return it to the last commit.
git restore <file>Discard unstaged changes in a file and return it to the last commit.git restore --staged <file>Unstage a file but keep your edits.
git restore --staged <file>Unstage a file but keep your edits.Moves the file from the staging area back to your working files. Nothing is lost. It replaces the older git reset HEAD <file>.
git restore --source=<commit> <file>Bring back a file as it was in an older commit.
git restore --source=<commit> <file>Bring back a file as it was in an older commit.Ejemplo
git restore --source=HEAD~3 src/app.js
git reset --soft HEAD~1Undo the last commit but keep the changes staged.
git reset --soft HEAD~1Undo the last commit but keep the changes staged.Handy when you committed too early or want to redo the message or split the commit.
git reset HEAD~1Undo the last commit and unstage the changes, keeping the files.
git reset HEAD~1Undo the last commit and unstage the changes, keeping the files.This is the default “mixed” reset. Your edits stay in the working directory.
git reset --hard HEAD~1Undo the last commit and delete its changes.
git reset --hard HEAD~1Undo the last commit and delete its changes.git revert <commit>Create a new commit that undoes an earlier commit.
git revert <commit>Create a new commit that undoes an earlier commit.Safe for shared branches because history is not rewritten. For a merge commit add -m 1 to pick the mainline parent.
Opciones
-n | Apply the undo without committing. |
|---|---|
-m 1 | Required when reverting a merge commit. |
git clean -fdDelete untracked files and folders.
git clean -fdDelete untracked files and folders.Always preview first with git clean -nd.
Opciones
-n | Dry run: only show what would be removed. |
|---|---|
-d | Include folders. |
-x | Also remove ignored files. |
-f | Required to actually delete. |
git stashShelve your uncommitted changes so you can switch tasks.
git stashShelve your uncommitted changes so you can switch tasks.Ejemplo
git stash push -u -m "wip login form"
Opciones
-u | Include untracked files. |
|---|---|
-m "msg" | Label the stash so you recognise it later. |
git stash popRe-apply the latest stash and remove it from the list.
git stash popRe-apply the latest stash and remove it from the list.Use git stash apply to keep the stash after applying it.
git stash listList your stashes, newest first.
git stash listList your stashes, newest first.Each entry has an index such as stash@{0}, the branch and your label. Inspect one with git stash show -p stash@{0}, apply a specific one with git stash apply stash@{1}.
git tag -a v1.0.0 -m "Release 1.0.0"Mark a commit as a release with an annotated tag.
git tag -a v1.0.0 -m "Release 1.0.0"Mark a commit as a release with an annotated tag.Annotated tags store author, date and message. A lightweight tag (git tag v1.0.0) is only a pointer.
git push --tagsUpload all local tags to the remote.
git push --tagsUpload all local tags to the remote.Tags are not pushed by a normal git push.
Las descripciones de esta referencia están por ahora en inglés.
Explicación
Busca o filtra por grupo y copia un comando con un clic. Abre un comando para ver qué hace, un ejemplo, sus opciones principales y avisos donde pueda causar daño.
🔒 Tu entrada se queda en tu navegador y no se envía a ningún sitio.
Preguntas frecuentes
¿Qué diferencia hay entre reset y revert?
Reset mueve el puntero de la rama y reescribe el historial. Revert añade un commit nuevo que deshace un cambio y es seguro en ramas compartidas.
Qué hacer después
¿Has encontrado un problema o tienes una sugerencia? Cuéntanoslo