Skip to content
xt-tools

Git cheatsheet

Searchable reference of everyday Git commands, from setup and branching to undoing mistakes.

187 of 187 commands

Git commands
SectionCommandDescription
Setupgit config --global user.name "<name>"Set the author name recorded in your commits.
Setupgit config --global user.email "<email>"Set the author email recorded in your commits.
Setupgit config --global init.defaultBranch mainName the first branch of new repositories main.
Setupgit config --global core.editor "vim"Editor used for commit messages and interactive rebase.
Setupgit config --global pull.rebase trueMake git pull rebase local commits instead of creating a merge.
Setupgit config --global push.autoSetupRemote trueThe first git push of a new branch sets its upstream automatically.
Setupgit config --global fetch.prune trueRemove deleted remote branches on every fetch.
Setupgit config --global rerere.enabled trueRecord conflict resolutions and replay them next time.
Setupgit config --global alias.st statusCreate an alias: git st now runs git status.
Setupgit config --list --show-originShow every setting and the file it comes from.
Setupgit config --global --editOpen the global config file (~/.gitconfig) in your editor.
Setupgit config user.email "<email>"Override the email for the current repository only.
Creategit initCreate an empty repository in the current directory.
Creategit init <directory>Create a new directory containing an empty repository.
Creategit init --bare <name>.gitCreate a repository without a working tree, for use as a shared remote.
Creategit clone <url>Copy a remote repository with its full history.
Creategit clone <url> <directory>Clone into a directory with a different name.
Creategit clone --branch <name> <url>Clone and check out a specific branch or tag.
Creategit clone --depth 1 <url>Shallow clone: only the latest commit, much faster for large repos.
Creategit clone --filter=blob:none <url>Partial clone: full history, file contents fetched on demand.
Creategit clone --recurse-submodules <url>Clone and initialise all submodules in one go.
Stage & commitgit statusShow staged, unstaged and untracked changes.
Stage & commitgit status -sbShort status with the branch and its ahead/behind count.
Stage & commitgit add <file>Stage a file for the next commit.
Stage & commitgit add .Stage all changes in the current directory and below.
Stage & commitgit add -AStage every change in the working tree, including deletions.
Stage & commitgit add -uStage modified and deleted tracked files, but not new files.
Stage & commitgit add -pChoose interactively which hunks to stage.
Stage & commitgit commit -m "<message>"Commit the staged changes with a message.
Stage & commitgit commit -am "<message>"Stage all modified tracked files and commit them.
Stage & commitgit commit --amendReplace the last commit with the staged changes and edit its message.
Stage & commitgit commit --amend --no-editAdd staged changes to the last commit, keeping its message.
Stage & commitgit commit --fixup <commit>Create a fixup! commit that rebase --autosquash folds into <commit>.
Stage & commitgit commit -S -m "<message>"Create a signed commit (GPG, SSH or X.509 key).
Stage & commitgit commit --allow-empty -m "<message>"Commit with no changes, e.g. to trigger CI.
Stage & commitgit rm <file>Delete a file and stage the removal.
Stage & commitgit rm --cached <file>Stop tracking a file but keep it on disk (then add it to .gitignore).
Stage & commitgit mv <old> <new>Rename or move a file and stage the change.
Branchgit branchList local branches; the current one is marked with *.
Branchgit branch -aList local and remote-tracking branches.
Branchgit branch -vvList branches with their upstream, ahead/behind count and last commit.
Branchgit branch --show-currentPrint the name of the current branch.
Branchgit branch <name>Create a branch at the current commit without switching to it.
Branchgit switch <branch>Switch to a branch. Creates a tracking branch if only origin/<branch> exists.
Branchgit switch -c <name>Create a branch and switch to it.
Branchgit switch -c <name> <start-point>Create a branch from a commit, tag or remote branch and switch to it.
Branchgit switch -Switch back to the previous branch.
Branchgit switch --detach <commit>Check out a commit without a branch (detached HEAD).
Branchgit switch --orphan <name>Start a new branch with no history and an empty working tree.
Branchgit checkout -b <name>Older equivalent of git switch -c.
Branchgit branch -m <new-name>Rename the current branch.
Branchgit branch -m <old> <new>Rename another branch.
Branchgit branch -d <name>Delete a branch that has been merged.
Branchgit branch -D <name>Force-delete a branch, even if unmerged.
Branchgit branch --mergedList branches already merged into the current one.
Branchgit branch --no-mergedList branches with commits not yet merged into the current one.
Branchgit branch -u origin/<branch>Set the upstream of the current branch.
Merge & rebasegit merge <branch>Merge a branch into the current one (fast-forwards when possible).
Merge & rebasegit merge --no-ff <branch>Always create a merge commit, even if a fast-forward is possible.
Merge & rebasegit merge --ff-only <branch>Merge only if it can fast-forward; otherwise fail.
Merge & rebasegit merge --squash <branch>Stage all changes from a branch as one change; you then commit it.
Merge & rebasegit merge --continueFinish a merge after resolving conflicts and staging the files.
Merge & rebasegit merge --abortStop a conflicted merge and return to the state before it.
Merge & rebasegit rebase <base>Replay the commits of the current branch on top of <base>.
Merge & rebasegit rebase -i HEAD~3Reorder, reword, squash, edit or drop the last 3 commits.
Merge & rebasegit rebase -i --autosquash <base>Interactive rebase that applies fixup! and squash! commits automatically.
Merge & rebasegit rebase --onto <new-base> <old-base> <branch>Move the commits after <old-base> onto <new-base>.
Merge & rebasegit rebase --update-refs <base>Rebase a stack of branches and move every branch in it along.
Merge & rebasegit rebase --continueContinue a rebase after resolving a conflict.
Merge & rebasegit rebase --skipSkip the commit that is causing the current conflict.
Merge & rebasegit rebase --abortCancel the rebase and restore the original branch.
Merge & rebasegit cherry-pick <commit>Apply the changes of a commit onto the current branch.
Merge & rebasegit cherry-pick <from>..<to>Apply a range of commits (excluding <from>).
Merge & rebasegit cherry-pick -x <commit>Cherry-pick and note the original commit hash in the message.
Merge & rebasegit cherry-pick --abortCancel a conflicted cherry-pick.
Merge & rebasegit restore --ours <file>During a conflict, keep our version of the file. In a rebase, "ours" is the branch being rebased onto.
Merge & rebasegit restore --theirs <file>During a conflict, take the incoming version of the file.
Merge & rebasegit mergetoolOpen the configured merge tool for each conflicted file.
Remotegit remote -vList remotes with their fetch and push URLs.
Remotegit remote add <name> <url>Add a remote, e.g. upstream for a fork.
Remotegit remote set-url origin <url>Change the URL of a remote.
Remotegit remote rename <old> <new>Rename a remote.
Remotegit remote remove <name>Remove a remote and its remote-tracking branches.
Remotegit ls-remote <remote>List the branches and tags on a remote without fetching.
Remotegit fetchDownload new commits and refs from the default remote without merging.
Remotegit fetch --all --pruneFetch every remote and drop remote branches that were deleted.
Remotegit pullFetch and integrate the upstream branch (merge or rebase, per pull.rebase).
Remotegit pull --rebaseFetch and rebase local commits on top of the upstream branch.
Remotegit pushUpload local commits of the current branch to its upstream.
Remotegit push -u origin <branch>Push a branch and set it as the upstream.
Remotegit push --force-with-leaseForce-push only if the remote branch is still where you last fetched it. Safer than --force.
Remotegit push origin --delete <branch>Delete a branch on the remote.
Remotegit push origin HEADPush the current branch to a branch of the same name.
Inspectgit logShow the commit history of the current branch.
Inspectgit log --oneline --graph --decorate --allCompact graph of every branch and tag.
Inspectgit log --statHistory with the files changed by each commit.
Inspectgit log -p <file>History of a file with the patch of each commit.
Inspectgit log --follow <file>History of a file, following renames.
Inspectgit log -S "<text>"Pickaxe: commits that added or removed <text>.
Inspectgit log -G "<regex>"Commits whose diff contains a line matching <regex>.
Inspectgit log --author="<name>"Commits by an author (matches name or email).
Inspectgit log --since="2 weeks ago"Commits newer than a date; pair with --until.
Inspectgit log main..<branch>Commits on <branch> that are not on main.
Inspectgit log --pretty=format:"%h %an %ar %s"Custom format: short hash, author, relative date, subject.
Inspectgit shortlog -snNumber of commits per author, most active first.
Inspectgit show <commit>Show a commit: message, author and diff.
Inspectgit show <commit>:<path>Print a file as it was in a commit.
Inspectgit diffChanges in the working tree that are not staged.
Inspectgit diff --stagedChanges that are staged for the next commit (same as --cached).
Inspectgit diff HEADAll uncommitted changes, staged or not.
Inspectgit diff main...<branch>Changes on <branch> since it diverged from main (three dots).
Inspectgit diff --statSummary of changed files and line counts.
Inspectgit diff --word-diffHighlight changed words instead of whole lines.
Inspectgit blame <file>Show who last changed each line, and in which commit.
Inspectgit blame -L 10,20 <file>Blame only lines 10 to 20.
Inspectgit grep "<text>"Search tracked files for text.
Inspectgit ls-filesList the files tracked in the index.
Inspectgit check-ignore -v <file>Show which .gitignore rule ignores a file.
Inspectgit rev-parse HEADPrint the full hash of the current commit.
Undogit restore <file>Discard unstaged changes to a file.
Undogit restore --staged <file>Unstage a file but keep its changes.
Undogit restore --source=<commit> <file>Restore a file as it was in <commit>.
Undogit reset --soft HEAD~1Undo the last commit, keeping its changes staged.
Undogit reset HEAD~1Undo the last commit, keeping its changes unstaged (mixed reset).
Undogit reset --hard HEAD~1Undo the last commit and discard its changes. Destructive.
Undogit reset --hard @{u}Make the branch match its upstream, discarding local commits and changes.
Undogit revert <commit>Create a new commit that undoes <commit>. Safe on shared branches.
Undogit revert -m 1 <merge-commit>Revert a merge commit, keeping the first parent as the mainline.
Undogit reflogEvery position HEAD has been at; use it to recover "lost" commits.
Undogit reset --hard HEAD@{1}Go back to where HEAD was before its last move, e.g. after a bad reset.
Undogit branch <name> <commit>Recover a deleted branch from a hash found in the reflog.
Undogit clean -nDry run: list untracked files that git clean would delete.
Undogit clean -fdDelete untracked files and directories.
Undogit clean -fdxDelete untracked and ignored files, e.g. build output.
Stashgit stashSave uncommitted changes to tracked files and clean the working tree.
Stashgit stash push -m "<message>"Stash with a description.
Stashgit stash -uStash untracked files as well.
Stashgit stash push <path>Stash only the changes to some paths.
Stashgit stash push --stagedStash only the staged changes.
Stashgit stash listList stashes, newest first (stash@{0}).
Stashgit stash show -p stash@{0}Show the diff stored in a stash.
Stashgit stash popApply the latest stash and remove it from the list.
Stashgit stash apply stash@{1}Apply a stash but keep it in the list.
Stashgit stash drop stash@{1}Delete one stash.
Stashgit stash clearDelete all stashes.
Stashgit stash branch <name>Create a branch from the commit the stash was made on and apply it there.
Tagsgit tagList tags.
Tagsgit tag -l "v1.*"List tags matching a pattern.
Tagsgit tag <name>Create a lightweight tag at the current commit.
Tagsgit tag -a v1.0.0 -m "<message>"Create an annotated tag with a message, author and date.
Tagsgit tag -a v1.0.0 <commit>Tag an older commit.
Tagsgit tag -s v1.0.0 -m "<message>"Create a signed annotated tag.
Tagsgit push origin v1.0.0Push one tag to the remote.
Tagsgit push --follow-tagsPush commits together with the annotated tags that point at them.
Tagsgit push --tagsPush all local tags.
Tagsgit tag -d v1.0.0Delete a local tag.
Tagsgit push origin --delete v1.0.0Delete a tag on the remote.
Tagsgit describe --tagsNearest tag, the number of commits since it and the short hash.
Submodules & worktreesgit submodule add <url> <path>Add a repository as a submodule at <path>.
Submodules & worktreesgit submodule update --init --recursiveFetch and check out all submodules, including nested ones.
Submodules & worktreesgit submodule update --remoteMove submodules to the latest commit of their tracked branch.
Submodules & worktreesgit submodule statusShow the commit each submodule is at.
Submodules & worktreesgit submodule foreach "<command>"Run a shell command in every submodule.
Submodules & worktreesgit rm <submodule-path>Remove a submodule from the working tree, the index and .gitmodules.
Submodules & worktreesgit worktree add ../<dir> <branch>Check out a branch in a second working directory.
Submodules & worktreesgit worktree add -b <new-branch> ../<dir>Create a branch and a worktree for it.
Submodules & worktreesgit worktree listList worktrees and the branch each has checked out.
Submodules & worktreesgit worktree remove <dir>Delete a worktree.
Submodules & worktreesgit worktree pruneClean up records of worktrees whose directory was deleted.
Advancedgit bisect startStart a binary search for the commit that introduced a bug.
Advancedgit bisect badMark the current commit as broken.
Advancedgit bisect good <commit>Mark a commit as working; Git checks out the midpoint.
Advancedgit bisect run <script>Automate bisect: exit code 0 means good, 1–127 (except 125) bad.
Advancedgit bisect resetEnd the bisect session and return to the original branch.
Advancedgit sparse-checkout set <dir> <dir>Only check out the listed directories (cone mode).
Advancedgit sparse-checkout disableCheck out the full tree again.
Advancedgit format-patch -1 <commit>Write a commit as an email-style .patch file.
Advancedgit am <file>.patchApply patches from format-patch as commits.
Advancedgit apply <file>.patchApply a diff to the working tree without committing.
Advancedgit archive --format=zip -o <file>.zip HEADExport the tracked files at a commit as an archive.
Advancedgit bundle create <file>.bundle --allPack the whole repository into one file for offline transfer.
Advancedgit range-diff <base> <old-tip> <new-tip>Compare two versions of a patch series, e.g. before and after a rebase.
Advancedgit notes add -m "<note>" <commit>Attach a note to a commit without changing its hash.
Advancedgit cat-file -p <object>Pretty-print a commit, tree, blob or tag object.
Advancedgit rev-list --count HEADCount the commits reachable from HEAD.
Advancedgit maintenance startSchedule background maintenance (prefetch, commit-graph, gc).
Advancedgit gcCompress history and remove unreachable objects.