DEV Community

Lucas M Dev
Lucas M Dev

Posted on

The Git Cheat Sheet Every Developer Should Bookmark (2026 Edition)

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

.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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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)