← Back to Blog

Git Cheat Sheet: 30 Commands Every Developer Needs

March 9, 2026 10 min read By CodeTidy Team

Git has over 150 commands, but you'll use about 30 of them for 99% of your work. This cheat sheet focuses on the commands that matter, organized by workflow rather than alphabetically. Bookmark this page — you'll come back to it.

Setup and Configuration

# Set your identity (required for commits)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Set default branch name for new repos
git config --global init.defaultBranch main

# Enable colored output
git config --global color.ui auto

# Set your preferred editor
git config --global core.editor "code --wait"  # VS Code
git config --global core.editor "vim"           # Vim

# View all settings
git config --list

Starting a Repository

# Initialize a new repository
git init

# Clone an existing repository
git clone https://github.com/user/repo.git

# Clone into a specific directory
git clone https://github.com/user/repo.git my-project

# Clone only the latest commit (faster for large repos)
git clone --depth 1 https://github.com/user/repo.git

Daily Workflow

The commands you'll use dozens of times per day:

# Check status of working directory
git status

# Short format (less verbose)
git status -s

# Stage specific files
git add file1.js file2.js

# Stage all changes in current directory
git add .

# Stage parts of a file interactively
git add -p file.js

# Commit with a message
git commit -m "Add user authentication"

# Commit with a longer message (opens editor)
git commit

# Stage all tracked files and commit in one step
git commit -am "Fix login redirect bug"

Viewing Changes

# Show unstaged changes
git diff

# Show staged changes (what will be committed)
git diff --staged

# Show changes in a specific file
git diff path/to/file.js

# Show changes between branches
git diff main..feature-branch

# Show changes between commits
git diff abc123..def456

# Show commit history
git log

# Compact one-line log
git log --oneline

# Log with graph visualization
git log --oneline --graph --all

# Show changes in each commit
git log -p

# Show last 5 commits
git log -5

# Search commit messages
git log --grep="fix login"

# Show who changed each line of a file
git blame file.js

For comparing longer text blocks visually, you can also use our Diff Checker tool for a side-by-side view with syntax highlighting.

Branching

# List branches
git branch

# List all branches (including remote)
git branch -a

# Create a new branch
git branch feature-auth

# Create and switch to a new branch
git checkout -b feature-auth
# Or (newer syntax):
git switch -c feature-auth

# Switch to an existing branch
git checkout main
# Or:
git switch main

# Delete a branch (must be merged)
git branch -d feature-auth

# Force delete an unmerged branch
git branch -D feature-auth

# Rename current branch
git branch -m new-name

Merging and Rebasing

# Merge a branch into current branch
git merge feature-auth

# Merge without fast-forward (always creates a merge commit)
git merge --no-ff feature-auth

# Rebase current branch onto main
git rebase main

# Interactive rebase (squash, reorder, edit commits)
git rebase -i HEAD~3

# Abort a merge or rebase in progress
git merge --abort
git rebase --abort

# Continue after resolving conflicts
git rebase --continue

When to merge vs rebase

  • Merge when you want to preserve the exact history of how branches converged. Best for shared/public branches.
  • Rebase when you want a linear history. Best for local feature branches before merging to main. Never rebase commits that have been pushed and shared with others.

Working with Remotes

# Show remotes
git remote -v

# Add a remote
git remote add origin https://github.com/user/repo.git

# Fetch changes from remote (doesn't merge)
git fetch origin

# Pull changes (fetch + merge)
git pull origin main

# Pull with rebase (cleaner history)
git pull --rebase origin main

# Push to remote
git push origin main

# Push and set upstream (first push of a branch)
git push -u origin feature-auth

# Push all branches
git push --all origin

# Delete a remote branch
git push origin --delete feature-auth

Stashing

Temporarily save uncommitted changes to switch context:

# Stash current changes
git stash

# Stash with a description
git stash push -m "WIP: auth feature"

# 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}

# Drop a specific stash
git stash drop stash@{0}

# Clear all stashes
git stash clear

Undoing Things

# Unstage a file (keep changes in working directory)
git restore --staged file.js
# Or (older syntax):
git reset HEAD file.js

# Discard changes in working directory
git restore file.js
# Or:
git checkout -- file.js

# Undo the last commit (keep changes staged)
git reset --soft HEAD~1

# Undo the last commit (keep changes unstaged)
git reset HEAD~1

# Undo the last commit (discard all changes) ⚠️
git reset --hard HEAD~1

# Create a new commit that reverses a previous commit
git revert abc123

# Amend the last commit message
git commit --amend -m "New message"

# Add forgotten files to the last commit
git add forgotten-file.js
git commit --amend --no-edit
Warning: git reset --hard permanently discards changes. Use git stash first if you might need them later.

Advanced: Cherry-pick, Bisect, and Clean

# Apply a specific commit from another branch
git cherry-pick abc123

# Find which commit introduced a bug (binary search)
git bisect start
git bisect bad              # current commit is broken
git bisect good abc123      # this older commit was working
# Git checks out a middle commit — test it, then:
git bisect good  # or  git bisect bad
# Repeat until Git identifies the culprit
git bisect reset            # return to original branch

# Remove untracked files
git clean -n    # dry run (show what would be deleted)
git clean -f    # delete untracked files
git clean -fd   # delete untracked files and directories

Useful Aliases

Add these to your ~/.gitconfig to save keystrokes:

[alias]
  s = status -s
  co = checkout
  br = branch
  ci = commit
  lg = log --oneline --graph --all --decorate
  unstage = restore --staged
  last = log -1 HEAD
  amend = commit --amend --no-edit

Now git lg shows a beautiful graph, git s gives a quick status, and git amend adds staged changes to the last commit.

.gitignore Essentials

# Dependencies
node_modules/
vendor/
__pycache__/
.venv/

# Build output
dist/
build/
*.o
*.pyc

# Environment and secrets
.env
.env.local
*.pem

# IDE files
.vscode/
.idea/
*.swp
.DS_Store

# Logs
*.log
npm-debug.log*

For managing the environment variables referenced in your .gitignore, see our guide on .env file best practices.

Quick Reference Card

TaskCommand
Create branchgit switch -c name
Stage everythinggit add .
Commitgit commit -m "msg"
Push new branchgit push -u origin name
Update from maingit pull --rebase origin main
Undo last commitgit reset --soft HEAD~1
Save work temporarilygit stash
View changesgit diff
Find bug introductiongit bisect start
Apply specific commitgit cherry-pick hash

Need help building the right Git command for a specific situation? Try our interactive Git Command Builder — select your scenario and get the exact command with an explanation.

Drop file to load