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.
| Name | Description |
|---|---|
git init | Create a new Git repository in a directory, optionally naming the first branch, ready for the first commit. |
git config | Read and set Git settings per repository, per user (--global) or system-wide, such as your name, email and aliases. |
git status | Show which files are staged, modified or untracked, and how the branch relates to its upstream. |
git add | Stage changes for the next commit: whole files, all changes, or selected hunks with -p. |
git commit | Record the staged changes as a new commit with a message; --amend rewrites the latest commit. |
git log | Show commit history: one line per commit, as a graph, filtered by author, date, path or message. |
git diff | Show changes: working tree versus staging area, staged changes (--staged), or between commits and branches. |
git show | Show 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.
| Name | Description |
|---|---|
git branch | List, create, rename and delete branches, and see which branches are merged. |
git switch | Switch to another branch, or create one and switch to it with -c; the modern replacement for git checkout branches. |
git merge | Combine another branch into the current one, fast-forwarding when possible or creating a merge commit. |
git rebase | Replay your branch's commits on top of another branch for a linear history, or clean up commits with -i before sharing. |
git cherry-pick | Apply the changes of specific commits from another branch onto the current branch as new commits. |
git tag | Mark 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.
| Name | Description |
|---|---|
git restore | Discard working-tree changes to files, or unstage files, without touching other work. |
git reset | Move the current branch to another commit, keeping changes staged (--soft), unstaged (default) or discarding them (--hard). |
git revert | Create a new commit that undoes an earlier commit's changes; the safe way to undo commits that were already pushed. |
git stash | Save uncommitted changes temporarily, switch tasks, then reapply them with pop or apply. |
git reflog | Show where HEAD and branches have pointed recently, so you can recover commits after a reset or bad rebase. |
git clean | Remove untracked files from the working tree; always preview with -n first. |
Remotes
Share work with other repositories: git clone, remote, push, fetch and pull.
| Name | Description |
|---|---|
git clone | Copy a repository with its full history and set up origin as the remote; --depth makes a shallow copy. |
git remote | List, add, rename and remove the remote repositories a local repository talks to. |
git push | Upload local commits and tags to a remote branch; -u sets the upstream, and --force-with-lease rewrites history safely. |
git fetch and git pull | Download 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.
| Name | Description |
|---|---|
git blame | Show who last changed each line of a file and in which commit, to find the context of a line of code. |
git bisect | Binary-search the history for the commit that introduced a bug, manually or automatically with a test script. |
git worktree | Check out several branches at once in separate directories that share one repository, without stashing or cloning. |
git check-ignore | Find out whether a path is ignored and which .gitignore rule ignores it; useful for debugging .gitignore patterns. |