Essential Git Commands Cheat Sheet: Complete Guide for 2026
Published February 4, 2026 • 14 min read
Git is the version control system that powers modern software development. Whether you're a beginner learning the basics or an experienced developer looking for a quick reference, this comprehensive Git cheat sheet covers every command you need.
Getting Started
Check Git Version
git --version
Configure Git
# Set your name and email
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# Set default branch name to 'main'
git config --global init.defaultBranch main
# Set default editor
git config --global core.editor "code --wait"
# View all settings
git config --list
# View specific setting
git config user.name
Creating & Cloning Repositories
Initialize New Repository
# Create new repo in current directory
git init
# Create new repo in specific directory
git init project-name
Clone Existing Repository
# Clone repo
git clone https://github.com/user/repo.git
# Clone to specific folder
git clone https://github.com/user/repo.git my-folder
# Clone specific branch
git clone -b branch-name https://github.com/user/repo.git
# Shallow clone (faster, less history)
git clone --depth 1 https://github.com/user/repo.git
Basic Workflow Commands
Check Status
# Show status
git status
# Short status
git status -s
Stage Changes
# Add specific file
git add filename.txt
# Add all files in directory
git add .
# Add all modified files (not new files)
git add -u
# Add files interactively
git add -p
# Add all files with specific extension
git add *.js
Commit Changes
# Commit with message
git commit -m "Your commit message"
# Commit and add all modified files
git commit -am "Your message"
# Amend last commit (change message or add files)
git commit --amend
# Amend without changing message
git commit --amend --no-edit
View Changes
# Show unstaged changes
git diff
# Show staged changes
git diff --staged
# Show changes in specific file
git diff filename.txt
# Compare branches
git diff branch1..branch2
Branching & Merging
Branch Operations
# List branches
git branch
# List all branches (including remote)
git branch -a
# Create new branch
git branch feature-name
# Switch to branch
git checkout feature-name
# Create and switch to new branch
git checkout -b feature-name
# Modern alternative (Git 2.23+)
git switch feature-name
git switch -c feature-name
# Delete branch
git branch -d feature-name
# Force delete branch
git branch -D feature-name
# Rename current branch
git branch -m new-name
Merging
# Merge branch into current branch
git merge feature-name
# Merge without fast-forward (always create merge commit)
git merge --no-ff feature-name
# Abort merge if conflicts occur
git merge --abort
# Continue merge after resolving conflicts
git merge --continue
Rebasing
# Rebase current branch onto main
git rebase main
# Interactive rebase (rewrite history)
git rebase -i HEAD~3
# Abort rebase
git rebase --abort
# Continue after resolving conflicts
git rebase --continue
Remote Repositories
Remote Operations
# List remotes
git remote -v
# Add remote
git remote add origin https://github.com/user/repo.git
# Change remote URL
git remote set-url origin https://github.com/user/new-repo.git
# Remove remote
git remote remove origin
# Rename remote
git remote rename origin upstream
Fetching & Pulling
# Fetch from remote (doesn't merge)
git fetch origin
# Fetch all remotes
git fetch --all
# Pull (fetch + merge)
git pull origin main
# Pull with rebase instead of merge
git pull --rebase origin main
# Pull and prune deleted remote branches
git pull --prune
Pushing
# Push to remote
git push origin main
# Push new branch and set upstream
git push -u origin feature-name
# Push all branches
git push --all
# Push tags
git push --tags
# Force push (dangerous!)
git push --force
# Safer force push (fails if remote has changes)
git push --force-with-lease
# Delete remote branch
git push origin --delete branch-name
Viewing History
Log Commands
# Show commit history
git log
# Compact one-line log
git log --oneline
# Show graph
git log --graph --oneline --all
# Show last N commits
git log -n 5
# Show commits by author
git log --author="Name"
# Show commits in date range
git log --since="2 weeks ago" --until="yesterday"
# Show commits affecting specific file
git log -- filename.txt
# Show commit stats (files changed)
git log --stat
# Show actual changes
git log -p
Pretty Logs
# Beautiful log format
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
# Create alias for pretty log
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
# Now use: git lg
Show Commits
# Show commit details
git show commit-hash
# Show specific file from commit
git show commit-hash:path/to/file.txt
Undoing Changes
Unstage Files
# Unstage file
git reset filename.txt
# Unstage all files
git reset
Discard Changes
# Discard changes in file (dangerous!)
git checkout -- filename.txt
# Modern alternative
git restore filename.txt
# Discard all changes
git checkout -- .
git restore .
Reset Commits
# Reset to previous commit, keep changes staged
git reset --soft HEAD~1
# Reset to previous commit, keep changes unstaged
git reset HEAD~1
git reset --mixed HEAD~1 # same as above
# Reset to previous commit, discard changes (dangerous!)
git reset --hard HEAD~1
# Reset to specific commit
git reset --hard commit-hash
Revert Commits
# Create new commit that undoes changes
git revert commit-hash
# Revert without creating commit
git revert -n commit-hash
# Revert range of commits
git revert HEAD~3..HEAD
Stashing
# Stash changes
git stash
# Stash with message
git stash save "Work in progress"
# List stashes
git stash list
# Apply last stash (keeps in stash list)
git stash apply
# Apply specific stash
git stash apply stash@{2}
# Apply and remove from stash list
git stash pop
# Show stash contents
git stash show -p stash@{0}
# Delete stash
git stash drop stash@{0}
# Clear all stashes
git stash clear
Tags
# List tags
git tag
# Create lightweight tag
git tag v1.0.0
# Create annotated tag (recommended)
git tag -a v1.0.0 -m "Version 1.0.0"
# Tag specific commit
git tag v1.0.0 commit-hash
# Push tag to remote
git push origin v1.0.0
# Push all tags
git push --tags
# Delete local tag
git tag -d v1.0.0
# Delete remote tag
git push origin --delete v1.0.0
# Checkout tag
git checkout v1.0.0
Advanced Commands
Cherry-Pick
# Apply specific commit to current branch
git cherry-pick commit-hash
# Cherry-pick range
git cherry-pick start-hash..end-hash
# Cherry-pick without committing
git cherry-pick -n commit-hash
Bisect (Find Bug)
# Start bisect
git bisect start
# Mark current commit as bad
git bisect bad
# Mark working commit as good
git bisect good commit-hash
# Git will checkout middle commit, test it, then:
git bisect good # or
git bisect bad
# Reset when done
git bisect reset
Clean Untracked Files
# Show what would be deleted
git clean -n
# Delete untracked files
git clean -f
# Delete untracked files and directories
git clean -fd
# Delete ignored files too
git clean -fX
Reflog (Recovery)
# Show reference log (even deleted commits)
git reflog
# Recover deleted commit
git checkout commit-hash
# Recover deleted branch
git branch recovered-branch commit-hash
Git Workflows
Feature Branch Workflow
# 1. Update main
git checkout main
git pull origin main
# 2. Create feature branch
git checkout -b feature/new-feature
# 3. Work and commit
git add .
git commit -m "Add new feature"
# 4. Push feature branch
git push -u origin feature/new-feature
# 5. Create PR on GitHub
# 6. After merge, delete branch
git checkout main
git pull origin main
git branch -d feature/new-feature
Hotfix Workflow
# 1. Create hotfix from main
git checkout main
git checkout -b hotfix/critical-bug
# 2. Fix and commit
git add .
git commit -m "Fix critical bug"
# 3. Merge to main
git checkout main
git merge hotfix/critical-bug
git push origin main
# 4. Tag release
git tag -a v1.0.1 -m "Hotfix release"
git push --tags
# 5. Clean up
git branch -d hotfix/critical-bug
Useful Aliases
# Set aliases
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual 'log --graph --oneline --all'
# Now use:
git st
git co main
git br
git ci -m "message"
Git Best Practices
1. Write Good Commit Messages
❌ Bad:
git commit -m "fix"
git commit -m "update code"
✅ Good:
git commit -m "Fix user login timeout issue"
git commit -m "Add email validation to signup form"
Format:
Short summary (50 chars or less)
More detailed explanation if needed. Wrap at 72 characters.
Explain what and why, not how.
- Bullet points are okay
- Use imperative mood: "Fix bug" not "Fixed bug"
2. Commit Often
Make small, logical commits. Easier to review, revert, and understand.
3. Don't Commit Generated Files
Use .gitignore for:
- Dependencies (
node_modules/) - Build outputs (
dist/,build/) - Environment files (
.env) - IDE files (
.vscode/,.idea/) - OS files (
.DS_Store,Thumbs.db)
4. Pull Before Push
# Always pull latest changes first
git pull origin main
git push origin feature-branch
5. Use Branches
Never commit directly to main. Use feature branches.
6. Review Before Committing
# Check what you're committing
git diff --staged
git status
Common Problems & Solutions
Undo Last Commit (Not Pushed)
git reset --soft HEAD~1
Change Last Commit Message
git commit --amend -m "New message"
Accidentally Committed to Wrong Branch
# On wrong branch
git log # copy commit hash
# Switch to correct branch
git checkout correct-branch
git cherry-pick commit-hash
# Go back and remove from wrong branch
git checkout wrong-branch
git reset --hard HEAD~1
Merge Conflicts
# 1. Check conflicted files
git status
# 2. Edit files, remove conflict markers
# 3. Stage resolved files
git add resolved-file.txt
# 4. Complete merge
git commit
Recover Deleted Branch
git reflog
git checkout -b recovered-branch commit-hash
Learning Resources
Conclusion
Git is powerful but has a learning curve. Master the basics (add, commit, push, pull, branch), then gradually learn advanced commands as you need them.
Keep this cheat sheet bookmarked for quick reference. The more you use Git, the more natural these commands become. When in doubt, git status is your friend.
Master More Developer Tools
Check out our Best Developer Tools 2026 guide for essential tools every developer needs, including Git clients, editors, and productivity boosters.