Git Cheat Sheet
Essential Git commands organized by workflow: setup, branching, staging, history, remotes, and undoing changes.
A quick reference for everyday Git commands. Organized by workflow so you can find what you need fast.
Configuration
| Command | Description |
|---|
| git config --global user.name "Name" | Set your name |
| git config --global user.email "email" | Set your email |
| git config --global core.editor vim | Set default editor |
| git config --list | Show all settings |
Creating Repositories
| Command | Description |
|---|
| git init | Create a new local repo |
| git clone <url> | Clone a remote repo |
| git clone --depth 1 <url> | Shallow clone (latest commit only) |
Staging & Committing
| Command | Description |
|---|
| git status | Show working tree status |
| git add <file> | Stage a file |
| git add -p | Stage changes interactively (by hunk) |
| git commit -m "msg" | Commit with message |
| git commit --amend | Amend the last commit |
| git diff | Show unstaged changes |
| git diff --staged | Show staged changes |
Branching
| Command | Description |
|---|
| git branch | List local branches |
| git branch <name> | Create a new branch |
| git switch <name> | Switch to a branch |
| git switch -c <name> | Create and switch to a new branch |
| git merge <branch> | Merge branch into current |
| git rebase <branch> | Rebase current onto branch |
| git branch -d <name> | Delete a merged branch |
| git branch -D <name> | Force delete a branch |
Remote Repositories
| Command | Description |
|---|
| git remote -v | List remotes |
| git remote add origin <url> | Add a remote |
| git fetch | Download remote changes |
| git pull | Fetch and merge |
| git pull --rebase | Fetch and rebase |
| git push | Push to remote |
| git push -u origin <branch> | Push and set upstream |
Viewing History
| Command | Description |
|---|
| git log --oneline | Compact commit log |
| git log --graph --all | Visual branch graph |
| git log -p <file> | Show changes to a file over time |
| git show <commit> | Show a specific commit |
| git blame <file> | Show who changed each line |
| git reflog | Show history of HEAD changes |
Undoing Changes
| Command | Description |
|---|
| git restore <file> | Discard working directory changes |
| git restore --staged <file> | Unstage a file |
| git reset --soft HEAD~1 | Undo last commit (keep changes staged) |
| git reset --mixed HEAD~1 | Undo last commit (keep changes unstaged) |
| git reset --hard HEAD~1 | Undo last commit (discard changes) |
| git revert <commit> | Create a new commit that undoes a commit |
Stashing
| Command | Description |
|---|
| git stash | Stash working changes |
| git stash pop | Restore and remove latest stash |
| git stash list | List all stashes |
| git stash drop | Remove the latest stash |
| git stash apply | Restore latest stash (keep in stash list) |
Tags
| Command | Description |
|---|
| git tag <name> | Create a lightweight tag |
| git tag -a <name> -m "msg" | Create an annotated tag |
| git tag | List all tags |
| git push origin <tag> | Push a tag to remote |
| git push origin --tags | Push all tags |