Version Control
Git Cheatsheet
Quick reference for essential Git commands including init, clone, branch, merge, rebase, stash, log, reset, and cherry-pick.
Setup & Init
| Command | Description |
|---|---|
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 |
Staging & Committing
| Command | Description |
|---|---|
git status | Show working tree status |
git add <file> | Stage a file |
git add -A | Stage all changes |
git add -p | Interactively stage hunks |
git commit -m "msg" | Commit with message |
git commit --amend | Amend last commit |
git diff | Show unstaged changes |
git diff --staged | Show staged changes |
Branching
| Command | Description |
|---|---|
git branch | List local branches |
git branch -a | List all branches (including remote) |
git branch <name> | Create a branch |
git branch -d <name> | Delete a branch (safe) |
git branch -D <name> | Force delete a branch |
git checkout <branch> | Switch to branch |
git checkout -b <branch> | Create and switch to branch |
git switch <branch> | Switch to branch (modern) |
git switch -c <branch> | Create and switch (modern) |
Merging & Rebasing
| Command | Description |
|---|---|
git merge <branch> | Merge branch into current |
git merge --no-ff <branch> | Merge with merge commit |
git merge --squash <branch> | Squash merge (no commit) |
git rebase <branch> | Rebase current onto branch |
git rebase -i HEAD~<n> | Interactive rebase last n commits |
git rebase --abort | Abort in-progress rebase |
git rebase --continue | Continue after resolving conflicts |
Stashing
| Command | Description |
|---|---|
git stash | Stash working changes |
git stash push -m "msg" | Stash with message |
git stash list | List all stashes |
git stash pop | Apply and remove latest stash |
git stash apply | Apply latest stash (keep in list) |
git stash drop | Remove latest stash |
git stash clear | Remove all stashes |
git stash show -p | Show stash diff |
Log & History
| Command | Description |
|---|---|
git log | Show commit history |
git log --oneline | Compact log |
git log --graph --oneline --all | Visual branch graph |
git log -p <file> | Show changes to a file over time |
git log --author="name" | Filter by author |
git log --since="2 weeks ago" | Filter by date |
git blame <file> | Show who changed each line |
git shortlog -sn | Commit count by author |
Undoing & Resetting
| Command | Description |
|---|---|
git reset <file> | Unstage a file |
git reset --soft HEAD~1 | Undo commit, keep changes staged |
git reset --mixed HEAD~1 | Undo commit, keep changes unstaged |
git reset --hard HEAD~1 | Undo commit, discard changes |
git revert <commit> | Create a new commit undoing a commit |
git checkout -- <file> | Discard changes to a file |
git restore <file> | Discard changes (modern) |
git restore --staged <file> | Unstage a file (modern) |
git clean -fd | Remove untracked files and dirs |
Cherry-Pick
| Command | Description |
|---|---|
git cherry-pick <commit> | Apply a specific commit |
git cherry-pick <a>..<b> | Apply a range of commits |
git cherry-pick --no-commit <commit> | Apply without committing |
git cherry-pick --abort | Abort cherry-pick |
Remote
| Command | Description |
|---|---|
git remote -v | List remotes |
git remote add origin <url> | Add remote |
git fetch | Fetch all remotes |
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 |
git push --force-with-lease | Safe force push |
Tags
| Command | Description |
|---|---|
git tag | List tags |
git tag v1.0.0 | Create lightweight tag |
git tag -a v1.0.0 -m "msg" | Create annotated tag |
git push --tags | Push all tags to remote |
git tag -d v1.0.0 | Delete a local tag |