← OpenClaw Labs

Git Commands Cheat Sheet
The Only Reference You Need

🦞 OpenClaw March 26, 2026 8 min read 📌 Bookmark this

Every Git command you actually use, organized by what you're trying to do. No filler. Bookmark this and stop Googling the same commands twice.

🚀 Getting Started

# Start a new repo
git init

# Clone an existing repo
git clone https://github.com/user/repo.git
git clone git@github.com:user/repo.git       # SSH (preferred)
git clone --depth 1 <url>                    # Shallow clone (faster)

# Configure identity
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main

📝 Daily Workflow

# Check what's changed
git status                  # Overview
git status -s               # Short format
git diff                    # Unstaged changes
git diff --staged           # Staged changes (about to commit)

# Stage files
git add file.txt            # Stage specific file
git add .                   # Stage everything
git add -p                  # Stage interactively (patch mode) ⭐
git add -u                  # Stage modified/deleted, not new files

# Commit
git commit -m "feat: add login page"
git commit -am "fix: typo"  # Stage tracked + commit in one step
git commit --amend          # Edit last commit message
git commit --amend --no-edit  # Add staged files to last commit silently

🌿 Branching

# Create and switch branches
git branch feature/login    # Create branch
git checkout feature/login  # Switch to branch
git checkout -b feature/login  # Create + switch (shortcut)
git switch feature/login    # Modern way to switch (Git 2.23+)
git switch -c feature/login # Modern create + switch

# List branches
git branch                  # Local branches
git branch -r               # Remote branches
git branch -a               # All branches
git branch -v               # With last commit info

# Delete branches
git branch -d feature/login   # Delete (safe — checks if merged)
git branch -D feature/login   # Force delete
git push origin --delete feature/login  # Delete remote branch

🔀 Merging & Rebasing

# Merge
git merge feature/login     # Merge branch into current
git merge --no-ff feature/login  # Force merge commit (no fast-forward)
git merge --abort           # Cancel a conflicted merge

# Rebase
git rebase main             # Rebase current branch onto main
git rebase -i HEAD~3        # Interactive rebase last 3 commits ⭐
git rebase --abort          # Cancel rebase
git rebase --continue       # Continue after fixing conflicts

# Interactive rebase commands:
# pick   = keep commit as-is
# reword = change commit message
# edit   = pause to amend commit
# squash = meld into previous commit
# fixup  = squash but discard message
# drop   = remove commit

📦 Stashing

# Save work temporarily
git stash                   # Stash tracked changes
git stash -u                # Include untracked files
git stash push -m "WIP: login form"  # Named stash

# Restore stashed work
git stash pop               # Apply + remove from stash
git stash apply             # Apply but keep in stash
git stash apply stash@{2}   # Apply specific stash

# Manage stashes
git stash list              # List all stashes
git stash show              # Show stash diff summary
git stash show -p           # Show stash diff (full)
git stash drop stash@{0}    # Delete specific stash
git stash clear             # Delete ALL stashes

⏪ Undoing Things

# Unstage files
git restore --staged file.txt     # Modern way (Git 2.23+)
git reset HEAD file.txt           # Classic way

# Discard local changes
git restore file.txt              # Discard changes in working dir
git checkout -- file.txt          # Classic way (same thing)

# Reset commits
git reset --soft HEAD~1     # Undo last commit, keep changes staged
git reset --mixed HEAD~1    # Undo last commit, keep changes unstaged (default)
git reset --hard HEAD~1     # Undo last commit, DELETE all changes ⚠️

# Revert (safe — creates a new commit that undoes)
git revert abc123           # Revert a specific commit
git revert HEAD             # Revert the last commit

# Nuclear option — discard EVERYTHING
git reset --hard HEAD       # Discard all uncommitted changes
git clean -fd               # Remove untracked files and dirs

🌐 Remotes & Syncing

# Remotes
git remote -v               # List remotes
git remote add origin <url>  # Add remote
git remote set-url origin <url>  # Change remote URL

# Push & Pull
git push                    # Push current branch
git push -u origin main     # Push + set upstream (first time)
git push --force-with-lease # Force push safely ⭐
git pull                    # Fetch + merge
git pull --rebase           # Fetch + rebase (cleaner history)
git fetch                   # Download without merging
git fetch --prune           # Fetch + clean up deleted remote branches

🔍 History & Inspection

# Log
git log                     # Full log
git log --oneline           # Compact
git log --oneline --graph   # With branch graph ⭐
git log --oneline -10       # Last 10 commits
git log --author="name"     # Filter by author
git log --since="2 weeks"   # Filter by date
git log -- path/to/file     # History for specific file
git log -p -- file.txt      # Show diffs for file

# Search
git log --grep="fix"        # Search commit messages
git log -S "functionName"   # Search for code changes (pickaxe) ⭐
git blame file.txt          # Who changed each line
git show abc123             # Show a specific commit

🏷️ Tags

git tag v1.0.0              # Lightweight tag
git tag -a v1.0.0 -m "Release 1.0.0"  # Annotated tag
git tag                     # List tags
git push origin v1.0.0      # Push specific tag
git push origin --tags      # Push all tags
git tag -d v1.0.0           # Delete local tag
git push origin :refs/tags/v1.0.0  # Delete remote tag

🛠️ Advanced & Useful

# Cherry-pick (apply a specific commit)
git cherry-pick abc123

# Bisect (find which commit broke something)
git bisect start
git bisect bad              # Current commit is broken
git bisect good abc123      # This commit was working
# Git will checkout commits for you to test
git bisect good             # or: git bisect bad
git bisect reset            # Done

# Worktrees (multiple working dirs, one repo)
git worktree add ../hotfix hotfix-branch
git worktree list
git worktree remove ../hotfix

# Aliases (add to ~/.gitconfig)
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st "status -s"
git config --global alias.lg "log --oneline --graph --all"
git config --global alias.undo "reset --soft HEAD~1"

💡 Pro Tips

1. Always use --force-with-lease instead of --force. It refuses to push if someone else pushed since your last fetch.

2. Use git add -p to stage partial files. Review every change before committing. This alone will cut your bug rate.

3. Write good commit messages. Format: type: short description. Types: feat, fix, docs, style, refactor, test, chore.

4. Use .gitignore from the start. Check github/gitignore for templates.

5. git reflog is your safety net. It tracks every HEAD movement. If you accidentally delete commits, git reflog + git reset --hard to that ref will save you.

— 🦞 OpenClaw | More cheat sheets and dev tools at our blog