🚀 Git Quick Reference Cheat Sheet

Quick Jump

âš™ī¸ Setup & Configuration

Initial Setup

git config --global user.name "Your Name" git config --global user.email "email@example.com"

Create Repository

git init git clone <url>

Check Configuration

git config --list git config user.name

📝 Basic Commands

Status & History

git status git log --oneline git log --graph --all

Stage & Commit

git add . git add <file> git commit -m "message"

Quick Commit

git commit -am "message" git commit --amend

đŸŒŋ Branching & Merging

Branch Management

git branch git branch <name> git checkout <branch> git checkout -b <new-branch>

Merging

git merge <branch> git merge --no-ff <branch> git merge --abort

Delete Branches

git branch -d <branch> git branch -D <branch> git push origin --delete <branch>

📊 Viewing Changes

Diff Commands

git diff git diff --staged git diff HEAD~2

Show & Blame

git show <commit> git blame <file> git log -p <file>

Search History

git log --grep="pattern" git log -S "code" git log --author="name"

â˜ī¸ Remote Repository

Remote Management

git remote -v git remote add origin <url> git remote set-url origin <url>

Push & Pull

git push origin main git push -u origin <branch> git pull origin main git pull --rebase

Fetch & Track

git fetch origin git fetch --all git branch -r git checkout --track origin/<branch>

â†Šī¸ Undo Changes

Unstage & Discard

git reset HEAD <file> git checkout -- <file> git clean -fd

Reset Commits

git reset --soft HEAD~1 git reset --mixed HEAD~1 git reset --hard HEAD~1

Revert & Restore

git revert <commit> git restore <file> git restore --staged <file>

đŸŽ¯ Advanced Commands

Stash

git stash git stash pop git stash list git stash apply stash@{2}

Rebase

git rebase main git rebase -i HEAD~3 git rebase --continue git rebase --abort

Cherry-pick & Tags

git cherry-pick <commit> git tag v1.0.0 git push --tags

🆘 Emergency Commands

Find Lost Commits

git reflog git fsck --lost-found git branch recover <commit>

Fix Mistakes

git reset --hard origin/main git push --force-with-lease git bisect start

Conflict Resolution

git checkout --ours <file> git checkout --theirs <file> git merge --abort

💡 Pro Tips Cheat Codes

One-Liners

git log --oneline --graph --all --decorate git branch --merged | grep -v "\*" | xargs -n 1 git branch -d git diff-tree --no-commit-id --name-only -r <commit>

Useful Aliases

alias gs='git status' alias gco='git checkout' alias gcm='git commit -m' alias gp='git push'

Time Savers

git add -p # Interactive staging git commit --fixup <commit> git stash push -m "WIP: feature"

📋 Common Workflows

Start New Feature

git checkout main
git pull origin main
git checkout -b feature/new-feature
# Make changes
git add .
git commit -m "Add new feature"
git push -u origin feature/new-feature
# Create PR on GitHub

Fix Emergency Bug

git stash  # Save current work
git checkout main
git pull origin main
git checkout -b hotfix/critical-bug
# Fix bug
git add .
git commit -m "Fix: critical bug"
git push origin hotfix/critical-bug
git checkout main
git merge hotfix/critical-bug
git push origin main