Quick Answer:
git initcreates a repo.git add .stages all changes.git commit -m "message"commits.git pushpushes to remote.git pullpulls latest.git checkout -b featurecreates a branch.git merge featuremerges it back.
Setup
# Configure identity (do this first)
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
# Set default branch name
git config --global init.defaultBranch main
# Set default editor
git config --global core.editor "code --wait" # VS Code
git config --global core.editor "nano" # Nano
# View all settings
git config --list
# Enable color output
git config --global color.ui auto
Create and Clone
# Create new repo in current directory
git init
# Clone a remote repo
git clone https://github.com/user/repo.git
# Clone into specific folder
git clone https://github.com/user/repo.git myproject
# Clone specific branch
git clone -b develop https://github.com/user/repo.git
# Shallow clone (faster, less disk)
git clone --depth 1 https://github.com/user/repo.git
Daily Workflow
# Check status
git status
# Stage files
git add file.txt # Single file
git add src/ # Directory
git add . # Everything
git add -p # Interactive (pick hunks)
# Commit
git commit -m "Add user authentication"
# Add and commit tracked files in one step
git commit -am "Fix login bug"
# Push to remote
git push
git push origin main
# Pull latest (fetch + merge)
git pull
# Pull with rebase (cleaner history)
git pull --rebase
Branching
# List branches
git branch # Local
git branch -r # Remote
git branch -a # All
# Create and switch to new branch
git checkout -b feature/login
# Or (newer syntax):
git switch -c feature/login
# Switch to existing branch
git checkout main
git switch main
# Delete a branch
git branch -d feature/login # Safe (only if merged)
git branch -D feature/login # Force delete
# Delete remote branch
git push origin --delete feature/login
# Rename current branch
git branch -m new-name
Merging
# Merge a branch into current branch
git checkout main
git merge feature/login
# Merge with no fast-forward (always create merge commit)
git merge --no-ff feature/login
# Abort a merge (if conflicts)
git merge --abort
# Resolve conflicts then:
git add .
git commit
Rebasing
# Rebase current branch onto main
git checkout feature
git rebase main
# Interactive rebase (squash, reorder, edit last 5 commits)
git rebase -i HEAD~5
# Abort rebase
git rebase --abort
# Continue after resolving conflicts
git rebase --continue
Interactive Rebase Commands
pick = keep commit as-is
squash = combine with previous commit
fixup = combine with previous (discard message)
reword = keep commit but edit message
drop = delete commit
edit = pause to amend commit
Stashing
# Stash current changes
git stash
# Stash with message
git stash push -m "WIP: new feature"
# List stashes
git stash list
# Apply most recent stash (keep in stash list)
git stash apply
# Apply and remove from stash list
git stash pop
# Apply specific stash
git stash apply stash@{2}
# Drop a stash
git stash drop stash@{0}
# Clear all stashes
git stash clear
# Stash including untracked files
git stash -u
Viewing History
# Log
git log
git log --oneline # Compact
git log --oneline --graph # With branch graph
git log --oneline -10 # Last 10 commits
git log --author="Sam" # By author
git log --since="2 weeks ago" # By date
git log --grep="fix" # Search commit messages
git log -- src/auth.js # History of specific file
# Show a specific commit
git show abc1234
# Diff
git diff # Unstaged changes
git diff --staged # Staged changes
git diff main..feature # Between branches
git diff HEAD~3..HEAD # Last 3 commits
# Blame (who changed each line)
git blame file.txt
git blame -L 10,20 file.txt # Lines 10-20 only
# Search code across history
git log -S "functionName" --oneline
Undoing Things
# Unstage a file (keep changes)
git restore --staged file.txt
# Or older syntax:
git reset HEAD file.txt
# Discard changes in working directory
git restore file.txt
# Or older syntax:
git checkout -- file.txt
# Amend last commit (fix message or add files)
git commit --amend -m "New message"
git add forgotten-file.txt && git commit --amend --no-edit
# Undo last commit (keep changes staged)
git reset --soft HEAD~1
# Undo last commit (keep changes unstaged)
git reset HEAD~1
# Undo last commit (discard changes completely)
git reset --hard HEAD~1 # WARNING: permanently destroys uncommitted changes
# Revert a commit (creates new commit that undoes it — safe for shared branches)
git revert abc1234
# Recover deleted branch or lost commits
git reflog
git checkout -b recovered abc1234
Remote Repositories
# View remotes
git remote -v
# Add remote
git remote add origin https://github.com/user/repo.git
# Change remote URL
git remote set-url origin https://github.com/user/new-repo.git
# Fetch (download without merging)
git fetch origin
git fetch --all
# Push new branch to remote
git push -u origin feature/login
# Push all branches
git push --all origin
# Push tags
git push --tags
Tags
# Create tag
git tag v1.0.0
git tag -a v1.0.0 -m "Release 1.0.0" # Annotated
# Tag a specific commit
git tag v0.9.0 abc1234
# List tags
git tag
git tag -l "v1.*"
# Push tags to remote
git push origin v1.0.0
git push --tags
# Delete tag
git tag -d v1.0.0
git push origin --delete v1.0.0
.gitignore
# Common .gitignore patterns
node_modules/
.env
*.log
dist/
build/
.DS_Store
__pycache__/
*.pyc
.vscode/
.idea/
# Stop tracking a file that's already committed
git rm --cached file.txt
echo "file.txt" >> .gitignore
git commit -m "Remove file.txt from tracking"
Cherry-Pick
# Apply a specific commit to current branch
git cherry-pick abc1234
# Cherry-pick without committing (just stage)
git cherry-pick --no-commit abc1234
# Cherry-pick a range (start commit is EXCLUDED)
git cherry-pick abc1234..def5678
Common Workflows
Feature Branch Workflow
git checkout main
git pull
git checkout -b feature/new-thing
# ... make changes ...
git add . && git commit -m "Add new thing"
git push -u origin feature/new-thing
# Create pull request on GitHub/GitLab
Fix a Mistake on Main
# Option 1: Revert (safe, creates new commit)
git revert abc1234
# Option 2: Reset (if not pushed yet)
git reset --hard HEAD~1 # WARNING: permanently destroys uncommitted changes
Squash Commits Before Merging
git checkout feature
git rebase -i main
# Change 'pick' to 'squash' for all but first commit
# Save and edit the combined message
git push --force-with-lease