Git - Command Reference S-Z

Reference

Complete reference for Git commands S-Z with syntax, options, examples, and practical use cases. This comprehensive guide covers all essential Git commands alphabetically from 'shortlog' to 'worktree'.

shortlog - Summarize Git Log Output

# Summarize by author
git shortlog
git shortlog -n                 # Sort by number of commits
git shortlog -s                 # Summary only (no commit messages)
git shortlog -sn                # Summary sorted by count

# Filter by date/branch
git shortlog --since="1 month ago"
git shortlog v1.0..v2.0         # Between versions
git shortlog main..feature      # Commits in feature not in main

# Include all authors
git shortlog --all              # All branches
git shortlog --no-merges        # Exclude merge commits

# Email format
git shortlog -e                 # Show email addresses
# Example output
$ git shortlog -sn
    15  John Doe
     8  Jane Smith
     3  Bob Johnson

show - Show Object Information

# Show commit
git show HEAD                   # Latest commit
git show abc1234                # Specific commit
git show v1.0                   # Tagged commit

# Show specific files in commit
git show HEAD:file.txt
git show main:src/app.js

# Show tree or blob
git show HEAD^{tree}            # Show tree object
git show HEAD~1:directory/     # Show directory at previous commit

# Format options
git show --stat HEAD            # Show file statistics
git show --name-only HEAD       # Show changed files only
git show --pretty=format:"%h %s" HEAD

# Show multiple objects
git show HEAD HEAD~1 HEAD~2    # Multiple commits

show-branch - Show Branch Ancestry

# Show branch relationships
git show-branch
git show-branch main feature    # Specific branches
git show-branch --all           # All branches

# Show more commits
git show-branch --more=10       # Show 10 more commits

# Sparse output
git show-branch --sparse main feature

show-ref - List References

# Show all references
git show-ref
git show-ref --heads            # Branches only
git show-ref --tags             # Tags only

# Verify references
git show-ref --verify refs/heads/main
git show-ref --quiet main       # Quiet mode (exit code only)

# Exclude patterns
git show-ref --exclude="refs/remotes/*"

stash - Temporarily Save Changes

# Basic stash operations
git stash                       # Stash changes
git stash save "work in progress"  # Stash with message
git stash -u                    # Include untracked files
git stash -a                    # Include all files (even ignored)

# List and show stashes
git stash list                  # List all stashes
git stash show stash@{0}        # Show stash contents
git stash show -p stash@{0}     # Show patch

# Apply stashes
git stash apply                 # Apply latest stash (keep stash)
git stash apply stash@{2}       # Apply specific stash
git stash pop                   # Apply and remove latest stash
git stash pop stash@{1}         # Apply and remove specific stash

# Drop stashes
git stash drop stash@{0}        # Remove specific stash
git stash clear                 # Remove all stashes

# Partial stashing
git stash -p                    # Interactive stashing
git stash --keep-index          # Stash but keep staged changes

# Create branch from stash
git stash branch new-feature stash@{0}

status - Show Working Tree Status

# Basic status
git status
git status -s                   # Short format
git status --porcelain          # Machine-readable format

# Status options
git status -b                   # Show branch info
git status --show-stash         # Show stash info
git status --ahead-behind       # Show ahead/behind info

# Ignore submodules
git status --ignore-submodules
git status --ignore-submodules=dirty

# Untracked files
git status -u                   # Show untracked files
git status -uno                 # Don't show untracked files
# Short status format
$ git status -s
M  modified-file.txt
A  new-file.txt
D  deleted-file.txt
?? untracked-file.txt

submodule - Manage Submodules

# Add submodule
git submodule add https://github.com/user/repo.git lib/repo
git submodule add -b branch-name repo.git path

# Initialize submodules
git submodule init              # Initialize local config
git submodule update            # Update submodules
git submodule update --init     # Initialize and update
git submodule update --init --recursive  # Include nested submodules

# Update submodules
git submodule update --remote  # Update to latest remote commit
git submodule update --merge    # Merge changes
git submodule update --rebase   # Rebase changes

# Status and info
git submodule status            # Show submodule status
git submodule summary           # Show changes

# Execute commands in submodules
git submodule foreach 'git pull origin main'
git submodule foreach --recursive 'git clean -fd'

# Remove submodule
git submodule deinit path/to/submodule
git rm path/to/submodule

subtree - Manage Subtrees

# Add subtree
git subtree add --prefix=lib/project https://github.com/user/project.git main
git subtree add --prefix=vendor/lib repo.git main --squash

# Pull updates
git subtree pull --prefix=lib/project https://github.com/user/project.git main
git subtree pull --prefix=lib/project repo.git main --squash

# Push changes back
git subtree push --prefix=lib/project repo.git main

# Split subtree
git subtree split --prefix=lib/project -b subtree-branch

switch - Switch Branches (Git 2.23+)

# Switch to existing branch
git switch main
git switch feature-branch
git switch -                    # Previous branch

# Create and switch to new branch
git switch -c new-feature
git switch -c hotfix main       # Create from specific branch

# Switch with tracking
git switch -c feature -t origin/feature

# Detached HEAD
git switch --detach HEAD~1     # Switch to commit (detached)

# Force switch (discard changes)
git switch -f main              # Force switch

symbolic-ref - Read/Modify Symbolic References

# Read symbolic reference
git symbolic-ref HEAD           # Show what HEAD points to
git symbolic-ref --short HEAD   # Short branch name

# Set symbolic reference
git symbolic-ref HEAD refs/heads/main

# Delete symbolic reference
git symbolic-ref --delete HEAD

