Git has ~150 commands. You actually need about 20. Here are the ones that matter.
Setup (do this first)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global pull.rebase false
# Better diff and merge tools
git config --global diff.tool vscode
git config --global merge.tool vscode
Starting a project
# New project
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/you/repo.git
git push -u origin main
# Clone existing
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git my-folder # Custom folder name
Daily workflow
# Check what's changed
git status
git diff # Unstaged changes
git diff --staged # Staged changes (what will be committed)
# Stage files
git add file.js # Specific file
git add src/ # Entire folder
git add -p # Interactive: choose which hunks to stage
# Commit
git commit -m "Fix login bug"
git commit -am "Quick fix" # Add + commit tracked files
# Push / Pull
git push
git pull
git fetch origin # Download but don't merge
Branches
# Create and switch
git checkout -b feature/user-auth
git switch -c feature/user-auth # Modern syntax
# List branches
git branch # Local
git branch -r # Remote
git branch -a # All
# Switch
git checkout main
git switch main
# Merge
git checkout main
git merge feature/user-auth
# Delete
git branch -d feature/user-auth # Safe (only merged)
git branch -D feature/user-auth # Force
git push origin -d feature/user-auth # Delete remote
Undoing things
# Undo last commit (keep changes staged)
git reset --soft HEAD~1
# Undo last commit (keep changes unstaged)
git reset HEAD~1
# Undo last commit and DISCARD changes (dangerous!)
git reset --hard HEAD~1
# Undo a specific file
git checkout -- file.js # Discard unstaged changes
git restore file.js # Modern syntax
# Revert a commit (creates a new "undo" commit — safe for shared branches)
git revert abc1234
# Fix the last commit message or add forgotten files
git commit --amend
Stashing
# Save work-in-progress
git stash
git stash push -m "WIP: user auth" # With message
# List stashes
git stash list
# Apply stash
git stash pop # Apply + delete
git stash apply stash@{0} # Apply without deleting
# Drop stash
git stash drop stash@{0}
git stash clear # Delete all
Viewing history
git log
git log --oneline # Compact
git log --oneline --graph # Tree view
git log --author="Alice" # Filter by author
git log -- file.js # History of a file
# Search commits
git log --grep="fix" # Search commit messages
git log -S "functionName" # Search added/removed content (pickaxe)
# Show a commit
git show abc1234
git show HEAD # Last commit
Remote management
git remote -v # List remotes
git remote add upstream https://github.com/original/repo.git
# Sync fork with upstream
git fetch upstream
git merge upstream/main
Tags
git tag v1.0.0
git tag -a v1.0.0 -m "Release 1.0.0"
git push origin v1.0.0
git push origin --tags # Push all tags
Useful one-liners
# See who changed a line
git blame file.js
# Find which commit introduced a bug (binary search)
git bisect start
git bisect bad # Current commit is broken
git bisect good v1.0.0 # This version worked
# Git will checkout commits for you to test
git bisect reset # When done
# Clean untracked files
git clean -n # Dry run (show what would be deleted)
git clean -fd # Delete untracked files and folders
# Cherry-pick a commit from another branch
git cherry-pick abc1234
# Rebase interactively (squash, reorder, edit commits)
git rebase -i HEAD~3 # Last 3 commits
.gitignore patterns
# Specific file
.env
secrets.json
# Folder
node_modules/
.venv/
dist/
# Pattern
*.log
*.pyc
.DS_Store
# Negate (include even if parent is ignored)
!important.log
The 10 commands you'll use 90% of the time
git status
git add -p
git commit -m ""
git push
git pull
git checkout -b feature/
git merge main
git stash
git log --oneline
git reset --soft HEAD~1
Git aliases worth setting up
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.lg "log --oneline --graph --all"
git config --global alias.undo "reset --soft HEAD~1"
Now git lg shows a beautiful commit tree. git undo undoes your last commit safely.
Found this useful? Bookmark it for your next git emergency.
Also check out DevToolkit for more developer utilities: https://lucasmdevdev.github.io/devtoolkit/
Top comments (0)