Git Reference ยท Concepts
Git command cheat sheet by task
A one-page map of Git commands by task: start, snapshot, branch, inspect, undo, share and repair, with the reference page for each command.
Git has around 150 commands, but a working day uses fewer than twenty. This page maps what you want to do to the command that does it, and links each command to its reference page. It is a map, not a manual: the linked pages hold the options, edge cases and examples.
The commands fall into two layers. Porcelain commands (commit, switch, merge) are the friendly everyday interface. Plumbing commands (rev-parse, ls-tree, update-ref) are the low-level building blocks that scripts and the porcelain are made of.
git status # what changed? git add -p && git commit # record it git switch -c topic # branch off git pull --rebase && git push
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Start a repository
| Task | Command |
|---|---|
| New empty repository | git init |
| Copy a remote repository | git clone |
| Set name, email, defaults | git config |
| Ignore generated files | .gitignore |
| Ask Git for help | git help, git version |
Record changes
| Task | Command |
|---|---|
| See what changed | git status |
| Stage files or hunks | git add (-p for hunks) |
| Save a snapshot | git commit |
| Fix the last commit | git commit --amend |
| Move or delete a tracked file | git mv, git rm |
| Park unfinished work | git stash |
| Delete untracked files | git clean |
Branch and combine
| Task | Command |
|---|---|
| List, create, delete branches | git branch |
| Change branch | git switch, older git checkout |
| Combine two lines of work | git merge |
| Replay commits on a new base | git rebase |
| Copy one commit | git cherry-pick |
| Find the common ancestor | git merge-base |
| Work in two folders at once | git worktree |
| Name a release | git tag |
Inspect history
| Task | Command |
|---|---|
| Browse commits | git log |
| Show one commit | git show |
| Compare states | git diff |
| Find who changed a line | git blame |
| Find the commit that broke it | git bisect |
| Name a commit from a tag | git describe |
| See where HEAD has been | git reflog |
| Search commit messages or code | git log -S, git log --grep |
Undo
| Situation | Command |
|---|---|
| Unstage a file | git restore --staged <file> |
| Discard edits in a file | git restore <file> |
| Undo commits locally | git reset (see reset modes) |
| Undo a pushed commit safely | git revert |
| Recover "lost" commits | git reflog, then git branch at the hash |
Share
| Task | Command |
|---|---|
| Download new commits | git fetch |
| Fetch and integrate | git pull (see pull settings) |
| Upload commits | git push (see push settings) |
| Manage remotes | git remote |
| Nested repositories | git submodule, git subtree |
| Offline transfer | git bundle, git format-patch with git am |
| Export a snapshot | git archive |
Maintain and repair
| Task | Command |
|---|---|
| Check integrity | git fsck |
| Compress and clean up | git gc, git maintenance |
| Remove unreachable objects | git prune |
| Rewrite history in bulk | git filter-branch |
| Shallow work on huge repos | git sparse-checkout |
Plumbing worth knowing
git rev-parse turns names into hashes (page), git ls-tree lists a tree (page), git rev-list walks history for scripts (page), git update-ref moves a reference (page) and git symbolic-ref reads or sets HEAD (page).
Usage notes
- If a command has a
-hflag,git <command> -hprints a quick usage summary without opening a pager. - Commands that rewrite history (
reset --hard,rebase,commit --amend,filter-branch,push --force) are safe on private work and risky on shared branches. - Prefer
switchandrestoreover the overloadedcheckoutin new habits. - Many commands take the same building blocks: revision ranges, pathspecs and refspecs. Learn those once and every command gets easier.
- Aliases (git config) turn your favorite long lines into short names.
Related
- Common error messages, the three trees and reset modes
- Tutorials: Git Cheat Sheet, Git Recipes, Getting Help