Git Commands Cheatsheet
A quick reference for everyday Git commands — setup, staging, committing, branching, merging, rebasing, and remote operations.
Sections
Setup & Config
| Command | Description |
|---|---|
git init | Initialise a new repository in the current directory |
git clone <url> | Clone a remote repository locally |
git config --global user.name "Name" | Set your commit author name globally |
git config --global user.email "…" | Set your commit author email globally |
git config --list | Show all config values |
Staging & Committing
| Command | Description |
|---|---|
git status | Show working tree status — staged, unstaged, and untracked files |
git add <file> | Stage a specific file |
git add . | Stage all changes in the current directory |
git add -p | Interactively stage hunks |
git commit -m "msg" | Commit staged changes with a message |
git commit --amend | Rewrite the last commit (message and/or content) |
git diff | Show unstaged changes |
git diff --staged | Show staged changes vs last commit |
git log --oneline | Compact one-line commit history |
git log --graph | ASCII graph of branch history |
Branching
| Command | Description |
|---|---|
git branch | List local branches |
git branch -a | List all branches (local + remote) |
git branch <name> | Create a new branch |
git switch <name> | Switch to an existing branch (modern syntax) |
git switch -c <name> | Create and switch to a new branch |
git branch -d <name> | Delete a merged branch |
git branch -D <name> | Force-delete a branch (even if unmerged) |
git branch -m <old> <new> | Rename a branch |
Merging & Rebasing
| Command | Description |
|---|---|
git merge <branch> | Merge branch into the current branch |
git merge --no-ff <branch> | Merge with a merge commit (no fast-forward) |
git merge --squash <branch> | Squash all commits from branch into one staged change |
git rebase <branch> | Rebase current branch on top of branch |
git rebase -i HEAD~n | Interactive rebase — squash, edit, reorder last n commits |
git cherry-pick <hash> | Apply a specific commit onto the current branch |
Remote Operations
| Command | Description |
|---|---|
git remote -v | Show remote URLs |
git remote add origin <url> | Add a remote named origin |
git fetch | Download remote changes without merging |
git pull | Fetch and merge remote changes |
git pull --rebase | Fetch and rebase instead of merge |
git push origin <branch> | Push branch to remote |
git push -u origin <branch> | Push and set upstream tracking |
git push --force-with-lease | Safe force push — fails if remote has new commits |
Undoing Changes
| Command | Description |
|---|---|
git restore <file> | Discard unstaged changes to a file |
git restore --staged <file> | Unstage a file (keep changes in working tree) |
git revert <hash> | Create a new commit that undoes a commit (safe for shared history) |
git reset --soft HEAD~1 | Undo last commit, keep changes staged |
git reset --mixed HEAD~1 | Undo last commit, keep changes unstaged (default) |
git reset --hard HEAD~1 | Undo last commit and discard all changes — destructive |
git clean -fd | Delete untracked files and directories |
Stash
| Command | Description |
|---|---|
git stash | Stash uncommitted changes |
git stash push -m "msg" | Stash with a descriptive message |
git stash list | Show all stashes |
git stash pop | Apply latest stash and remove it from the list |
git stash apply | Apply latest stash without removing it |
git stash drop | Delete the latest stash entry |
git stash clear | Remove all stashes |
Tags
| Command | Description |
|---|---|
git tag | List all tags |
git tag v1.0.0 | Create a lightweight tag on HEAD |
git tag -a v1.0.0 -m "msg" | Create an annotated tag with a message |
git push origin v1.0.0 | Push a specific tag to remote |
git push origin --tags | Push all tags to remote |
git tag -d v1.0.0 | Delete a local tag |
git push origin --delete v1.0.0 | Delete a remote tag |