Git Cheat Sheet
Essential Git commands for everyday development. Generate ignore files with our .gitignore Generator.
Setup & Init
| git init | Initialize a new repository |
| git clone <url> | Clone a remote repository |
| git config --global user.name "Name" | Set global username |
| git config --global user.email "email" | Set global email |
Basic Workflow
| git status | Show working tree status |
| git add <file> | Stage a file |
| git add . | Stage all changes |
| git commit -m "message" | Commit staged changes |
| git commit -am "message" | Stage + commit tracked files |
| git diff | Show unstaged changes |
| git diff --staged | Show staged changes |
Branching
| git branch | List local branches |
| git branch <name> | Create a branch |
| git checkout <branch> | Switch to branch |
| git checkout -b <name> | Create and switch to branch |
| git branch -d <name> | Delete a branch (safe) |
| git branch -D <name> | Delete a branch (force) |
| git merge <branch> | Merge branch into current |
| git rebase <branch> | Rebase current onto branch |
Remote
| git remote -v | List remotes |
| git remote add origin <url> | Add a remote |
| git push -u origin <branch> | Push and set upstream |
| git push | Push to upstream |
| git pull | Fetch and merge from upstream |
| git fetch | Fetch without merging |
| git push origin --delete <branch> | Delete remote branch |
Stash
| git stash | Stash working changes |
| git stash pop | Apply and remove latest stash |
| git stash list | List all stashes |
| git stash drop | Remove latest stash |
| git stash apply stash@{n} | Apply specific stash |
History & Undo
| git log --oneline | Compact commit log |
| git log --graph | Visual branch graph |
| 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 commit that undoes a commit |
| git cherry-pick <commit> | Apply a commit from another branch |
| git reflog | Show all reference updates |
Tags
| git tag v1.0.0 | Create lightweight tag |
| git tag -a v1.0.0 -m "msg" | Create annotated tag |
| git push origin v1.0.0 | Push a tag |
| git push origin --tags | Push all tags |
Useful Combos
| git log --oneline --graph --all | Full visual history |
| git diff HEAD~3..HEAD | Changes in last 3 commits |
| git blame <file> | Show who changed each line |
| git clean -fd | Remove untracked files and dirs |
| git bisect start / good / bad | Binary search for a bug |