Last updated
Git Cheat Sheet Examples
The Git Cheat Sheet on TechConverter.me provides a comprehensive, searchable reference for all essential Git commands organized by workflow and use case. Below are the most commonly needed commands with explanations and practical examples.
Setup & Configuration
# Set your identity (required before first commit)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Set default branch name
git config --global init.defaultBranch main
# Set preferred editor
git config --global core.editor "code --wait" # VS Code
git config --global core.editor "vim" # Vim
# View all config settings
git config --list
# Useful aliases
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 --decorate --all"
Starting a Repository
# Initialize a new local repository
git init
# Clone an existing repository
git clone https://github.com/user/repo.git
# Clone into a specific folder name
git clone https://github.com/user/repo.git my-project
# Clone a specific branch
git clone -b develop https://github.com/user/repo.git
Daily Workflow Commands
# Check working directory status
git status
# Stage a specific file
git add filename.txt
# Stage all changes
git add .
# Stage parts of a file interactively
git add -p filename.txt
# Commit staged changes
git commit -m "feat: add user authentication"
# Stage and commit tracked files in one step
git commit -am "fix: correct login redirect URL"
# Amend the last commit (before pushing)
git commit --amend -m "fix: correct login redirect URL"
Branching
# List all local branches
git branch
# List all branches (local + remote)
git branch -a
# Create a new branch
git branch feature/add-search
# Switch to a branch
git switch feature/add-search # modern syntax
git checkout feature/add-search # classic syntax
# Create and switch in one step
git switch -c feature/add-search
git checkout -b feature/add-search
# Rename current branch
git branch -m new-branch-name
# Delete a merged branch
git branch -d feature/add-search
# Force delete an unmerged branch
git branch -D feature/add-search
Merging & Rebasing
# Merge a branch into current branch
git merge feature/add-search
# Merge without fast-forward (always creates merge commit)
git merge --no-ff feature/add-search
# Rebase current branch onto main
git rebase main
# Interactive rebase — edit last 3 commits
git rebase -i HEAD~3
# Interactive rebase options:
# pick → keep commit as-is
# reword → keep commit, edit message
# squash → combine with previous commit
# fixup → combine with previous, discard message
# drop → remove commit entirely
# Abort a rebase in progress
git rebase --abort
# Continue after resolving conflicts
git rebase --continue
Undoing Changes
# Discard changes in working directory (unstaged)
git restore filename.txt
git checkout -- filename.txt # classic syntax
# Unstage a file (keep changes in working directory)
git restore --staged filename.txt
git reset HEAD filename.txt # classic syntax
# Undo last commit, keep changes staged
git reset --soft HEAD~1
# Undo last commit, keep changes unstaged
git reset --mixed HEAD~1
# Undo last commit, discard all changes (DESTRUCTIVE)
git reset --hard HEAD~1
# Safely undo a commit by creating a new revert commit
git revert abc1234
# Revert a merge commit
git revert -m 1 abc1234
Stashing
# Save current changes to stash
git stash
# Stash with a descriptive message
git stash push -m "WIP: half-done login form"
# List all stashes
git stash list
# Apply most recent stash (keeps it in stash list)
git stash apply
# Apply and remove most recent stash
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
# Show commit log
git log
# Compact one-line log
git log --oneline
# Visual branch graph
git log --oneline --graph --decorate --all
# Show commits by a specific author
git log --author="Alice"
# Show commits in a date range
git log --after="2025-01-01" --before="2025-03-01"
# Show changes introduced by each commit
git log -p
# Show last N commits
git log -5
# Show who last changed each line of a file
git blame filename.txt
# Find the commit that introduced a bug
git bisect start
git bisect bad # current commit is broken
git bisect good abc1234 # this commit was working
# Git checks out midpoint — test and mark good/bad
git bisect good # or: git bisect bad
git bisect reset # when done
Remote Repositories
# List remote connections
git remote -v
# Add a remote
git remote add origin https://github.com/user/repo.git
# Rename a remote
git remote rename origin upstream
# Remove a remote
git remote remove upstream
# Fetch changes without merging
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
# Push to remote
git push origin feature/add-search
# Push and set upstream tracking
git push -u origin feature/add-search
# Force push (use with caution — rewrites remote history)
git push --force-with-lease origin feature/add-search
# Delete a remote branch
git push origin --delete feature/old-branch
Cherry-Pick & Advanced Operations
# Apply a specific commit to current branch
git cherry-pick abc1234
# Cherry-pick without committing (stage only)
git cherry-pick --no-commit abc1234
# Show all HEAD movements (useful for recovering lost commits)
git reflog
# Recover a deleted branch using reflog
git checkout -b recovered-branch abc1234
# Create a tag
git tag v1.0.0
git tag -a v1.0.0 -m "Release version 1.0.0"
# Push tags to remote
git push origin --tags
Diff Commands
# Show unstaged changes
git diff
# Show staged changes
git diff --staged
# Compare two branches
git diff main..feature/add-search
# Compare specific files between branches
git diff main..feature/add-search -- src/auth.js
# Show summary of changes (files changed, insertions, deletions)
git diff --stat main..feature/add-search
Quick Reference: Common Workflows
- Start a feature:
git switch -c feature/my-feature - Save work in progress:
git stash push -m "WIP description" - Clean up commits before PR:
git rebase -i HEAD~N - Sync with main:
git fetch origin && git rebase origin/main - Undo last commit safely:
git reset --soft HEAD~1 - Find a bug's origin:
git bisect start - Recover lost work:
git reflog