Git Cheat Sheet
Essential Git commands for everyday development. From setup to advanced workflows — everything you need in one page.
Setup & Config
| Command | Description |
|---|---|
git config --global user.name "Name" | Set your global username |
git config --global user.email "email" | Set your global email |
git config --global core.editor "vim" | Set default editor |
git config --global init.defaultBranch main | Set default branch name to main |
git config --list | List all configuration settings |
git config --global alias.co checkout | Create a shortcut alias |
git config --global credential.helper cache | Cache credentials in memory |
Create & Clone
| Command | Description |
|---|---|
git init | Initialize a new Git repository |
git init <directory> | Create a new repo in a specific directory |
git clone <url> | Clone a remote repository |
git clone <url> <dir> | Clone into a specific directory |
git clone --depth 1 <url> | Shallow clone (latest commit only) |
git clone --branch <branch> <url> | Clone a specific branch |
Stage & Snapshot
| Command | Description |
|---|---|
git status | Show working tree status |
git status -s | Short-format status |
git add <file> | Stage a specific file |
git add . | Stage all changes in current directory |
git add -p | Stage changes interactively (by hunk) |
git reset <file> | Unstage a file (keep changes) |
git commit -m "message" | Commit staged changes with a message |
git commit -am "message" | Stage all tracked files and commit |
git commit --amend | Modify the last commit |
git commit --amend --no-edit | Amend last commit without changing message |
git rm <file> | Remove file from repo and working tree |
git rm --cached <file> | Remove file from repo but keep locally |
git mv <old> <new> | Rename/move a file |
Tip: Use
git add -p to review each change before staging. This helps create clean, focused commits.
Branch & Merge
| Command | Description |
|---|---|
git branch | List local branches |
git branch -a | List all branches (local + remote) |
git branch <name> | Create a new branch |
git branch -d <name> | Delete a merged branch |
git branch -D <name> | Force delete an unmerged branch |
git branch -m <new-name> | Rename current branch |
git checkout <branch> | Switch to a branch |
git checkout -b <branch> | Create and switch to a new branch |
git switch <branch> | Switch branches (modern syntax) |
git switch -c <branch> | Create and switch (modern syntax) |
git merge <branch> | Merge a branch into current branch |
git merge --no-ff <branch> | Merge with a merge commit (no fast-forward) |
git merge --abort | Abort a merge in progress |
git rebase <branch> | Rebase current branch onto another |
git rebase --abort | Abort an in-progress rebase |
git cherry-pick <commit> | Apply a specific commit to current branch |
Warning: Avoid rebasing commits that have been pushed to a shared branch. Rebase rewrites history and can cause issues for collaborators.
Remote & Sync
| Command | Description |
|---|---|
git remote -v | List remote repositories |
git remote add <name> <url> | Add a new remote |
git remote remove <name> | Remove a remote |
git remote rename <old> <new> | Rename a remote |
git fetch | Download objects and refs from remote |
git fetch --all | Fetch from all remotes |
git fetch --prune | Fetch and remove stale remote-tracking refs |
git pull | Fetch and merge remote changes |
git pull --rebase | Fetch and rebase instead of merge |
git push | Push commits to remote |
git push -u origin <branch> | Push and set upstream tracking |
git push origin --delete <branch> | Delete a remote branch |
git push --tags | Push all tags to remote |
Stash
| Command | Description |
|---|---|
git stash | Stash current changes |
git stash push -m "message" | Stash with a description |
git stash -u | Stash including untracked files |
git stash list | List all stashes |
git stash pop | Apply latest stash and remove it |
git stash apply | Apply latest stash (keep in stash list) |
git stash apply stash@{n} | Apply a specific stash |
git stash drop stash@{n} | Delete a specific stash |
git stash clear | Remove all stashes |
git stash show -p | Show stash diff |
Inspect & Compare
| Command | Description |
|---|---|
git log | Show commit history |
git log --oneline | Compact log (one line per commit) |
git log --graph --oneline | Visual branch graph |
git log --author="name" | Filter by author |
git log --since="2 weeks ago" | Filter by date |
git log -p <file> | Show changes to a specific file over time |
git log --stat | Show file change statistics per commit |
git diff | Show unstaged changes |
git diff --staged | Show staged changes (ready to commit) |
git diff <branch1> <branch2> | Compare two branches |
git diff <commit1> <commit2> | Compare two commits |
git show <commit> | Show details of a specific commit |
git blame <file> | Show who changed each line |
git shortlog -sn | Summary of commits by author |
Undo & Rewrite
| Command | Description |
|---|---|
git checkout -- <file> | Discard changes in a file |
git restore <file> | Discard changes (modern syntax) |
git restore --staged <file> | Unstage a file (modern syntax) |
git reset HEAD~1 | Undo last commit (keep changes staged) |
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 and discard all changes |
git revert <commit> | Create a new commit that undoes a specific commit |
git clean -fd | Remove untracked files and directories |
git clean -n | Dry run — show what would be removed |
git reflog | Show reference log (recover lost commits) |
Warning:
git reset --hard permanently discards changes. Use git reflog if you need to recover lost commits.
Tags
| Command | Description |
|---|---|
git tag | List all tags |
git tag <name> | Create a lightweight tag |
git tag -a <name> -m "msg" | Create an annotated tag |
git tag -a <name> <commit> | Tag a specific commit |
git tag -d <name> | Delete a local tag |
git push origin <tag> | Push a tag to remote |
git push origin --delete <tag> | Delete a remote tag |
git checkout <tag> | Checkout a tag (detached HEAD) |
Advanced
| Command | Description |
|---|---|
git bisect start | Start binary search for a bug |
git bisect bad | Mark current commit as bad |
git bisect good <commit> | Mark a known good commit |
git bisect reset | End bisect session |
git submodule add <url> | Add a submodule |
git submodule update --init | Initialize and update submodules |
git worktree add <path> <branch> | Create a linked working tree |
git archive --format=zip HEAD -o file.zip | Create a zip archive of the repo |
git grep "pattern" | Search for a pattern in tracked files |
git log -S "string" | Find commits that added/removed a string |
Common .gitignore Patterns
# Dependencies
node_modules/
vendor/
__pycache__/
# Build output
dist/
build/
*.min.js
*.min.css
# IDE / Editor
.vscode/
.idea/
*.swp
*.swo
# OS files
.DS_Store
Thumbs.db
# Environment
.env
.env.local
*.log