Essential Git Commands Cheatsheet for Developers
· 9 min read
Setup & Configuration
Before using Git, configure your identity. These settings are stored globally and attached to every commit you make:
# Set your name and email
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
# Set default branch name to 'main'
git config --global init.defaultBranch main
# Enable color output
git config --global color.ui auto
# Set your preferred editor
git config --global core.editor "code --wait"
# View all configuration
git config --list
These commands only need to be run once per machine. The --global flag applies settings to all repositories. Drop it to configure settings for a single repo.
Basic Git Commands
These are the commands you'll use every day:
# Initialize a new repository
git init
# Clone an existing repository
git clone https://github.com/user/repo.git
git clone [email protected]:user/repo.git # SSH
# Check status of working directory
git status
git status -s # Short format
# Stage files for commit
git add filename.js # Single file
git add . # All changes
git add -p # Interactive staging (patch mode)
# Commit staged changes
git commit -m "feat: add user authentication"
git commit -am "fix: resolve login bug" # Stage + commit tracked files
# View differences
git diff # Unstaged changes
git diff --staged # Staged changes
git diff main..feature # Between branches
🛠️ Useful developer tools
Branching & Merging
Branches let you work on features independently. Mastering branching is essential for any Git workflow:
# List branches
git branch # Local branches
git branch -r # Remote branches
git branch -a # All branches
# Create and switch to a new branch
git checkout -b feature/login
# Or the newer syntax:
git switch -c feature/login
# Switch to an existing branch
git checkout main
git switch main
# Merge a branch into current branch
git merge feature/login
# Merge with no fast-forward (preserves branch history)
git merge --no-ff feature/login
# Delete a branch
git branch -d feature/login # Safe delete (only if merged)
git branch -D feature/login # Force delete
# Rename current branch
git branch -m new-name
Resolving Merge Conflicts
When Git can't auto-merge, you'll see conflict markers in the affected files:
<<<<<<< HEAD
const timeout = 3000;
=======
const timeout = 5000;
>>>>>>> feature/update-timeout
Edit the file to keep the correct version, remove the markers, then:
git add resolved-file.js
git commit -m "fix: resolve merge conflict in timeout value"
Use our Diff Checker to compare conflicting versions side by side before deciding which to keep.
Remote Operations
Working with remote repositories — pushing, pulling, and syncing:
# Add a remote
git remote add origin https://github.com/user/repo.git
# View remotes
git remote -v
# Push to remote
git push origin main
git push -u origin main # Set upstream (then just 'git push')
git push --force-with-lease # Safer force push
# Pull from remote (fetch + merge)
git pull origin main
git pull --rebase origin main # Rebase instead of merge
# Fetch without merging
git fetch origin
git fetch --all # All remotes
# Push a new branch to remote
git push -u origin feature/new-feature
# Delete remote branch
git push origin --delete feature/old-branch
Stashing Changes
Stash lets you save uncommitted changes temporarily — perfect when you need to switch branches quickly:
# Stash current changes
git stash
git stash save "work in progress: login form"
# Stash including untracked files
git stash -u
# List all stashes
git stash list
# Apply most recent stash (keeps it in stash list)
git stash apply
# Apply and remove from stash list
git stash pop
# Apply a specific stash
git stash apply stash@{2}
# View stash contents
git stash show -p stash@{0}
# Drop a specific stash
git stash drop stash@{1}
# Clear all stashes
git stash clear
History & Inspection
Understanding project history is crucial for debugging and code review:
# View commit log
git log
git log --oneline # Compact format
git log --graph --oneline # Visual branch graph
git log -5 # Last 5 commits
git log --author="John" # Filter by author
git log --since="2 weeks ago"
git log --grep="fix" # Search commit messages
# View a specific commit
git show abc1234
# Who changed each line? (blame)
git blame filename.js
git blame -L 10,20 filename.js # Lines 10-20
# Search through code history
git log -S "functionName" # Find when a string was added/removed
git log -p -- path/to/file # Full diff history of a file
Undoing Changes
Everyone makes mistakes. Here's how to undo them safely:
# Discard changes in working directory
git checkout -- filename.js
git restore filename.js # Newer syntax
# Unstage a file
git reset HEAD filename.js
git restore --staged filename.js
# Amend the last commit (change message or add files)
git commit --amend -m "updated commit message"
git add forgotten-file.js && git commit --amend --no-edit
# Undo last commit but keep changes staged
git reset --soft HEAD~1
# Undo last commit and unstage changes
git reset HEAD~1
# Undo last commit and discard changes (DANGEROUS)
git reset --hard HEAD~1
# Create a new commit that undoes a previous commit
git revert abc1234
# Recover deleted branch or lost commits
git reflog
git checkout -b recovered-branch abc1234
Advanced Commands
Power user commands for complex workflows:
# Interactive rebase (rewrite history)
git rebase -i HEAD~5 # Edit last 5 commits
# Cherry-pick a commit from another branch
git cherry-pick abc1234
# Create a tag
git tag v1.0.0
git tag -a v1.0.0 -m "Release version 1.0.0"
git push origin v1.0.0
# Bisect (binary search for bugs)
git bisect start
git bisect bad # Current commit is broken
git bisect good abc1234 # This commit was working
# Git checks out middle commits for you to test
# Clean untracked files
git clean -n # Dry run
git clean -fd # Force delete files and directories
# Worktrees (multiple working directories)
git worktree add ../hotfix hotfix-branch
git worktree list
git worktree remove ../hotfix
Frequently Asked Questions
What's the difference between git merge and git rebase?
Git merge creates a merge commit that combines two branches, preserving the full branching history. Git rebase replays your commits on top of another branch, creating a clean, linear history. Use merge for shared/public branches, rebase for local feature branches before merging.
How do I undo a git push?
Use git revert <commit> to create a new commit that undoes the changes, then push normally. Avoid git push --force on shared branches as it rewrites history for everyone. If you must force push, use --force-with-lease which checks that nobody else has pushed since your last fetch.
What is git stash used for?
Git stash temporarily saves uncommitted changes so you can switch branches or pull updates without committing half-done work. Use git stash to save and git stash pop to restore your changes.
How do I compare two branches in Git?
Use git diff main..feature to see code differences between branches. For a visual comparison, use our Diff Checker tool to paste and compare code side by side.