Git
Updated: September 16, 2025Categories: Git
Printed from:
Complete Git Cheatsheet
Table of Contents
- Git Configuration
- Repository Creation & Cloning
- Basic Workflow
- Branching & Merging
- Remote Repositories
- Git Tree & History
- Undoing Changes
- Stashing
- Tags
- Advanced Commands
- Git Aliases
- Troubleshooting
Git Configuration
Initial Setup
Bash
1234567891011121314# Set global username and email
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# Set default branch name
git config --global init.defaultBranch main
# Set default editor
git config --global core.editor "code --wait" # VS Code
git config --global core.editor "vim" # Vim
# Enable colored output
git config --global color.ui auto
View Configuration
Bash
12345678910# List all configuration
git config --list
# View specific config
git config user.name
git config user.email
# View config with location
git config --list --show-origin
Repository Creation & Cloning
Create New Repository
Bash
1234567# Initialize new repository
git init
git init <directory-name>
# Initialize with specific branch
git init --initial-branch=main
Clone Repository
Bash
12345678910# Clone repository
git clone <url>
git clone <url> <directory-name>
# Clone specific branch
git clone -b <branch-name> <url>
# Shallow clone (limited history)
git clone --depth 1 <url>
Basic Workflow
Check Status & Add Files
Bash
1234567891011# Check repository status
git status
git status -s # Short format
# Add files to staging area
git add <file>
git add . # Add all files
git add *.js # Add all JS files
git add -A # Add all files (including deleted)
git add -u # Add only modified/deleted files
Commit Changes
Bash
12345678910111213# Commit staged changes
git commit -m "Commit message"
# Commit with detailed message
git commit -m "Title" -m "Description"
# Add and commit in one step
git commit -am "Commit message"
# Amend last commit
git commit --amend
git commit --amend -m "New message"
View Changes
Bash
12345678910# Show differences
git diff # Working directory vs staging
git diff --staged # Staging vs last commit
git diff HEAD # Working directory vs last commit
git diff <commit1> <commit2> # Between commits
# Show changes in specific file
git diff <file>
git diff --staged <file>
Branching & Merging
Branch Operations
Bash
12345678910111213141516171819202122# List branches
git branch # Local branches
git branch -r # Remote branches
git branch -a # All branches
# Create branch
git branch <branch-name>
git branch <branch-name> <commit-hash>
# Switch branches
git checkout <branch-name>
git switch <branch-name> # Git 2.23+
# Create and switch to new branch
git checkout -b <branch-name>
git switch -c <branch-name> # Git 2.23+
# Delete branch
git branch -d <branch-name> # Safe delete
git branch -D <branch-name> # Force delete
git push origin --delete <branch-name> # Delete remote branch
Merging
Bash
123456789101112# Merge branch into current branch
git merge <branch-name>
# Merge with no fast-forward
git merge --no-ff <branch-name>
# Squash merge
git merge --squash <branch-name>
# Abort merge
git merge --abort
Rebasing
Bash
1234567891011# Rebase current branch onto another
git rebase <branch-name>
# Interactive rebase
git rebase -i <commit-hash>
git rebase -i HEAD~3 # Last 3 commits
# Continue/abort rebase
git rebase --continue
git rebase --abort
Remote Repositories
Remote Management
Bash
1234567891011121314151617# List remotes
git remote
git remote -v # Verbose (shows URLs)
# Add remote
git remote add <name> <url>
git remote add origin <url>
# Remove remote
git remote remove <name>
# Rename remote
git remote rename <old-name> <new-name>
# Change remote URL
git remote set-url <name> <new-url>
Push & Pull
Bash
1234567891011121314151617# Push to remote
git push <remote> <branch>
git push origin main
git push -u origin main # Set upstream
# Push all branches
git push --all origin
# Pull from remote
git pull # Fetch and merge
git pull <remote> <branch>
# Fetch without merging
git fetch
git fetch <remote>
git fetch --all
Git Tree & History
Viewing Commit History
Bash
123456789101112131415# Basic log
git log
git log --oneline # Condensed format
git log --graph # Show branch graph
git log --all # All branches
# Pretty formats
git log --pretty=format:"%h - %an, %ar : %s"
git log --pretty=oneline
git log --pretty=short
git log --pretty=full
# Custom format
git log --pretty=format:"%C(yellow)%h%C(reset) - %C(green)%an%C(reset), %C(blue)%ar%C(reset) : %s"
Git Tree Visualization
Bash
1234567891011121314# Tree view with graph
git log --graph --pretty=oneline --abbrev-commit
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --all
# Compact tree view
git log --oneline --graph --all
# Show branch structure
git show-branch
git show-branch --all
# Alternative tree visualization
git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
Filtering History
Bash
123456789101112131415161718# Filter by author
git log --author="John Doe"
# Filter by date
git log --since="2023-01-01"
git log --until="2023-12-31"
git log --since="2 weeks ago"
# Filter by commit message
git log --grep="bug fix"
# Filter by file
git log <file>
git log -p <file> # Show changes
# Show commits that changed specific lines
git log -L <start>,<end>:<file>
Show Specific Commits
Bash
123456789101112# Show commit details
git show <commit-hash>
git show HEAD
git show HEAD~1 # Previous commit
# Show only files changed
git show --name-only <commit-hash>
# Show tree structure of a commit
git ls-tree <commit-hash>
git ls-tree -r <commit-hash> # Recursive
Undoing Changes
Working Directory
Bash
12345678# Discard changes in working directory
git checkout -- <file>
git restore <file> # Git 2.23+
# Discard all changes
git checkout -- .
git restore .
Staging Area
Bash
12345678# Unstage files
git reset HEAD <file>
git restore --staged <file> # Git 2.23+
# Unstage all files
git reset HEAD
git restore --staged .
Commits
Bash
1234567891011121314151617# Soft reset (keep changes in working directory)
git reset --soft HEAD~1
# Mixed reset (keep changes in working directory, unstaged)
git reset HEAD~1
git reset --mixed HEAD~1
# Hard reset (discard all changes)
git reset --hard HEAD~1
# Reset to specific commit
git reset --hard <commit-hash>
# Revert commit (create new commit that undoes changes)
git revert <commit-hash>
git revert HEAD
Stashing
Basic Stashing
Bash
12345678910111213141516171819202122# Stash changes
git stash
git stash push -m "Work in progress"
# List stashes
git stash list
# Apply stash
git stash apply # Apply latest stash
git stash apply stash@{1} # Apply specific stash
# Pop stash (apply and remove)
git stash pop
git stash pop stash@{1}
# Drop stash
git stash drop
git stash drop stash@{1}
# Clear all stashes
git stash clear
Advanced Stashing
Bash
123456789101112# Stash including untracked files
git stash -u
# Stash including ignored files
git stash -a
# Stash specific files
git stash push <file1> <file2>
# Create branch from stash
git stash branch <branch-name>
Tags
Creating Tags
Bash
1234567891011# Lightweight tag
git tag <tag-name>
git tag v1.0.0
# Annotated tag
git tag -a <tag-name> -m "Tag message"
git tag -a v1.0.0 -m "Release version 1.0.0"
# Tag specific commit
git tag <tag-name> <commit-hash>
Managing Tags
Bash
123456789101112131415# List tags
git tag
git tag -l "v1.*" # Pattern matching
# Show tag details
git show <tag-name>
# Delete tag
git tag -d <tag-name> # Local
git push origin --delete <tag-name> # Remote
# Push tags
git push origin <tag-name> # Single tag
git push origin --tags # All tags
Advanced Commands
Cherry Pick
Bash
123456789# Apply commit to current branch
git cherry-pick <commit-hash>
# Cherry pick multiple commits
git cherry-pick <commit1> <commit2>
# Cherry pick range
git cherry-pick <start-commit>..<end-commit>
Bisect (Find Bug)
Bash
123456789101112# Start bisecting
git bisect start
git bisect bad # Current commit is bad
git bisect good <commit> # Known good commit
# Continue bisecting
git bisect good # Current commit is good
git bisect bad # Current commit is bad
# End bisecting
git bisect reset
Submodules
Bash
123456789101112# Add submodule
git submodule add <repository-url> <path>
# Initialize submodules
git submodule init
# Update submodules
git submodule update
# Clone repository with submodules
git clone --recursive <url>
Worktree
Bash
123456789# Add worktree
git worktree add <path> <branch>
# List worktrees
git worktree list
# Remove worktree
git worktree remove <path>
Git Aliases
Useful Aliases
Bash
12345678910111213# Set up common aliases
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.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual '!gitk'
# Advanced aliases
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
git config --global alias.tree "log --graph --pretty=oneline --abbrev-commit --all"
Troubleshooting
Common Issues
Bash
123456789101112131415161718192021222324# Check repository integrity
git fsck
# Garbage collection
git gc
# Clean untracked files
git clean -f # Files
git clean -fd # Files and directories
git clean -n # Dry run
# Resolve merge conflicts
# 1. Edit conflicted files
# 2. git add <resolved-files>
# 3. git commit
# Find lost commits
git reflog
git reflog show <branch>
# Recover lost commits
git checkout <commit-hash>
git branch <new-branch-name> <commit-hash>
Configuration Issues
Bash
12345678910# Fix line ending issues
git config --global core.autocrlf true # Windows
git config --global core.autocrlf input # Linux/Mac
# Ignore file permissions
git config core.fileMode false
# Set default merge tool
git config --global merge.tool vimdiff
Performance
Bash
123456789101112# Pack references
git pack-refs --all
# Optimize repository
git repack -ad
# Prune remote branches
git remote prune origin
# Check repository size
git count-objects -vH
Quick Reference Commands
Daily Workflow
Bash
123456git status # Check status
git add . # Stage all changes
git commit -m "message" # Commit changes
git push # Push to remote
git pull # Pull from remote
Branch Workflow
Bash
12345git branch feature-branch # Create branch
git checkout feature-branch # Switch to branch
git merge feature-branch # Merge branch
git branch -d feature-branch # Delete branch
Emergency Commands
Bash
12345git stash # Quick save
git reset --hard HEAD # Undo all changes
git clean -fd # Remove untracked files
git reflog # Find lost commits
Note: Replace <placeholder> values with actual names, URLs, or commit hashes as needed.
Continue Learning
Discover more cheatsheets to boost your productivity