Quick reference for the most commonly used Git commands.
# Set your name and email
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# Set default editor
git config --global core.editor "code --wait"
# Set default branch name
git config --global init.defaultBranch main
# View config
git config --list
git config --global --listgit init # Initialize new repository
git clone <url> # Clone remote repositorygit status # Show working directory status
git add <file> # Stage specific file
git add . # Stage all changes
git commit -m "message" # Commit staged changes
git commit -am "message" # Stage and commit (tracked files only)
git commit --amend # Amend last commitgit diff # Show unstaged changes
git diff --staged # Show staged changes
git diff HEAD # Show all changes since last commitgit branch # List local branches
git branch <name> # Create new branch
git branch -d <name> # Delete branch (safe)
git branch -D <name> # Force delete branch
git branch -m <new-name> # Rename current branchgit switch <branch> # Switch to branch
git switch -c <branch> # Create and switch to branch
git checkout <branch> # (Legacy) Switch to branchgit merge <branch> # Merge branch into current
git merge --no-ff <branch> # Merge with merge commit
git merge --abort # Abort mergegit remote -v # List remotes
git remote add origin <url> # Add remote
git remote set-url origin <url> # Change remote URL
git remote prune origin # Remove stale remote branchesgit fetch # Download objects from remote
git fetch --prune # Fetch and prune stale branches
git pull # Fetch and merge
git pull --rebase # Fetch and rebase
git push # Upload to remote
git push -u origin <branch> # Push and set upstream
git push --force-with-lease # Safe force pushgit clone <url> # Clone repository
git clone <url> <directory> # Clone to specific directory
git clone --branch <name> <url> # Clone specific branchgit restore <file> # Discard changes in file
git restore --staged <file> # Unstage file
git reset <file> # Unstage file (legacy)
git checkout -- <file> # Restore file (legacy)git reset --soft HEAD~1 # Undo commit, keep changes staged
git reset HEAD~1 # Undo commit, keep changes unstaged
git reset --hard HEAD~1 # Undo commit, discard changesgit revert <commit> # Create new commit undoing changes
git revert --no-commit <commit> # Revert without committinggit stash # Save changes temporarily
git stash push -m "message" # Stash with message
git stash list # List all stashes
git stash pop # Apply and remove last stash
git stash apply # Apply last stash (keep it)
git stash drop # Delete last stash
git stash clear # Delete all stashesgit log # Show commit history
git log --oneline # Compact log
git log --oneline --graph --all # Visual branch history
git log -n 5 # Show last 5 commits
git log --author="name" # Filter by author
git log --since="2 weeks ago" # Time-based filter
git log -- <file> # History of specific filegit show <commit> # Show commit details
git show HEAD # Show last commit
git diff <commit1> <commit2> # Compare commits
git diff <branch1> <branch2> # Compare branchesgit blame <file> # Show who changed each line
git grep "search term" # Search in repositorygit rebase <branch> # Rebase current branch
git rebase -i HEAD~3 # Interactive rebase last 3 commits
git rebase --continue # Continue after resolving conflicts
git rebase --abort # Cancel rebasegit cherry-pick <commit> # Apply specific commitgit tag # List tags
git tag <name> # Create lightweight tag
git tag -a <name> -m "message" # Create annotated tag
git push origin --tags # Push tags to remotegit reflog # Show reference log
git reflog show <branch> # Show reflog for branchgit clean -n # Preview untracked files to remove
git clean -f # Remove untracked files
git clean -fd # Remove untracked files and directoriesAdd these to your .gitconfig for faster workflow:
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global alias.undo "reset HEAD~1 --mixed"
git config --global alias.unstage "reset HEAD --"
git config --global alias.amend "commit --amend --no-edit"
git config --global alias.pushf "push --force-with-lease"
git config --global alias.up "pull --rebase --autostash"# Undo last commit (keep changes)
git reset HEAD~1
# Discard all local changes
git reset --hard HEAD
git clean -fd
# Recover deleted commit
git reflog # Find commit hash
git cherry-pick <hash> # Restore commit
# Fix wrong branch
git branch <new-branch> # Create branch with current commit
git reset --hard HEAD~1 # Remove commit from current branch
git switch <new-branch> # Switch to new branch
# Abort operations
git merge --abort
git rebase --abort
git cherry-pick --abort| Command | Description |
|---|---|
git init |
Initialize repository |
git clone <url> |
Clone repository |
git status |
Check status |
git add . |
Stage all changes |
git commit -m "msg" |
Commit changes |
git push |
Push to remote |
git pull |
Pull from remote |
git branch |
List branches |
git switch <branch> |
Switch branch |
git merge <branch> |
Merge branch |
git log |
View history |
git diff |
Show changes |
git stash |
Save work temporarily |
git reset --hard HEAD |
Discard all changes |
git reflog |
Recovery tool |
For detailed explanations and examples:
- 📘 Basics — Fundamental Git commands and usage
- ⚙️ Advanced — Advanced Git commands and workflows
- 🧩 Prefixes — Commit and branch naming conventions
- 🚀 Modern — Modern Git features and tools
- 🔧 Troubleshooting — Common problems and solutions
- ⚙️ Config — Useful aliases and configuration examples
- 💡 Tips — Practical tips and best practices
- 🔗 Resources — External references and learning materials
Print this cheat sheet and keep it nearby for quick reference while learning Git!