🧠 AI Computer Institute
Content is AI-generated for educational purposes. Verify critical information independently. A bharath.ai initiative.

Git Commands Cheat Sheet

programmingGrades 8-129 sections

Visual Overview: Git Workflow

Git Workflow: Local to Remote Working Directory (Your files) status, diff git add Staging Area (Index) diff --staged git commit Local Repository (.git folder) log, branch git push Remote Repository (GitHub, GitLab) origin/main git pull Working Directory Modified files: • Edit your code • New files added • Files deleted Commands: git status git diff git restore git clean git checkout Staging Area Prepared changes: • Ready to commit • Selected files • Atomic commits Commands: git add . git add file git reset git diff --staged git restore --staged Local Repo Version history: • Commits stored • Branches track • Full history Commands: git commit -m git log git branch git merge git tag Remote Repo Shared history: • Team visibility • Backup location • Pull requests Commands: git push git pull git fetch git remote git clone

The four-stage Git workflow: Edit files locally → Stage changes → Commit to local repo → Push to remote

Repository Setup

# Initialize repository
git init

# Clone repository
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git my-folder

# Configure user
git config user.name "Raj Kumar"
git config user.email "raj@example.com"
git config --global user.name "Raj Kumar"  # Global config

# View configuration
git config --list

Basic Workflow

# Check status
git status

# Stage files
git add file.txt
git add .              # Stage all changes

# Commit changes
git commit -m "Fix bug in login"
git commit -am "Quick fix"  # Stage & commit tracked files

# View commit history
git log
git log --oneline       # Compact view
git log --graph --all   # Visualize branches

# Show changes
git diff               # Unstaged changes
git diff --staged      # Staged changes
git diff HEAD          # All changes since last commit
git diff HEAD~1        # Compare with previous commit

# Undo changes
git restore file.txt          # Discard unstaged changes
git restore --staged file.txt # Unstage file
git reset HEAD~1              # Undo last commit
git reset --hard HEAD~1       # Discard last commit & changes

Branches

# List branches
git branch              # Local branches
git branch -a           # All branches

# Create branch
git branch feature-login
git switch -c feature-login  # Create & switch

# Switch branch
git switch main
git checkout main       # Alternative (older syntax)

# Delete branch
git branch -d feature-login
git branch -D feature-login  # Force delete

# Rename branch
git branch -m feature-login feature-auth

# View branch info
git branch -v           # Show last commit
git branch --merged     # Merged branches
git branch --no-merged  # Unmerged branches

Merge & Rebase

# Merge branch into current branch
git merge feature-login

# Create merge commit (even for fast-forward)
git merge --no-ff feature-login

# Resolve merge conflicts
# 1. Open conflicted files
# 2. Edit and remove conflict markers
# 3. git add resolved_file.txt
# 4. git commit

# Rebase (replay commits on new base)
git rebase main              # Rebase current branch onto main
git rebase -i HEAD~3         # Interactive rebase last 3 commits

# Abort rebase/merge
git rebase --abort
git merge --abort

# See what rebase will do
git rebase -i --dry-run main

Stash (Temporary Save)

# Save uncommitted changes
git stash
git stash save "WIP: feature in progress"

# List stashes
git stash list

# Apply stash
git stash apply              # Keep stash
git stash apply stash@{0}    # Apply specific stash
git stash pop                # Apply & remove (default latest)

# Delete stash
git stash drop stash@{0}
git stash clear              # Delete all

# Show stash changes
git stash show
git stash show -p            # Show detailed diff

Remote Repositories

# View remotes
git remote
git remote -v               # With URLs

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

# Fetch from remote (downloads, doesn't merge)
git fetch origin
git fetch --all

# Pull from remote (fetch + merge)
git pull origin main
git pull origin main --rebase  # Fetch + rebase

# Push to remote
git push origin main
git push origin feature-login  # Push branch

# Delete remote branch
git push origin --delete feature-login
git push origin :feature-login  # Alternative

# Set upstream branch
git push -u origin main        # Push & set tracking
git branch -u origin/main      # Just set tracking

# Force push (use carefully!)
git push origin main --force

Useful Inspection Commands

# Show commit details
git show HEAD
git show abc123              # Show specific commit

# Who changed this line? (blame)
git blame file.txt
git blame file.txt | grep pattern

# Find commits by keyword
git log --grep="login"
git log -S "searchTerm"       # Search by content

# Show branches touched a commit
git branch --contains abc123

# Get commit hash
git rev-parse HEAD           # Get current commit hash
git rev-parse --short HEAD   # Short hash

# Reflog (reference logs, useful for recovery)
git reflog                   # Show all actions
git reset --hard abc123      # Go back to any commit

Tags (Mark Releases)

# Create tag
git tag v1.0.0              # Lightweight tag
git tag -a v1.0.0 -m "Release 1.0"  # Annotated tag

# List tags
git tag
git tag -l "v1.*"

# Push tags
git push origin v1.0.0       # Push specific tag
git push origin --tags       # Push all tags

# Delete tag
git tag -d v1.0.0            # Local
git push origin --delete v1.0.0  # Remote

# Show tag details
git show v1.0.0

More Cheat Sheets