Git Cheat Sheet
The Git commands you use every day, with what they do.
About Git cheat sheet
Git records snapshots of a project as commits. Changes move through three places: the working tree (your files), the staging area (what the next commit will contain, filled with git add) and the repository (the commits). Branches are movable names for commits, and remotes such as origin are other copies of the repository that you fetch from and push to.
The commands below cover everyday work. Newer Git versions split the old all-purpose checkout command into git switch (change branches) and git restore (discard or unstage changes), which are harder to misuse. Commands marked as destructive throw work away; when in doubt, git reflog lists recent positions of HEAD, so commits that seem lost can usually be recovered.
49 entries
Set up
| Command | What it does | Note |
|---|---|---|
| git config --global user.name "Ada Lovelace" | Set the name on your commits | |
| git config --global user.email ada@example.com | Set the email on your commits | |
| git config --global init.defaultBranch main | Name the first branch of new repositories main | |
| git config --list | Show your settings |
Start a repository
| Command | What it does | Note |
|---|---|---|
| git init | Turn the current folder into a repository | |
| git clone <url> | Copy a remote repository | git clone git@github.com:org/repo.git |
| git clone --depth 1 <url> | Clone only the latest commit (shallow) |
Stage and commit
| Command | What it does | Note |
|---|---|---|
| git status | Show changed, staged and untracked files | |
| git add <file> | Stage a file | git add src/app.ts |
| git add -p | Stage changes hunk by hunk | |
| git add -A | Stage everything, including deletions | |
| git commit -m "message" | Commit staged changes | |
| git commit --amend | Change the last commit (message or content) | Only before you've pushed it |
| git rm <file> | Delete a file and stage the deletion | |
| git mv <old> <new> | Rename a file and stage the rename |
Branches
| Command | What it does | Note |
|---|---|---|
| git branch | List local branches | -a includes remote branches |
| git switch -c <branch> | Create a branch and switch to it | Older: git checkout -b <branch> |
| git switch <branch> | Switch to a branch | |
| git merge <branch> | Merge a branch into the current one | |
| git rebase <branch> | Replay your commits on top of another branch | Don't rebase commits others already have |
| git branch -d <branch> | Delete a merged branch | -D forces deletion |
| git cherry-pick <commit> | Apply one commit onto the current branch |
Remotes
| Command | What it does | Note |
|---|---|---|
| git remote -v | List remotes and their URLs | |
| git fetch | Download new commits without changing your files | |
| git pull | Fetch and merge (or rebase) the upstream branch | git pull --rebase keeps history linear |
| git push | Upload commits to the upstream branch | |
| git push -u origin <branch> | Push a new branch and set its upstream | |
| git push --force-with-lease | Force-push, but only if nobody else pushed meanwhile | Safer than --force |
Inspect history
| Command | What it does | Note |
|---|---|---|
| git log --oneline --graph | Compact history with branches drawn | |
| git log -p <file> | History of one file with its changes | |
| git diff | Unstaged changes | |
| git diff --staged | Staged changes | |
| git diff main...<branch> | What a branch changed since it left main | |
| git show <commit> | One commit and its changes | |
| git blame <file> | Who last changed each line | |
| git bisect start | Binary-search history for the commit that broke something |
Undo
| Command | What it does | Note |
|---|---|---|
| git restore <file> | Discard unstaged changes to a file | Cannot be undone |
| git restore --staged <file> | Unstage a file, keeping the changes | |
| git revert <commit> | Make a new commit that undoes a commit | Safe on shared branches |
| git reset --soft HEAD~1 | Undo the last commit, keep its changes staged | |
| git reset --hard HEAD~1 | Throw away the last commit and its changes | Destructive |
| git reflog | Every position HEAD has been at: find lost commits | |
| git clean -n | Preview untracked files that git clean -f would delete |
Stash and tags
| Command | What it does | Note |
|---|---|---|
| git stash | Put uncommitted changes aside | -u includes untracked files |
| git stash pop | Reapply the latest stash and drop it | |
| git stash list | List stashes | |
| git tag v1.2.0 | Tag the current commit | |
| git tag -a v1.2.0 -m "Release" | Create an annotated tag | |
| git push --tags | Push tags to the remote |
Good to know
Rewriting history
commit --amend, rebase and reset change commits. That's fine for commits that only exist on your machine; for commits others have pulled, use git revert instead, or coordinate and push with --force-with-lease.
More references
- HTTP status codesEvery HTTP status code and what it means
- MIME typesCommon MIME types by file extension
- ASCII tableAll 128 ASCII codes in dec, hex, octal and binary
- HTML entitiesNamed HTML entities with their codes
- Regex cheat sheetRegular expression syntax at a glance
- Cron examples30 common cron schedules explained
- Chmod examplesCommon Unix permissions like 644 and 755
- Unix timestamp referenceNotable epochs, the 2038 problem and code
- Markdown cheat sheetMarkdown and GitHub Flavored syntax