Git cheatsheet
Searchable reference of everyday Git commands, from setup and branching to undoing mistakes.
187 of 187 commands
| Section | Command | Description |
|---|---|---|
| Setup | git config --global user.name "<name>" | Set the author name recorded in your commits. |
| Setup | git config --global user.email "<email>" | Set the author email recorded in your commits. |
| Setup | git config --global init.defaultBranch main | Name the first branch of new repositories main. |
| Setup | git config --global core.editor "vim" | Editor used for commit messages and interactive rebase. |
| Setup | git config --global pull.rebase true | Make git pull rebase local commits instead of creating a merge. |
| Setup | git config --global push.autoSetupRemote true | The first git push of a new branch sets its upstream automatically. |
| Setup | git config --global fetch.prune true | Remove deleted remote branches on every fetch. |
| Setup | git config --global rerere.enabled true | Record conflict resolutions and replay them next time. |
| Setup | git config --global alias.st status | Create an alias: git st now runs git status. |
| Setup | git config --list --show-origin | Show every setting and the file it comes from. |
| Setup | git config --global --edit | Open the global config file (~/.gitconfig) in your editor. |
| Setup | git config user.email "<email>" | Override the email for the current repository only. |
| Create | git init | Create an empty repository in the current directory. |
| Create | git init <directory> | Create a new directory containing an empty repository. |
| Create | git init --bare <name>.git | Create a repository without a working tree, for use as a shared remote. |
| Create | git clone <url> | Copy a remote repository with its full history. |
| Create | git clone <url> <directory> | Clone into a directory with a different name. |
| Create | git clone --branch <name> <url> | Clone and check out a specific branch or tag. |
| Create | git clone --depth 1 <url> | Shallow clone: only the latest commit, much faster for large repos. |
| Create | git clone --filter=blob:none <url> | Partial clone: full history, file contents fetched on demand. |
| Create | git clone --recurse-submodules <url> | Clone and initialise all submodules in one go. |
| Stage & commit | git status | Show staged, unstaged and untracked changes. |
| Stage & commit | git status -sb | Short status with the branch and its ahead/behind count. |
| Stage & commit | git add <file> | Stage a file for the next commit. |
| Stage & commit | git add . | Stage all changes in the current directory and below. |
| Stage & commit | git add -A | Stage every change in the working tree, including deletions. |
| Stage & commit | git add -u | Stage modified and deleted tracked files, but not new files. |
| Stage & commit | git add -p | Choose interactively which hunks to stage. |
| Stage & commit | git commit -m "<message>" | Commit the staged changes with a message. |
| Stage & commit | git commit -am "<message>" | Stage all modified tracked files and commit them. |
| Stage & commit | git commit --amend | Replace the last commit with the staged changes and edit its message. |
| Stage & commit | git commit --amend --no-edit | Add staged changes to the last commit, keeping its message. |
| Stage & commit | git commit --fixup <commit> | Create a fixup! commit that rebase --autosquash folds into <commit>. |
| Stage & commit | git commit -S -m "<message>" | Create a signed commit (GPG, SSH or X.509 key). |
| Stage & commit | git commit --allow-empty -m "<message>" | Commit with no changes, e.g. to trigger CI. |
| Stage & commit | git rm <file> | Delete a file and stage the removal. |
| Stage & commit | git rm --cached <file> | Stop tracking a file but keep it on disk (then add it to .gitignore). |
| Stage & commit | git mv <old> <new> | Rename or move a file and stage the change. |
| Branch | git branch | List local branches; the current one is marked with *. |
| Branch | git branch -a | List local and remote-tracking branches. |
| Branch | git branch -vv | List branches with their upstream, ahead/behind count and last commit. |
| Branch | git branch --show-current | Print the name of the current branch. |
| Branch | git branch <name> | Create a branch at the current commit without switching to it. |
| Branch | git switch <branch> | Switch to a branch. Creates a tracking branch if only origin/<branch> exists. |
| Branch | git switch -c <name> | Create a branch and switch to it. |
| Branch | git switch -c <name> <start-point> | Create a branch from a commit, tag or remote branch and switch to it. |
| Branch | git switch - | Switch back to the previous branch. |
| Branch | git switch --detach <commit> | Check out a commit without a branch (detached HEAD). |
| Branch | git switch --orphan <name> | Start a new branch with no history and an empty working tree. |
| Branch | git checkout -b <name> | Older equivalent of git switch -c. |
| Branch | git branch -m <new-name> | Rename the current branch. |
| Branch | git branch -m <old> <new> | Rename another branch. |
| Branch | git branch -d <name> | Delete a branch that has been merged. |
| Branch | git branch -D <name> | Force-delete a branch, even if unmerged. |
| Branch | git branch --merged | List branches already merged into the current one. |
| Branch | git branch --no-merged | List branches with commits not yet merged into the current one. |
| Branch | git branch -u origin/<branch> | Set the upstream of the current branch. |
| Merge & rebase | git merge <branch> | Merge a branch into the current one (fast-forwards when possible). |
| Merge & rebase | git merge --no-ff <branch> | Always create a merge commit, even if a fast-forward is possible. |
| Merge & rebase | git merge --ff-only <branch> | Merge only if it can fast-forward; otherwise fail. |
| Merge & rebase | git merge --squash <branch> | Stage all changes from a branch as one change; you then commit it. |
| Merge & rebase | git merge --continue | Finish a merge after resolving conflicts and staging the files. |
| Merge & rebase | git merge --abort | Stop a conflicted merge and return to the state before it. |
| Merge & rebase | git rebase <base> | Replay the commits of the current branch on top of <base>. |
| Merge & rebase | git rebase -i HEAD~3 | Reorder, reword, squash, edit or drop the last 3 commits. |
| Merge & rebase | git rebase -i --autosquash <base> | Interactive rebase that applies fixup! and squash! commits automatically. |
| Merge & rebase | git rebase --onto <new-base> <old-base> <branch> | Move the commits after <old-base> onto <new-base>. |
| Merge & rebase | git rebase --update-refs <base> | Rebase a stack of branches and move every branch in it along. |
| Merge & rebase | git rebase --continue | Continue a rebase after resolving a conflict. |
| Merge & rebase | git rebase --skip | Skip the commit that is causing the current conflict. |
| Merge & rebase | git rebase --abort | Cancel the rebase and restore the original branch. |
| Merge & rebase | git cherry-pick <commit> | Apply the changes of a commit onto the current branch. |
| Merge & rebase | git cherry-pick <from>..<to> | Apply a range of commits (excluding <from>). |
| Merge & rebase | git cherry-pick -x <commit> | Cherry-pick and note the original commit hash in the message. |
| Merge & rebase | git cherry-pick --abort | Cancel a conflicted cherry-pick. |
| Merge & rebase | git restore --ours <file> | During a conflict, keep our version of the file. In a rebase, "ours" is the branch being rebased onto. |
| Merge & rebase | git restore --theirs <file> | During a conflict, take the incoming version of the file. |
| Merge & rebase | git mergetool | Open the configured merge tool for each conflicted file. |
| Remote | git remote -v | List remotes with their fetch and push URLs. |
| Remote | git remote add <name> <url> | Add a remote, e.g. upstream for a fork. |
| Remote | git remote set-url origin <url> | Change the URL of a remote. |
| Remote | git remote rename <old> <new> | Rename a remote. |
| Remote | git remote remove <name> | Remove a remote and its remote-tracking branches. |
| Remote | git ls-remote <remote> | List the branches and tags on a remote without fetching. |
| Remote | git fetch | Download new commits and refs from the default remote without merging. |
| Remote | git fetch --all --prune | Fetch every remote and drop remote branches that were deleted. |
| Remote | git pull | Fetch and integrate the upstream branch (merge or rebase, per pull.rebase). |
| Remote | git pull --rebase | Fetch and rebase local commits on top of the upstream branch. |
| Remote | git push | Upload local commits of the current branch to its upstream. |
| Remote | git push -u origin <branch> | Push a branch and set it as the upstream. |
| Remote | git push --force-with-lease | Force-push only if the remote branch is still where you last fetched it. Safer than --force. |
| Remote | git push origin --delete <branch> | Delete a branch on the remote. |
| Remote | git push origin HEAD | Push the current branch to a branch of the same name. |
| Inspect | git log | Show the commit history of the current branch. |
| Inspect | git log --oneline --graph --decorate --all | Compact graph of every branch and tag. |
| Inspect | git log --stat | History with the files changed by each commit. |
| Inspect | git log -p <file> | History of a file with the patch of each commit. |
| Inspect | git log --follow <file> | History of a file, following renames. |
| Inspect | git log -S "<text>" | Pickaxe: commits that added or removed <text>. |
| Inspect | git log -G "<regex>" | Commits whose diff contains a line matching <regex>. |
| Inspect | git log --author="<name>" | Commits by an author (matches name or email). |
| Inspect | git log --since="2 weeks ago" | Commits newer than a date; pair with --until. |
| Inspect | git log main..<branch> | Commits on <branch> that are not on main. |
| Inspect | git log --pretty=format:"%h %an %ar %s" | Custom format: short hash, author, relative date, subject. |
| Inspect | git shortlog -sn | Number of commits per author, most active first. |
| Inspect | git show <commit> | Show a commit: message, author and diff. |
| Inspect | git show <commit>:<path> | Print a file as it was in a commit. |
| Inspect | git diff | Changes in the working tree that are not staged. |
| Inspect | git diff --staged | Changes that are staged for the next commit (same as --cached). |
| Inspect | git diff HEAD | All uncommitted changes, staged or not. |
| Inspect | git diff main...<branch> | Changes on <branch> since it diverged from main (three dots). |
| Inspect | git diff --stat | Summary of changed files and line counts. |
| Inspect | git diff --word-diff | Highlight changed words instead of whole lines. |
| Inspect | git blame <file> | Show who last changed each line, and in which commit. |
| Inspect | git blame -L 10,20 <file> | Blame only lines 10 to 20. |
| Inspect | git grep "<text>" | Search tracked files for text. |
| Inspect | git ls-files | List the files tracked in the index. |
| Inspect | git check-ignore -v <file> | Show which .gitignore rule ignores a file. |
| Inspect | git rev-parse HEAD | Print the full hash of the current commit. |
| Undo | git restore <file> | Discard unstaged changes to a file. |
| Undo | git restore --staged <file> | Unstage a file but keep its changes. |
| Undo | git restore --source=<commit> <file> | Restore a file as it was in <commit>. |
| Undo | git reset --soft HEAD~1 | Undo the last commit, keeping its changes staged. |
| Undo | git reset HEAD~1 | Undo the last commit, keeping its changes unstaged (mixed reset). |
| Undo | git reset --hard HEAD~1 | Undo the last commit and discard its changes. Destructive. |
| Undo | git reset --hard @{u} | Make the branch match its upstream, discarding local commits and changes. |
| Undo | git revert <commit> | Create a new commit that undoes <commit>. Safe on shared branches. |
| Undo | git revert -m 1 <merge-commit> | Revert a merge commit, keeping the first parent as the mainline. |
| Undo | git reflog | Every position HEAD has been at; use it to recover "lost" commits. |
| Undo | git reset --hard HEAD@{1} | Go back to where HEAD was before its last move, e.g. after a bad reset. |
| Undo | git branch <name> <commit> | Recover a deleted branch from a hash found in the reflog. |
| Undo | git clean -n | Dry run: list untracked files that git clean would delete. |
| Undo | git clean -fd | Delete untracked files and directories. |
| Undo | git clean -fdx | Delete untracked and ignored files, e.g. build output. |
| Stash | git stash | Save uncommitted changes to tracked files and clean the working tree. |
| Stash | git stash push -m "<message>" | Stash with a description. |
| Stash | git stash -u | Stash untracked files as well. |
| Stash | git stash push <path> | Stash only the changes to some paths. |
| Stash | git stash push --staged | Stash only the staged changes. |
| Stash | git stash list | List stashes, newest first (stash@{0}). |
| Stash | git stash show -p stash@{0} | Show the diff stored in a stash. |
| Stash | git stash pop | Apply the latest stash and remove it from the list. |
| Stash | git stash apply stash@{1} | Apply a stash but keep it in the list. |
| Stash | git stash drop stash@{1} | Delete one stash. |
| Stash | git stash clear | Delete all stashes. |
| Stash | git stash branch <name> | Create a branch from the commit the stash was made on and apply it there. |
| Tags | git tag | List tags. |
| Tags | git tag -l "v1.*" | List tags matching a pattern. |
| Tags | git tag <name> | Create a lightweight tag at the current commit. |
| Tags | git tag -a v1.0.0 -m "<message>" | Create an annotated tag with a message, author and date. |
| Tags | git tag -a v1.0.0 <commit> | Tag an older commit. |
| Tags | git tag -s v1.0.0 -m "<message>" | Create a signed annotated tag. |
| Tags | git push origin v1.0.0 | Push one tag to the remote. |
| Tags | git push --follow-tags | Push commits together with the annotated tags that point at them. |
| Tags | git push --tags | Push all local tags. |
| Tags | git tag -d v1.0.0 | Delete a local tag. |
| Tags | git push origin --delete v1.0.0 | Delete a tag on the remote. |
| Tags | git describe --tags | Nearest tag, the number of commits since it and the short hash. |
| Submodules & worktrees | git submodule add <url> <path> | Add a repository as a submodule at <path>. |
| Submodules & worktrees | git submodule update --init --recursive | Fetch and check out all submodules, including nested ones. |
| Submodules & worktrees | git submodule update --remote | Move submodules to the latest commit of their tracked branch. |
| Submodules & worktrees | git submodule status | Show the commit each submodule is at. |
| Submodules & worktrees | git submodule foreach "<command>" | Run a shell command in every submodule. |
| Submodules & worktrees | git rm <submodule-path> | Remove a submodule from the working tree, the index and .gitmodules. |
| Submodules & worktrees | git worktree add ../<dir> <branch> | Check out a branch in a second working directory. |
| Submodules & worktrees | git worktree add -b <new-branch> ../<dir> | Create a branch and a worktree for it. |
| Submodules & worktrees | git worktree list | List worktrees and the branch each has checked out. |
| Submodules & worktrees | git worktree remove <dir> | Delete a worktree. |
| Submodules & worktrees | git worktree prune | Clean up records of worktrees whose directory was deleted. |
| Advanced | git bisect start | Start a binary search for the commit that introduced a bug. |
| Advanced | git bisect bad | Mark the current commit as broken. |
| Advanced | git bisect good <commit> | Mark a commit as working; Git checks out the midpoint. |
| Advanced | git bisect run <script> | Automate bisect: exit code 0 means good, 1–127 (except 125) bad. |
| Advanced | git bisect reset | End the bisect session and return to the original branch. |
| Advanced | git sparse-checkout set <dir> <dir> | Only check out the listed directories (cone mode). |
| Advanced | git sparse-checkout disable | Check out the full tree again. |
| Advanced | git format-patch -1 <commit> | Write a commit as an email-style .patch file. |
| Advanced | git am <file>.patch | Apply patches from format-patch as commits. |
| Advanced | git apply <file>.patch | Apply a diff to the working tree without committing. |
| Advanced | git archive --format=zip -o <file>.zip HEAD | Export the tracked files at a commit as an archive. |
| Advanced | git bundle create <file>.bundle --all | Pack the whole repository into one file for offline transfer. |
| Advanced | git range-diff <base> <old-tip> <new-tip> | Compare two versions of a patch series, e.g. before and after a rebase. |
| Advanced | git notes add -m "<note>" <commit> | Attach a note to a commit without changing its hash. |
| Advanced | git cat-file -p <object> | Pretty-print a commit, tree, blob or tag object. |
| Advanced | git rev-list --count HEAD | Count the commits reachable from HEAD. |
| Advanced | git maintenance start | Schedule background maintenance (prefetch, commit-graph, gc). |
| Advanced | git gc | Compress history and remove unreachable objects. |