Essential Git Commands Every Developer Should Know
A concise Git cheatsheet covering repository setup, branching, rebasing, stashing, and common workflows.
Repository Setup
git init # Initialize repository
git clone <repo-url> # Clone repository
git remote add origin <url> # Add remoteBasic File Operations
git status # Check status
git add <file> # Add specific file
git add . # Add all files
git rm <file> # Remove file
git rm --cached <file> # Unstage fileCommitting
git commit -m "message" # Commit staged
git commit -am "message" # Add & commit
git commit --amend -m "new" # Amend last commitBranching
git branch # List branches
git branch <name> # Create branch
git checkout <name> # Switch branch
git checkout -b <name> # Create & switch
git switch <name> # Modern switch
git switch -c <name> # Modern create & switch
git branch -d <name> # Delete merged branch
git branch -D <name> # Force deleteRemote Sync
git fetch # Fetch changes
git pull # Fetch + merge
git pull origin main # Pull main
git push # Push changes
git push origin <branch> # Push branch
git push -u origin <branch> # Set upstreamMerging & Rebasing
git merge <branch> # Merge branch
git merge --no-ff <branch> # No fast-forward
git rebase <branch> # Rebase branch
git rebase -i HEAD~3 # Interactive rebaseHistory
git log # Full log
git log --oneline # Compact log
git log --graph # Graph view
git show <hash> # Show commit
git diff # Working vs staged
git diff --staged # Staged vs lastUndoing
git reset <file> # Unstage file
git reset # Unstage all
git checkout -- <file> # Discard file changes
git reset --soft HEAD~1 # Keep staged
git reset --mixed HEAD~1 # Keep unstaged
git reset --hard HEAD~1 # Discard all
git revert <hash> # Revert commitTagging
git tag v1.0.0 # Create tag
git tag -a v1.0.0 -m "Release" # Annotated tag
git push origin v1.0.0 # Push tag
git push origin --tags # Push all tagsStashing
git stash # Stash changes
git stash push -m "msg" # Named stash
git stash pop # Apply & drop
git stash apply # Apply keep
git stash list # List stashes
git stash drop stash@{0} # Drop stashConfig
git config --global user.name "Your Name"
git config --global user.email "you@email.com"
git config --listReal-World Workflow Example
# 1. Start new feature
git checkout main
git pull origin main
git checkout -b feature/file-upload
# 2. Make changes
git add .
git commit -m "Add file upload functionality"
# 3. Push and create PR
git push -u origin feature/file-upload
# 4. After PR approval, cleanup
git checkout main
git pull origin main
git branch -d feature/file-uploadPro Tips
- Use meaningful commit messages (Conventional Commits)
- Commit often (small, focused commits)
- Pull before push (always sync with remote)
- Use branches (don't work directly on
main) - Review before commit (
git status,git diff)
