Reference · 28 entries

Git Commands Reference

A Git cheat sheet: init, config, status, add, commit, log, diff, branches, merge, rebase, cherry-pick, undoing changes, stash, reflog, remotes, blame, bisect and worktrees, with real examples.

Git 2 Version history

Every example is a shell script that was run with Git 2.43 when this page was built, in its own fresh repository, so the output (commit hashes included) is real. For explanations and workflows, see the Git tutorial.

Basics

Create repositories and record changes: git init, config, status, add, commit, log, diff and show.

NameDescription
git initCreate a new Git repository in a directory, optionally naming the first branch, ready for the first commit.
git configRead and set Git settings per repository, per user (--global) or system-wide, such as your name, email and aliases.
git statusShow which files are staged, modified or untracked, and how the branch relates to its upstream.
git addStage changes for the next commit: whole files, all changes, or selected hunks with -p.
git commitRecord the staged changes as a new commit with a message; --amend rewrites the latest commit.
git logShow commit history: one line per commit, as a graph, filtered by author, date, path or message.
git diffShow changes: working tree versus staging area, staged changes (--staged), or between commits and branches.
git showShow a commit's message and changes, or the contents of a file at any commit with COMMIT:PATH.

Branching and Merging

Work on branches and combine them: git branch, switch, merge, rebase, cherry-pick and tag.

NameDescription
git branchList, create, rename and delete branches, and see which branches are merged.
git switchSwitch to another branch, or create one and switch to it with -c; the modern replacement for git checkout branches.
git mergeCombine another branch into the current one, fast-forwarding when possible or creating a merge commit.
git rebaseReplay your branch's commits on top of another branch for a linear history, or clean up commits with -i before sharing.
git cherry-pickApply the changes of specific commits from another branch onto the current branch as new commits.
git tagMark releases with lightweight or annotated tags, list them, and push them to a remote.

Undoing Changes

Discard, unstage, reset, revert, stash, recover with reflog, and clean untracked files.

NameDescription
git restoreDiscard working-tree changes to files, or unstage files, without touching other work.
git resetMove the current branch to another commit, keeping changes staged (--soft), unstaged (default) or discarding them (--hard).
git revertCreate a new commit that undoes an earlier commit's changes; the safe way to undo commits that were already pushed.
git stashSave uncommitted changes temporarily, switch tasks, then reapply them with pop or apply.
git reflogShow where HEAD and branches have pointed recently, so you can recover commits after a reset or bad rebase.
git cleanRemove untracked files from the working tree; always preview with -n first.

Remotes

Share work with other repositories: git clone, remote, push, fetch and pull.

NameDescription
git cloneCopy a repository with its full history and set up origin as the remote; --depth makes a shallow copy.
git remoteList, add, rename and remove the remote repositories a local repository talks to.
git pushUpload local commits and tags to a remote branch; -u sets the upstream, and --force-with-lease rewrites history safely.
git fetch and git pullDownload new commits from a remote (fetch), or download and integrate them into your branch (pull).

Investigating and Tools

Find who changed what and when, hunt bugs with bisect, use worktrees, and debug .gitignore rules.

NameDescription
git blameShow who last changed each line of a file and in which commit, to find the context of a line of code.
git bisectBinary-search the history for the commit that introduced a bug, manually or automatically with a test script.
git worktreeCheck out several branches at once in separate directories that share one repository, without stashing or cloning.
git check-ignoreFind out whether a path is ignored and which .gitignore rule ignores it; useful for debugging .gitignore patterns.

Was this page helpful?