Git Cheat Sheet: Essential Commands Every Developer Should Know

DevToolbox

The ultimate Git cheat sheet with practical examples. Learn branching, merging, rebasing, stashing, and more — with an interactive searchable reference.

Git is the backbone of modern software development. Whether you're a solo developer or part of a team of hundreds, Git is how you track changes, collaborate, and ship code. But with hundreds of commands and options, it's easy to forget the exact syntax for that one thing you do every few weeks.

This guide covers the essential Git commands every developer should know, organized by workflow. For a searchable, copy-to-clipboard reference, check our interactive Git Cheat Sheet.

Setting Up Git

Before anything else, configure your identity:

# Set your name and email (used in commits)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Set default branch name to main
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

# Check your configuration
git config --list

Creating and Cloning Repositories

# Initialize a new repository
git init

# Initialize with a specific branch name
git init -b main

# Clone a remote repository
git clone https://github.com/user/repo.git

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

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

The Daily Workflow: Add, Commit, Push

Checking Status

# See what's changed
git status

# Short format
git status -s

# See detailed diff
git diff

# See diff of staged changes
git diff --staged

Staging Changes

# Stage a specific file
git add file.txt

# Stage all changes
git add .

# Stage all changes (including deletions)
git add -A

# Interactively stage parts of a file
git add -p file.txt

# Unstage a file (keep changes)
git restore --staged file.txt

Committing

# Commit with a message
git commit -m "feat: add user authentication"

# Stage all tracked changes and commit
git commit -am "fix: resolve login timeout"

# Amend the last commit (change message or add files)
git commit --amend -m "feat: add user authentication with OAuth"

# Amend without changing the message
git commit --amend --no-edit

# Empty commit (useful for triggering CI)
git commit --allow-empty -m "chore: trigger deployment"

Pushing

# Push to remote
git push origin main

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

# Force push (use with caution!)
git push --force-with-lease origin feature/login

# Push all branches
git push --all

Branching: The Heart of Git

# List branches
git branch          # local
git branch -r       # remote
git branch -a       # all

# Create a new branch
git branch feature/login

# Create and switch to it
git checkout -b feature/login
# or (Git 2.23+)
git switch -c feature/login

# Switch branches
git checkout main
git switch main

# Rename current branch
git branch -m new-name

# Delete a branch (local)
git branch -d feature/login     # safe (only if merged)
git branch -D feature/login     # force

# Delete a remote branch
git push origin --delete feature/login

Merging and Rebasing

Merging

# Merge a branch into current
git merge feature/login

# Merge with a commit message
git merge feature/login -m "Merge feature/login into main"

# Abort a merge with conflicts
git merge --abort

# See merge conflicts
git diff --name-only --diff-filter=U

Rebasing

# Rebase current branch onto main
git rebase main

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

# Continue after resolving conflicts
git rebase --continue

# Abort rebase
git rebase --abort

When to merge vs rebase: Merge preserves history and is safer for shared branches. Rebase creates a linear history and is great for feature branches before merging to main. Never rebase commits that have been pushed to a shared branch.

Stashing: Save Work for Later

# Stash current changes
git stash

# Stash with a description
git stash push -m "WIP: login form validation"

# List all stashes
git stash list

# Apply the most recent stash (keep 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

# Stash including untracked files
git stash -u

Viewing History

# View commit log
git log

# Compact one-line format
git log --oneline

# With graph visualization
git log --oneline --graph --all

# Show changes in each commit
git log -p

# Show stats (files changed, insertions, deletions)
git log --stat

# Filter by author
git log --author="Alice"

# Filter by date range
git log --since="2026-01-01" --until="2026-03-01"

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

# Show who changed each line (blame)
git blame file.txt

# Show a specific commit
git show abc1234

Undoing Things

# Discard changes in a file (restore to last commit)
git restore file.txt
git checkout -- file.txt  # older syntax

# Unstage a file
git restore --staged file.txt

# Revert a commit (creates a new undo commit)
git revert abc1234

# Reset to a previous commit (⚠️ destructive!)
git reset --soft HEAD~1   # keep changes staged
git reset --mixed HEAD~1  # keep changes unstaged (default)
git reset --hard HEAD~1   # discard everything

# Recover a deleted branch or lost commit
git reflog
git checkout abc1234

Working with Remotes

# List remotes
git remote -v

# Add a remote
git remote add upstream https://github.com/original/repo.git

# Fetch changes (without merging)
git fetch origin

# Fetch and merge (pull)
git pull origin main

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

# Remove a remote
git remote remove upstream

Git Aliases: Work Faster

Set up aliases for commands you use constantly:

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.lg "log --oneline --graph --all"
git config --global alias.unstage "restore --staged"
git config --global alias.last "log -1 HEAD"

# Now use them:
git co main
git lg
git st

Git Best Practices

  • Write good commit messages: Use conventional commits (feat:, fix:, chore:) or at least clear, imperative-mood messages.
  • Commit often, push regularly: Small, focused commits are easier to review and revert.
  • Branch per feature: Keep main/master clean and create branches for every feature or fix.
  • Pull before push: Always fetch/pull before pushing to avoid unnecessary merge conflicts.
  • Use .gitignore: Never commit build artifacts,node_modules, .env files, or IDE configs.
  • Use --force-with-lease instead of --force: It prevents overwriting others' work.

Interactive Git Cheat Sheet

Our Git Cheat Sheet has every command above (and more) in a searchable, categorized format. Type any keyword to find the command you need, and copy it to your clipboard with one click.

Need to compare code changes? Try the Diff Viewer. Generating changelogs from your Git history? The Changelog Generator can help.

Summary

Git is deep, but you only need about 20 commands for daily work. Master the basics (add, commit, push, pull, branch, merge), learn stashing and interactive rebase for intermediate workflows, and use aliases to save keystrokes. When you need a quick command reference, the DevToolbox Git Cheat Sheet is always a search away.