tag - Create/List/Delete Tags

# List tags
git tag                         # List all tags
git tag -l "v1.*"              # List with pattern
git tag --sort=version:refname  # Sort by version

# Create tags
git tag v1.0                    # Lightweight tag
git tag -a v1.0 -m "Version 1.0"  # Annotated tag
git tag -a v1.0 abc1234 -m "Tag specific commit"

# Show tag info
git show v1.0                   # Show tag and commit
git tag -v v1.0                 # Verify signed tag

# Delete tags
git tag -d v1.0                 # Delete local tag
git push origin --delete v1.0  # Delete remote tag

# Push tags
git push origin v1.0            # Push specific tag
git push origin --tags          # Push all tags
git push --follow-tags          # Push annotated tags with commits

# Sign tags
git tag -s v1.0 -m "Signed release"  # Sign with GPG

update-index - Register File Contents

# Update index (low-level)
git update-index --add file.txt
git update-index --remove file.txt

# Assume unchanged
git update-index --assume-unchanged file.txt
git update-index --no-assume-unchanged file.txt

# Skip worktree
git update-index --skip-worktree file.txt
git update-index --no-skip-worktree file.txt

# Refresh index
git update-index --refresh

update-ref - Update References

# Update reference (low-level)
git update-ref refs/heads/main abc1234
git update-ref HEAD abc1234

# Delete reference
git update-ref -d refs/heads/old-branch

# Create reference with old value check
git update-ref refs/heads/main new-sha old-sha

verify-commit - Verify Commit Signatures

# Verify signed commits
git verify-commit HEAD
git verify-commit abc1234

# Verify multiple commits
git verify-commit HEAD~3..HEAD

verify-pack - Validate Packed Objects

# Verify pack files
git verify-pack .git/objects/pack/pack-*.idx
git verify-pack -v pack-file.idx    # Verbose output
git verify-pack -s pack-file.idx    # Statistics only

verify-tag - Verify Tag Signatures

# Verify signed tags
git verify-tag v1.0
git verify-tag --verbose v1.0

# Verify multiple tags
git tag -l | xargs git verify-tag

version - Show Git Version

# Show Git version
git version
git --version

# Detailed version info
git version --build-options

whatchanged - Show Commit Logs with Differences

# Show changes (deprecated, use git log)
git whatchanged
git whatchanged --since="1 week ago"
git whatchanged -p              # Show patches

worktree - Manage Multiple Working Trees

# List worktrees
git worktree list
git worktree list --porcelain   # Machine-readable format

# Add worktree
git worktree add ../feature-work feature
git worktree add ../new-feature -b new-feature
git worktree add --detach ../temp-work HEAD~1

# Remove worktree
git worktree remove ../feature-work
git worktree remove --force ../feature-work

# Prune worktrees
git worktree prune              # Remove stale worktree entries
git worktree prune --dry-run    # Show what would be pruned

# Move worktree
git worktree move ../old-path ../new-path

# Lock/unlock worktree
git worktree lock ../feature-work
git worktree unlock ../feature-work

write-tree - Create Tree Object

# Create tree from index (low-level)
git write-tree

# Create tree with prefix
git write-tree --prefix=subdir/

Special Commands and Aliases

Common Git Aliases

# Useful aliases to set up
git config --global alias.s 'status -s'
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'
git config --global alias.timeline 'log --graph --branches --pretty=oneline --decorate'

Advanced Git Operations

# Find large files
git rev-list --objects --all | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | sed -n 's/^blob //p' | sort --numeric-sort --key=2 | tail -20

# Find commits that introduced/removed a string
git log -S "search_string" --source --all

# Show files changed between commits
git diff --name-only HEAD~5 HEAD

# Count commits by author
git shortlog -sn --all --no-merges

# Show branch merge history
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

Git Plumbing vs Porcelain Commands

Porcelain Commands (User-Facing)

# High-level commands for daily use
add, commit, push, pull, merge, rebase
branch, checkout, switch, restore
log, status, diff, show
clone, init, remote
stash, tag, reset, revert

Plumbing Commands (Low-Level)

# Low-level commands for scripting
cat-file, hash-object, write-tree, commit-tree
update-index, read-tree, checkout-index
update-ref, symbolic-ref, rev-parse
pack-objects, unpack-objects, verify-pack

Quick Reference Summary

CommandPurposeCommon Usage
shortlogSummarize commitsgit shortlog -sn
showShow objectsgit show HEAD
stashSave temporary changesgit stash save "message"
statusWorking tree statusgit status -s
submoduleManage submodulesgit submodule update --init
switchSwitch branchesgit switch -c new-branch
tagManage tagsgit tag -a v1.0 -m "Release"
worktreeMultiple work dirsgit worktree add ../feature

Exit Codes

# Common Git exit codes
0   - Success
1   - General error  
125 - Command not supported (bisect skip)
126 - Command not executable
127 - Command not found
128 - Invalid argument or repository not found
129 - Repository corrupt

Environment Variables

# Important Git environment variables
GIT_DIR              - Path to .git directory
GIT_WORK_TREE        - Path to working directory
GIT_AUTHOR_NAME      - Override author name
GIT_AUTHOR_EMAIL     - Override author email
GIT_COMMITTER_NAME   - Override committer name
GIT_COMMITTER_EMAIL  - Override committer email
GIT_EDITOR           - Default text editor
GIT_PAGER           - Default pager (less, more, etc.)
GIT_CONFIG          - Path to config file

This reference completes the comprehensive Git command guide from S-Z. Combined with the A-G and H-R references, it covers all essential Git commands with practical examples and real-world use cases.