Git - Command Reference H-R
Reference
Complete reference for Git commands H-R with syntax, options, examples, and practical use cases. This comprehensive guide covers all essential Git commands alphabetically from 'hash-object' to 'revert'.
hash-object - Create Git Objects
# Create blob object from file
git hash-object file.txt
git hash-object -w file.txt # Write to object database
# Create object from stdin
echo "Hello World" | git hash-object --stdin
echo "Hello World" | git hash-object -w --stdin
# Specify object type
git hash-object -t blob file.txt
# Example output
$ echo "Hello World" | git hash-object --stdin
5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689
help - Get Help Information
# Get help for commands
git help
git help add # Detailed help for 'add'
git help -a # List all commands
git help -g # List concept guides
# Quick help
git add -h # Short usage summary
git commit --help # Same as 'git help commit'
# Web help (opens in browser)
git help -w add
init - Initialize Repository
# Initialize repository
git init # Current directory
git init my-project # New directory
git init --bare # Bare repository (no working directory)
# Initialize with specific branch name
git init -b main
git init --initial-branch=main
# Initialize with template
git init --template=/path/to/template
# Reinitialize existing repository
git init # Safe to run in existing repo
log - Show Commit History
# Basic log
git log
git log --oneline # Compact format
git log -5 # Last 5 commits
git log --graph # ASCII graph
# Formatting
git log --pretty=format:"%h %s"
git log --pretty=oneline
git log --pretty=short
git log --pretty=full
# Filter by author/date
git log --author="John Doe"
git log --since="2 weeks ago"
git log --until="2023-01-01"
git log --grep="bug fix"
# File history
git log -- file.txt # Commits affecting file
git log --follow file.txt # Follow renames
git log -p file.txt # Show patches
# Branch specific
git log main..feature # Commits in feature not in main
git log --all # All branches
git log --branches # All local branches
# Advanced options
git log --stat # Show file statistics
git log --shortstat # Condensed stats
git log --name-only # File names only
git log --name-status # File names with status
# Merge commits
git log --merges # Merge commits only
git log --no-merges # Exclude merge commits
git log --first-parent # Follow first parent only
# Example pretty format
$ git log --pretty=format:"%h - %an, %ar : %s"
ca82a6d - Scott Chacon, 6 years ago : changed the version number
085bb3b - Scott Chacon, 6 years ago : removed unnecessary test code
ls-files - List Files in Index
# List tracked files
git ls-files
git ls-files -s # Show staging info
git ls-files -c # Show cached files
# Show different file states
git ls-files -d # Deleted files
git ls-files -m # Modified files
git ls-files -o # Other (untracked) files
git ls-files -u # Unmerged files
# Ignore patterns
git ls-files --exclude-standard # Apply .gitignore rules
git ls-files --others --ignored --exclude-standard
# Filter by patterns
git ls-files "*.js"
git ls-files --directory
ls-remote - List Remote References
# List remote references
git ls-remote origin
git ls-remote --heads origin # Branches only
git ls-remote --tags origin # Tags only
# Check specific refs
git ls-remote origin main
git ls-remote origin refs/heads/main
# List all remotes
git ls-remote --exit-code origin # Exit code indicates if remote exists
# Example output
$ git ls-remote origin
5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c0d1e2f3a4b HEAD
5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c0d1e2f3a4b refs/heads/main
3c4d5e6f7a8b9c0d1e2f3a4b5a6b7c8d9e0f1a2b refs/heads/develop
ls-tree - List Tree Objects
# List tree contents
git ls-tree HEAD
git ls-tree -r HEAD # Recursive
git ls-tree --name-only HEAD # Names only
# List specific tree
git ls-tree HEAD~1
git ls-tree v1.0
# Show specific directory
git ls-tree HEAD src/
merge - Join Development Histories
# Basic merge
git merge feature-branch
git merge origin/main
# Merge strategies
git merge --no-ff feature-branch # Always create merge commit
git merge --ff-only feature-branch # Fast-forward only
git merge --squash feature-branch # Squash commits
# Merge with message
git merge -m "Merge feature" feature-branch
# Merge strategies (advanced)
git merge -s recursive feature-branch
git merge -s ours feature-branch # Ignore their changes
git merge -s octopus branch1 branch2 branch3
# Strategy options
git merge -X ours feature-branch # Prefer our changes in conflicts
git merge -X theirs feature-branch # Prefer their changes
# Abort merge
git merge --abort
# Continue merge after resolving conflicts
git merge --continue
merge-base - Find Common Ancestor
# Find merge base
git merge-base main feature
git merge-base --all main feature # All merge bases
# Check if ancestor
git merge-base --is-ancestor main feature
echo $? # 0 if main is ancestor of feature
# Fork point
git merge-base --fork-point main feature
mergetool - Run Merge Conflict Resolution Tools
# Launch merge tool
git mergetool
git mergetool file.txt # Specific file
# Use specific tool
git mergetool --tool=vimdiff
git mergetool -t kdiff3
# Configure merge tool
git config merge.tool vimdiff
git config mergetool.vimdiff.cmd 'vimdiff "$LOCAL" "$MERGED" "$REMOTE"'
mv - Move/Rename Files
# Rename file
git mv old-name.txt new-name.txt
# Move file to directory
git mv file.txt directory/
# Move directory
git mv old-dir/ new-dir/
# Multiple files
git mv *.txt docs/
name-rev - Find Symbolic Names for Commits
# Find name for commit
git name-rev HEAD
git name-rev abc1234
# Options
git name-rev --tags HEAD # Use tags only
git name-rev --all # All refs
git name-rev --stdin # Read from stdin
notes - Add/Inspect Object Notes
# Add note to commit
git notes add -m "Important fix" HEAD
git notes add -m "Note" abc1234
# Show notes
git notes show HEAD
git notes list
# Edit note
git notes edit HEAD
# Remove note
git notes remove HEAD
# Different note namespaces
git notes --ref=bugs add -m "Bug report" HEAD
git notes --ref=bugs show HEAD
pack-objects - Create Packed Archive
# Create pack file (low-level)
git rev-list --objects --all | git pack-objects pack
# Create pack with specific objects
echo "HEAD" | git pack-objects --stdout > pack.pack
prune - Prune Unreachable Objects
# Prune unreachable objects
git prune
git prune --dry-run # Show what would be pruned
git prune --expire=1.week.ago # Prune objects older than 1 week
# Prune with verbose output
git prune -v
pull - Fetch and Merge
# Basic pull
git pull # Default remote and branch
git pull origin main # Specific remote and branch
# Pull with rebase
git pull --rebase
git pull --rebase origin main
# Pull all remotes
git pull --all
# Pull with specific strategy
git pull --no-ff # Always create merge commit
git pull --ff-only # Fast-forward only
# Shallow pull
git pull --depth=1
# Pull with submodules
git pull --recurse-submodules
push - Update Remote References
# Basic push
git push # Default remote and branch
git push origin main # Specific remote and branch
# Push all branches
git push --all origin
git push --mirror origin # Mirror (including refs)
# Push tags
git push --tags origin
git push origin v1.0 # Specific tag
# Set upstream
git push -u origin feature # Set upstream tracking
git push --set-upstream origin feature
# Force push (dangerous)
git push --force origin main
git push --force-with-lease origin main # Safer force push
# Delete remote branch/tag
git push origin --delete feature-branch
git push origin --delete tag v1.0
git push origin :feature-branch # Alternative syntax
# Push to multiple remotes
git push origin main
git push backup main
# Dry run
git push --dry-run origin main
range-diff - Compare Commit Ranges
# Compare ranges (Git 2.19+)
git range-diff main~3..main~1 feature~3..feature~1
git range-diff main...main-new feature...feature-new
read-tree - Read Tree Objects into Index
# Read tree into index (low-level)
git read-tree HEAD
git read-tree --reset HEAD # Reset index to match tree
# Three-way read
git read-tree -m HEAD~1 HEAD~1 HEAD
rebase - Reapply Commits
# Basic rebase
git rebase main # Rebase current branch onto main
git rebase main feature # Rebase feature onto main
# Interactive rebase
git rebase -i HEAD~3 # Edit last 3 commits
git rebase -i main # Interactive rebase onto main
# Rebase onto different base
git rebase --onto main server client
# Continue/abort rebase
git rebase --continue # After resolving conflicts
git rebase --skip # Skip current commit
git rebase --abort # Abort rebase
# Rebase options
git rebase --preserve-merges main # Keep merge structure
git rebase --rebase-merges main # Recreate merge structure
git rebase --autosquash main # Auto-squash fixup commits
# Strategy options
git rebase -X ours main # Prefer our changes in conflicts
git rebase -X theirs main # Prefer their changes
reflog - Reference Logs
# Show reflog
git reflog # Current branch reflog
git reflog HEAD # HEAD reflog
git reflog main # Specific branch reflog
# Show all reflogs
git reflog --all
# Reflog with dates
git reflog --date=iso
git reflog --date=relative
# Expire reflog entries
git reflog expire --expire=30.days --all
git reflog delete HEAD@{5} # Delete specific entry
# Reflog for specific branch
git reflog show main
# Example reflog output
$ git reflog
a1b2c3d HEAD@{0}: commit: Add new feature
e4f5g6h HEAD@{1}: checkout: moving from main to feature
i7j8k9l HEAD@{2}: pull: Fast-forward
remote - Manage Remote Repositories
# List remotes
git remote # Remote names
git remote -v # Remote names with URLs
git remote show origin # Detailed remote info
# Add remote
git remote add origin https://github.com/user/repo.git
git remote add upstream https://github.com/original/repo.git
# Remove remote
git remote remove origin
git remote rm origin # Alias for remove
# Rename remote
git remote rename origin old-origin
# Change remote URL
git remote set-url origin https://new-url.git
git remote set-url --add origin https://additional-url.git
# Prune remote branches
git remote prune origin # Remove stale remote-tracking branches
git remote prune --dry-run origin
repack - Pack Repository Objects
# Repack repository
git repack
git repack -a # Include all objects
git repack -d # Remove redundant packs
# Aggressive repack
git repack -a -d --depth=250 --window=250
# Repack with progress
git repack -a -d --progress
replace - Create Replace References
# Replace object with another
git replace bad-commit good-commit
# List replacements
git replace -l
git replace --list
# Delete replacement
git replace -d bad-commit
rerere - Reuse Recorded Resolution
# Enable rerere
git config rerere.enabled true
# Status of rerere
git rerere status
git rerere diff
# Clear rerere database
git rerere clear
git rerere forget file.txt # Forget resolution for file
reset - Reset Current HEAD
# Reset modes
git reset --soft HEAD~1 # Keep changes staged
git reset --mixed HEAD~1 # Keep changes unstaged (default)
git reset --hard HEAD~1 # Discard all changes
# Reset to specific commit
git reset --hard abc1234
git reset --soft v1.0
# Reset specific files
git reset HEAD file.txt # Unstage file
git reset file.txt # Same as above
# Reset with paths
git reset HEAD~1 -- file.txt # Reset file to previous version
# Interactive reset
git reset -p # Interactively unstage hunks
restore - Restore Files
# Restore working directory (Git 2.23+)
git restore file.txt # Discard changes
git restore . # Restore all files
# Restore from specific commit
git restore --source=HEAD~1 file.txt
git restore --source=main file.txt
# Restore staged files
git restore --staged file.txt # Unstage file
git restore --staged . # Unstage all files
# Restore both working directory and staged
git restore --staged --worktree file.txt
# Interactive restore
git restore -p file.txt # Interactive hunks
rev-list - List Commit Objects
# List commits
git rev-list HEAD # All reachable commits from HEAD
git rev-list HEAD~5..HEAD # Range of commits
git rev-list --count HEAD # Count commits
# With objects
git rev-list --objects HEAD # Include trees and blobs
git rev-list --objects --all # All reachable objects
# Filtering
git rev-list --since="1 week ago" HEAD
git rev-list --author="John Doe" HEAD
git rev-list --grep="bug" HEAD
# Advanced options
git rev-list --pretty HEAD # Pretty format
git rev-list --reverse HEAD # Reverse chronological order
git rev-list --ancestry-path main..feature
rev-parse - Parse Revision Parameters
# Parse revisions
git rev-parse HEAD # Full SHA-1 of HEAD
git rev-parse --short HEAD # Abbreviated SHA-1
git rev-parse main # Full SHA-1 of main
# Repository paths
git rev-parse --git-dir # .git directory path
git rev-parse --show-toplevel # Repository root
git rev-parse --show-prefix # Current directory relative to root
# Branch information
git rev-parse --abbrev-ref HEAD # Current branch name
git rev-parse --symbolic-full-name HEAD
# Verify objects
git rev-parse --verify HEAD # Verify and return SHA-1
git rev-parse --verify HEAD^{commit} # Verify it's a commit
# Example output
$ git rev-parse HEAD
a1b2c3d4e5f6789012345678901234567890abcd
$ git rev-parse --short HEAD
a1b2c3d
$ git rev-parse --abbrev-ref HEAD
main
revert - Revert Commits
# Revert single commit
git revert HEAD # Revert last commit
git revert abc1234 # Revert specific commit
# Revert multiple commits
git revert HEAD~3..HEAD # Revert range
git revert --no-commit HEAD~3..HEAD # Don't auto-commit
# Revert merge commit
git revert -m 1 merge-commit # Revert to first parent
git revert -m 2 merge-commit # Revert to second parent
# Continue/abort revert
git revert --continue # After resolving conflicts
git revert --abort # Abort revert operation
# Revert with custom message
git revert -e HEAD # Edit commit message
git revert --no-edit HEAD # Use default message
Quick Reference Summary
Command | Purpose | Common Usage |
---|---|---|
init | Initialize repo | git init |
log | Show history | git log --oneline |
merge | Join histories | git merge feature |
pull | Fetch and merge | git pull origin main |
push | Upload changes | git push origin main |
rebase | Reapply commits | git rebase main |
reflog | Reference logs | git reflog |
remote | Manage remotes | git remote -v |
reset | Reset changes | git reset --hard HEAD~1 |
restore | Restore files | git restore file.txt |
revert | Undo commits | git revert HEAD |
This reference covers the essential Git commands from H-R. Each command includes practical examples and common use cases to help you master Git operations efficiently.