Git Commands Cheat Sheet
Complete Git command reference from init to interactive rebase. Branching, merging, stashing, and remote operations. Searchable and printable.
Setup
| Command | Description | Example |
|---|---|---|
| Initialize a new repository | git init my-project | |
| Clone a remote repository | git clone https://github.com/user/repo.git | |
| Set your Git username | git config --global user.name "John" | |
| Set your Git email | git config --global user.email "john@example.com" | |
| View all configuration | git config --list --global | |
| Set default editor | git config --global core.editor "code --wait" |
Basics
| Command | Description | Example |
|---|---|---|
| Show working tree status | git status -s (short format) | |
| Stage a file for commit | git add index.html | |
| Stage all changes | git add -A (includes deletions) | |
| Commit with a message | git commit -m "Add login feature" | |
| Stage tracked files and commit | git commit -am "Quick fix" | |
| Modify the last commit | git commit --amend --no-edit | |
| Remove file from repo | git rm --cached secret.env | |
| Rename/move a file | git mv old.txt new.txt | |
| Show unstaged changes | git diff --staged (staged changes) | |
| Show all changes since last commit | git diff HEAD~3 (last 3 commits) |
Branching
| Command | Description | Example |
|---|---|---|
| List all local branches | git branch -a (all branches) | |
| Create a new branch | git branch feature/login | |
| Switch to a branch | git checkout develop | |
| Create and switch to branch | git checkout -b feature/api | |
| Switch branches (modern) | git switch main | |
| Create and switch (modern) | git switch -c hotfix/bug-123 | |
| Delete a merged branch | git branch -D feature/old (force) | |
| Rename current branch | git branch -m new-name |
Merging
| Command | Description | Example |
|---|---|---|
| Merge branch into current | git merge feature/login | |
| Merge with merge commit | git merge --no-ff develop | |
| Squash all commits into one | git merge --squash feature/x | |
| Abort a conflicted merge | git merge --abort | |
| Rebase onto another branch | git rebase main | |
| Interactive rebase last N commits | git rebase -i HEAD~5 | |
| Apply a specific commit | git cherry-pick abc123 |
Remote
| Command | Description | Example |
|---|---|---|
| List remote repositories | git remote -v | |
| Add a remote | git remote add origin https://... | |
| Push commits to remote | git push origin main | |
| Push and set upstream | git push -u origin feature/x | |
| Force push safely | git push --force-with-lease | |
| Fetch and merge from remote | git pull origin main | |
| Pull with rebase | git pull --rebase origin main | |
| Download remote changes | git fetch --all | |
| Delete remote branch | git push origin --delete feature/old |
Stashing
| Command | Description | Example |
|---|---|---|
| Stash working changes | git stash (saves current work) | |
| Stash with a description | git stash save "WIP: login form" | |
| List all stashes | git stash list | |
| Apply and remove last stash | git stash pop stash@{2} | |
| Apply stash without removing | git stash apply stash@{0} | |
| Delete a stash | git stash drop stash@{1} | |
| Delete all stashes | git stash clear |
History
| Command | Description | Example |
|---|---|---|
| Show commit history | git log --oneline --graph | |
| Compact commit history | git log --oneline -10 | |
| Visual branch graph | git log --graph --all --oneline | |
| Show changes to a file | git log -p src/app.js | |
| Show a specific commit | git show abc123 | |
| Show who changed each line | git blame src/index.js | |
| Commits per author | git shortlog -sn --all | |
| Show reference log (undo history) | git reflog --all |
Undoing
| Command | Description | Example |
|---|---|---|
| Discard working changes | git restore src/app.js | |
| Unstage a file | git restore --staged . | |
| Undo last commit (keep changes) | git reset HEAD~1 | |
| Undo last commit (discard changes) | git reset --hard HEAD~3 | |
| Reset to remote state | git reset --hard origin/main | |
| Create undo commit | git revert abc123 | |
| Remove untracked files/dirs | git clean -n (dry run first) |
Tags
| Command | Description | Example |
|---|---|---|
| Create a lightweight tag | git tag v1.0.0 | |
| Create an annotated tag | git tag -a v1.0 -m "Release 1.0" | |
| List all tags | git tag -l "v1.*" | |
| Push all tags to remote | git push origin v1.0.0 | |
| Delete a local tag | git tag -d v0.9 |
Advanced
| Command | Description | Example |
|---|---|---|
| Binary search for bad commit | git bisect start / bad / good | |
| Add a submodule | git submodule add https://... | |
| Create linked working tree | git worktree add ../hotfix main | |
| Export repo as archive | git archive --format=zip HEAD > repo.zip | |
| Clean up and optimize repo | git gc --aggressive |
Frequently asked questions
What's the difference between git merge and git rebase?
Merge creates a merge commit that combines two branches, preserving the full history. Rebase replays your commits on top of another branch, creating a linear history. Use merge for shared branches, rebase for cleaning up local feature branches before merging.
How do I undo the last commit?
'git reset HEAD~1' undoes the commit but keeps your changes staged. 'git reset --soft HEAD~1' keeps changes in working directory. 'git reset --hard HEAD~1' discards everything. If already pushed, use 'git revert' instead to create a new undo commit.
What's the difference between git fetch and git pull?
'git fetch' downloads changes from the remote but doesn't merge them - it's safe and non-destructive. 'git pull' is essentially 'git fetch + git merge'. Use fetch when you want to review changes before merging.
How do I resolve merge conflicts?
Open the conflicted files and look for <<<<<<< markers. Edit to keep the code you want, remove the markers, then 'git add' the resolved files and 'git commit'. Use 'git merge --abort' to cancel the merge entirely.
What does git stash do exactly?
git stash temporarily shelves your uncommitted changes (both staged and unstaged) so you can work on something else. 'git stash pop' restores them. Think of it as a clipboard for your working directory.
How do I delete a remote branch?
Use 'git push origin --delete branch-name'. To delete a local branch, use 'git branch -d branch-name' (safe) or 'git branch -D branch-name' (force delete even if not merged).
Go from reference to real skills
Cheat sheets are great for quick lookups. Our in-depth courses take you from the fundamentals to professional-level mastery.
Browse all courses