Essential commands for Git version control.
Initial Setup
Configuration
# Set username
git config --global user.name "John Doe"
# Set email
git config --global user.email "john@example.com"
# Set default editor
git config --global core.editor vim
# Set default branch name
git config --global init.defaultBranch main
# List all settings
git config --list
# Show specific setting
git config user.name
# Edit config file
git config --global --edit
Repository Creation
# Initialize new repository
git init
# Clone existing repository
git clone https://github.com/user/repo.git
# Clone to specific directory
git clone https://github.com/user/repo.git myproject
# Clone specific branch
git clone -b develop https://github.com/user/repo.git
# Shallow clone (recent history only)
git clone --depth 1 https://github.com/user/repo.git
Basic Commands
Status and Information
# Show working tree status
git status
# Short status
git status -s
# Show commit history
git log
# One line per commit
git log --oneline
# Show last N commits
git log -5
# Show commits with file changes
git log --stat
# Show commits with diff
git log -p
# Show graph
git log --graph --oneline --all
# Show commits by author
git log --author="John"
# Show commits since date
git log --since="2 weeks ago"
# Show commits with search
git log --grep="bug fix"
# Show who changed what in file
git blame filename.txt
# Show changes between commits
git diff commit1 commit2
Staging and Committing
# Add file to staging
git add filename.txt
# Add all files
git add .
# Add all modified files (not new)
git add -u
# Interactive staging
git add -i
# Add parts of file
git add -p
# Commit staged changes
git commit -m "Commit message"
# Add and commit in one step
git commit -am "Message"
# Amend last commit
git commit --amend
# Amend without changing message
git commit --amend --no-edit
# Empty commit (useful for CI)
git commit --allow-empty -m "Trigger build"
Undoing Changes
# Discard changes in working directory
git checkout -- filename.txt
# Unstage file (keep changes)
git reset HEAD filename.txt
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Undo last commit (discard changes)
git reset --hard HEAD~1
# Revert specific commit (creates new commit)
git revert commit-hash
# Remove file from staging and working dir
git rm filename.txt
# Remove file from repo but keep locally
git rm --cached filename.txt
# Restore file from specific commit
git checkout commit-hash -- filename.txt
Branching and Merging
Branch Management
# List local branches
git branch
# List all branches (local and remote)
git branch -a
# List remote branches
git branch -r
# Create new branch
git branch feature-branch
# Create and switch to branch
git checkout -b feature-branch
# Switch branches
git checkout main
# Modern switch command
git switch main
# Create and switch (modern)
git switch -c feature-branch
# Rename current branch
git branch -m new-name
# Rename specific branch
git branch -m old-name new-name
# Delete local branch
git branch -d feature-branch
# Force delete branch
git branch -D feature-branch
# Delete remote branch
git push origin --delete feature-branch
Merging
# Merge branch into current branch
git merge feature-branch
# Merge with commit even if fast-forward
git merge --no-ff feature-branch
# Abort merge in progress
git merge --abort
# Show merge conflicts
git diff --name-only --diff-filter=U
# Mark conflict as resolved
git add conflicted-file.txt
Rebasing
# Rebase current branch onto main
git rebase main
# Interactive rebase (last 3 commits)
git rebase -i HEAD~3
# Continue rebase after resolving conflicts
git rebase --continue
# Skip current commit during rebase
git rebase --skip
# Abort rebase
git rebase --abort
# Rebase and autosquash
git rebase -i --autosquash main
Remote Repositories
Remote Management
# Show remote repositories
git remote -v
# Add remote repository
git remote add origin https://github.com/user/repo.git
# Change remote URL
git remote set-url origin https://github.com/user/newrepo.git
# Remove remote
git remote remove origin
# Rename remote
git remote rename origin upstream
# Show remote details
git remote show origin
Fetching and Pulling
# Fetch from remote
git fetch origin
# Fetch all remotes
git fetch --all
# Fetch and prune deleted branches
git fetch --prune
# Pull from remote (fetch + merge)
git pull origin main
# Pull with rebase
git pull --rebase origin main
# Pull all branches
git pull --all
Pushing
# Push to remote
git push origin main
# Push and set upstream
git push -u origin feature-branch
# Push all branches
git push --all
# Push tags
git push --tags
# Force push (dangerous!)
git push --force
# Safer force push
git push --force-with-lease
# Delete remote branch
git push origin --delete feature-branch
Stashing
Stash Commands
# Stash changes
git stash
# Stash with message
git stash save "WIP: feature implementation"
# List stashes
git stash list
# Apply most recent stash
git stash apply
# Apply specific stash
git stash apply stash@{2}
# Apply and remove stash
git stash pop
# Show stash contents
git stash show
# Show stash diff
git stash show -p
# Drop specific stash
git stash drop stash@{0}
# Clear all stashes
git stash clear
# Stash including untracked files
git stash -u
# Stash including ignored files
git stash -a
Tags
Tag Management
# List tags
git tag
# Create lightweight tag
git tag v1.0.0
# Create annotated tag
git tag -a v1.0.0 -m "Version 1.0.0"
# Tag specific commit
git tag v1.0.0 commit-hash
# Show tag details
git show v1.0.0
# 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
Inspection and Comparison
Diff Commands
# Show unstaged changes
git diff
# Show staged changes
git diff --staged
# Show changes between branches
git diff main feature-branch
# Show changes in specific file
git diff filename.txt
# Show word-level diff
git diff --word-diff
# Show summary of changes
git diff --stat
# Compare with remote
git diff origin/main
Show Command
# Show commit details
git show commit-hash
# Show file from commit
git show commit-hash:path/to/file
# Show commit stats
git show --stat commit-hash
History Rewriting
Interactive Rebase Actions
# Reword commit message
git rebase -i HEAD~3
# Change 'pick' to 'reword'
# Squash commits
# Change 'pick' to 'squash' or 's'
# Edit commit
# Change 'pick' to 'edit'
# Drop commit
# Change 'pick' to 'drop' or delete line
# Reorder commits
# Reorder lines in rebase file
Reset and Clean
# Reset to specific commit (keep changes)
git reset commit-hash
# Reset and discard changes
git reset --hard commit-hash
# Clean untracked files
git clean -n # dry run
git clean -f # force
# Clean directories too
git clean -fd
# Clean ignored files
git clean -fx
Advanced Commands
Cherry Pick
# Apply specific commit to current branch
git cherry-pick commit-hash
# Cherry pick multiple commits
git cherry-pick commit1 commit2
# Cherry pick without committing
git cherry-pick -n commit-hash
# Abort cherry pick
git cherry-pick --abort
# Continue after resolving conflicts
git cherry-pick --continue
Submodules
# Add submodule
git submodule add https://github.com/user/repo.git path/to/submodule
# Initialize submodules
git submodule init
# Update submodules
git submodule update
# Clone with submodules
git clone --recursive https://github.com/user/repo.git
# Update all submodules to latest
git submodule update --remote
# Remove submodule
git submodule deinit path/to/submodule
git rm path/to/submodule
Bisect (Binary Search)
# Start bisect
git bisect start
# Mark current commit as bad
git bisect bad
# Mark specific commit as good
git bisect good commit-hash
# Git will checkout middle commit for testing
# After testing, mark as good or bad
git bisect good
# or
git bisect bad
# Reset after finding bad commit
git bisect reset
Worktrees
# Create new worktree
git worktree add ../project-feature feature-branch
# List worktrees
git worktree list
# Remove worktree
git worktree remove ../project-feature
# Prune deleted worktrees
git worktree prune
Searching
Search Commands
# Search in files
git grep "search term"
# Search with line numbers
git grep -n "search term"
# Search in specific commit
git grep "search term" commit-hash
# Count matches
git grep -c "search term"
# Search with context
git grep -C 3 "search term"
# Search commit messages
git log --grep="search term"
# Search code changes
git log -S "function_name"
Maintenance
Repository Maintenance
# Optimize repository
git gc
# Aggressive optimization
git gc --aggressive
# Verify repository integrity
git fsck
# Count objects
git count-objects -v
# Show repository size
git count-objects -vH
# Prune unreachable objects
git prune
# Remove files from history
git filter-branch --tree-filter 'rm -f passwords.txt' HEAD
Reflog
# Show reference log
git reflog
# Show reflog for branch
git reflog show main
# Recover lost commit
git checkout commit-hash
# Undo reset using reflog
git reset --hard HEAD@{1}
Aliases
Common Aliases
# Set up useful 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 'log --graph --oneline --all'
git config --global alias.amend 'commit --amend --no-edit'
Best Practices
Workflow Tips
# Always pull before push
git pull --rebase origin main
# Create feature branch from updated main
git checkout main
git pull
git checkout -b feature-branch
# Commit often with clear messages
git commit -m "Add user authentication"
# Keep commits atomic (one logical change)
# Use descriptive branch names
# feature/user-login
# bugfix/header-spacing
# hotfix/security-patch
# Before merging feature
git checkout main
git pull
git checkout feature-branch
git rebase main
git checkout main
git merge feature-branch
.gitignore Examples
# Python
__pycache__/
*.py[cod]
*.so
.env
venv/
# Node.js
node_modules/
npm-debug.log
.env
# IDE
.vscode/
.idea/
*.swp
# OS
.DS_Store
Thumbs.db
# Build
dist/
build/
*.log
Emergency Commands
Oh Shit, Git!
# Undo last commit but keep changes
git reset --soft HEAD~1
# Discard all local changes
git reset --hard HEAD
# I committed to wrong branch
git checkout correct-branch
git cherry-pick commit-hash
# I need to undo a public commit
git revert commit-hash
# I accidentally committed secrets
# Remove from history (careful!)
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch path/to/secret' \
--prune-empty --tag-name-filter cat -- --all
# Then force push (coordinate with team!)
git push --force --all