Git Command Reference - Complete A-Z Command Guide

Git Command Reference

🟢 Beginner 🟡 Intermediate 🔴 Advanced 🟣 Expert

Complete A-Z reference of Git commands with syntax, common options, and practical examples. Your comprehensive Git command encyclopedia.

Basic Commands

git init

Syntax: git init [directory]
Initialize a new Git repository or reinitialize an existing one.

Examples:

# Initialize current directory
git init

# Initialize new directory
git init my-project

# Initialize bare repository
git init --bare shared-repo.git

git add

Syntax: git add [options] [pathspec]
Add file contents to the staging area (index).

Examples:

# Add specific file
git add filename.txt

# Add all files
git add .

# Add all tracked files
git add -u

# Interactive staging
git add -i

# Add by patches
git add -p

git commit

Syntax: git commit [options]
Record changes to the repository.

Examples:

# Commit with message
git commit -m "Add new feature"

# Commit all tracked changes
git commit -am "Fix bug in authentication"

# Amend last commit
git commit --amend

# Empty commit
git commit --allow-empty -m "Trigger CI"

git status

Syntax: git status [options]
Show the working tree status.

Examples:

# Full status
git status

# Short format
git status -s

# Show branch and tracking info
git status -b

# Ignore untracked files
git status -uno

git clone

Syntax: git clone [options] <repository> [directory]
Clone a repository into a new directory.

Examples:

# Basic clone
git clone https://github.com/user/repo.git

# Clone to specific directory
git clone https://github.com/user/repo.git my-project

# Shallow clone
git clone --depth 1 https://github.com/user/repo.git

# Clone specific branch
git clone -b develop https://github.com/user/repo.git

Branching & Merging

git branch

Syntax: git branch [options] [branch-name]
List, create, or delete branches.

Examples:

# List branches
git branch

# Create new branch
git branch feature-auth

# Delete branch
git branch -d feature-auth

# Force delete
git branch -D feature-auth

# List remote branches
git branch -r

# List all branches with details
git branch -vv

git switch

Syntax: git switch [options] <branch>
Switch branches (modern alternative to checkout for branches).

Examples:

# Switch to existing branch
git switch main

# Create and switch to new branch
git switch -c feature-auth

# Switch to remote branch
git switch -c local-branch origin/remote-branch

# Switch to previous branch
git switch -

git merge

Syntax: git merge [options] <commit>...
Join two or more development histories together.

Examples:

# Merge branch
git merge feature-auth

# No fast-forward merge
git merge --no-ff feature-auth

# Squash merge
git merge --squash feature-auth

# Abort merge
git merge --abort

git rebase

Syntax: git rebase [options] <upstream> [branch]
Reapply commits on top of another base tip.

Examples:

# Rebase current branch
git rebase main

# Interactive rebase
git rebase -i HEAD~3

# Rebase onto specific commit
git rebase --onto main feature~5 feature

# Continue after resolving conflicts
git rebase --continue

# Abort rebase
git rebase --abort

Remote Commands

git remote

Syntax: git remote [options]
Manage set of tracked repositories.

Examples:

# List remotes
git remote

# List with URLs
git remote -v

# Add remote
git remote add origin https://github.com/user/repo.git

# Remove remote
git remote remove origin

# Rename remote
git remote rename origin upstream

git push

Syntax: git push [options] [repository] [refspec]
Update remote refs along with associated objects.

Examples:

# Push to origin/main
git push origin main

# Push and set upstream
git push -u origin feature-auth

# Force push (dangerous)
git push --force origin main

# Safe force push
git push --force-with-lease origin main

# Push tags
git push --tags

git pull

Syntax: git pull [options] [repository] [refspec]
Fetch from and integrate with another repository or branch.

Examples:

# Pull from origin
git pull origin main

# Pull with rebase
git pull --rebase origin main

# Pull all branches
git pull --all

# Pull without merge commit
git pull --ff-only

git fetch

Syntax: git fetch [options] [repository] [refspec]
Download objects and refs from another repository.

Examples:

# Fetch from origin
git fetch origin

# Fetch all remotes
git fetch --all

# Fetch and prune
git fetch --prune

# Fetch specific branch
git fetch origin feature-auth

History & Inspection

git log

Syntax: git log [options] [revision-range] [paths]
Show commit logs.

Examples:

# Basic log
git log

# One line format
git log --oneline

# Graph view
git log --graph --oneline

# Last 5 commits
git log -5

# Filter by author
git log --author="John Doe"

# Filter by date
git log --since="2023-01-01"

git diff

Syntax: git diff [options] [commit] [commit] [path]
Show changes between commits, commit and working tree, etc.

