Git Cheat Sheet

Essential Git commands for everyday development. From setup to advanced workflows — everything you need in one page.

Setup & Config

CommandDescription
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 mainSet default branch name to main
git config --listList all configuration settings
git config --global alias.co checkoutCreate a shortcut alias
git config --global credential.helper cacheCache credentials in memory

Create & Clone

CommandDescription
git initInitialize 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

CommandDescription
git statusShow working tree status
git status -sShort-format status
git add <file>Stage a specific file
git add .Stage all changes in current directory
git add -pStage 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 --amendModify the last commit
git commit --amend --no-editAmend 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

CommandDescription
git branchList local branches
git branch -aList 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 --abortAbort a merge in progress
git rebase <branch>Rebase current branch onto another
git rebase --abortAbort 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

CommandDescription
git remote -vList 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 fetchDownload objects and refs from remote
git fetch --allFetch from all remotes
git fetch --pruneFetch and remove stale remote-tracking refs
git pullFetch and merge remote changes
git pull --rebaseFetch and rebase instead of merge
git pushPush commits to remote
git push -u origin <branch>Push and set upstream tracking
git push origin --delete <branch>Delete a remote branch
git push --tagsPush all tags to remote

Stash

CommandDescription
git stashStash current changes
git stash push -m "message"Stash with a description
git stash -uStash including untracked files
git stash listList all stashes
git stash popApply latest stash and remove it
git stash applyApply 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 clearRemove all stashes
git stash show -pShow stash diff

Inspect & Compare

CommandDescription
git logShow commit history
git log --onelineCompact log (one line per commit)
git log --graph --onelineVisual 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 --statShow file change statistics per commit
git diffShow unstaged changes
git diff --stagedShow 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 -snSummary of commits by author

Undo & Rewrite

CommandDescription
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~1Undo last commit (keep changes staged)
git reset --soft HEAD~1Undo last commit (keep changes staged)
git reset --mixed HEAD~1Undo last commit (keep changes unstaged)
git reset --hard HEAD~1Undo last commit and discard all changes
git revert <commit>Create a new commit that undoes a specific commit
git clean -fdRemove untracked files and directories
git clean -nDry run — show what would be removed
git reflogShow reference log (recover lost commits)
Warning: git reset --hard permanently discards changes. Use git reflog if you need to recover lost commits.

Tags

CommandDescription
git tagList 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

CommandDescription
git bisect startStart binary search for a bug
git bisect badMark current commit as bad
git bisect good <commit>Mark a known good commit
git bisect resetEnd bisect session
git submodule add <url>Add a submodule
git submodule update --initInitialize and update submodules
git worktree add <path> <branch>Create a linked working tree
git archive --format=zip HEAD -o file.zipCreate 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