Examples:

# Unstaged changes
git diff

# Staged changes
git diff --staged

# Between branches
git diff main..feature-auth

# Specific file
git diff HEAD~1 file.txt

# Word diff
git diff --word-diff

git show

Syntax: git show [options] <object>
Show various types of objects.

Examples:

# Show last commit
git show

# Show specific commit
git show abc1234

# Show file at commit
git show HEAD:file.txt

# Show tag
git show v1.0.0

git blame

Syntax: git blame [options] <file>
Show what revision and author last modified each line of a file.

Examples:

# Blame file
git blame file.txt

# Blame specific lines
git blame -L 10,20 file.txt

# Ignore whitespace
git blame -w file.txt

Undoing Changes

git reset

Syntax: git reset [options] [commit]
Reset current HEAD to the specified state.

Examples:

# Soft reset (keep changes staged)
git reset --soft HEAD~1

# Mixed reset (unstage changes)
git reset HEAD~1

# Hard reset (discard changes)
git reset --hard HEAD~1

# Reset specific file
git reset HEAD file.txt

git restore

Syntax: git restore [options] <pathspec>
Restore working tree files (modern alternative to checkout for files).

Examples:

# Restore file from HEAD
git restore file.txt

# Restore staged file
git restore --staged file.txt

# Restore from specific commit
git restore --source=HEAD~2 file.txt

git revert

Syntax: git revert [options] <commit>
Create new commit that undoes changes from previous commits.

Examples:

# Revert last commit
git revert HEAD

# Revert specific commit
git revert abc1234

# Revert without committing
git revert --no-commit HEAD~2..HEAD

git stash

Syntax: git stash [options]
Stash changes in a dirty working directory away.

Examples:

# Stash changes
git stash

# Stash with message
git stash push -m "Work in progress"

# List stashes
git stash list

# Apply stash
git stash apply

# Pop stash
git stash pop

# Drop stash
git stash drop

Advanced Commands

git cherry-pick

Syntax: git cherry-pick [options] <commit>
Apply changes introduced by existing commits.

Examples:

# Cherry-pick commit
git cherry-pick abc1234

# Cherry-pick range
git cherry-pick abc1234..def5678

# Cherry-pick without committing
git cherry-pick --no-commit abc1234

git bisect

Syntax: git bisect <subcommand> [options]
Use binary search to find the commit that introduced a bug.

Examples:

# Start bisect
git bisect start

# Mark bad commit
git bisect bad

# Mark good commit
git bisect good abc1234

# Automated bisect
git bisect run ./test-script.sh

# Reset bisect
git bisect reset

git worktree

Syntax: git worktree <subcommand> [options]
Manage multiple working trees.

Examples:

# Add worktree
git worktree add ../feature-branch feature

# List worktrees
git worktree list

# Remove worktree
git worktree remove ../feature-branch

# Prune worktrees
git worktree prune

git submodule

Syntax: git submodule <subcommand> [options]
Initialize, update or inspect submodules.

Examples:

# Add submodule
git submodule add https://github.com/user/lib.git lib

# Initialize submodules
git submodule init

# Update submodules
git submodule update

# Update to remote
git submodule update --remote

Configuration

git config

Syntax: git config [options] <name> [value]
Get and set repository or global options.

Examples:

# Set global user name
git config --global user.name "John Doe"

# Set email
git config --global user.email "[email protected]"

# List all config
git config --list

# Get specific value
git config user.name

# Create alias
git config --global alias.st status

Plumbing Commands

git reflog

Syntax: git reflog [options] [ref]
Show reference logs (history of ref updates).

Examples:

# Show reflog
git reflog

# Show reflog for branch
git reflog main

# Expire old entries
git reflog expire --expire=30.days

git fsck

Syntax: git fsck [options]
Verify connectivity and validity of objects in database.

Examples:

# Check repository
git fsck

# Full check
git fsck --full

# Find dangling objects
git fsck --dangling

🎯 Quick Reference Card

Essential Daily Commands

  • git status - Check status
  • git add . - Stage all changes
  • git commit -m - Commit with message
  • git push - Push to remote
  • git pull - Pull from remote

Branching Essentials

  • git branch - List branches
  • git switch -c - Create & switch branch
  • git merge - Merge branch
  • git branch -d - Delete branch

Inspection & History

  • git log --oneline - Compact log
  • git diff - Show changes
  • git show - Show commit
  • git blame - Show line authors

Undoing Changes

  • git restore - Restore files
  • git reset - Reset commits
  • git revert - Revert commit
  • git stash - Stash changes