Git & GitHub Cheat Sheet

Complete reference guide for Git version control and GitHub collaboration

Quick Navigation

Setup & Configuration

Initial Setup

git config --global user.name "Your Name"
Set your name globally
git config --global user.email "your.email@example.com"
Set your email globally
git config --list
View all configuration settings
git config user.name
Check current username

SSH Key Setup

ssh-keygen -t rsa -b 4096 -C "your.email@example.com"
Generate SSH key
cat ~/.ssh/id_rsa.pub
Display public key
ssh -T git@github.com
Test SSH connection to GitHub

Repository Basics

Creating Repositories

git init
Initialize new repository
git clone <repository-url>
Clone existing repository
git clone <url> <directory>
Clone to specific directory

Repository Status

git status
Check repository status
git log
View commit history
git log --oneline
Condensed commit history
git log --graph --oneline --all
Visual branch history

Staging & Committing

Adding Files

git add <filename>
Add specific file
git add .
Add all changes
git add -A
Add all changes including deletions
git add -p
Interactive staging

Committing

git commit -m "commit message"
Commit with message
git commit -am "message"
Add and commit tracked files
git commit --amend
Amend last commit
git commit --amend -m "new message"
Amend with new message

Undoing Changes

git restore <filename>
Discard changes in file
git restore --staged <filename>
Unstage file
git reset HEAD~1
Undo last commit (keep changes)
git reset --hard HEAD~1
Undo last commit (discard changes)

Branching & Merging

Branch Operations

git branch
List local branches
git branch -a
List all branches
git branch <branch-name>
Create new branch
git checkout <branch-name>
Switch to branch
git checkout -b <branch-name>
Create and switch to branch
git branch -d <branch-name>
Delete branch
git branch -D <branch-name>
Force delete branch
git switch <branch-name>
Modern way to switch branches

Merging

git merge <branch-name>
Merge branch into current
git merge --no-ff <branch-name>
Merge with merge commit
git rebase <branch-name>
Rebase current branch
git rebase -i HEAD~3
Interactive rebase last 3 commits
Use merge for preserving history, rebase for clean linear history

Remote Repositories

Remote Management

git remote -v
List remotes
git remote add origin <url>
Add remote origin
git remote remove <name>
Remove remote
git remote rename <old> <new>
Rename remote

Pushing & Pulling

git push origin <branch>
Push branch to remote
git push -u origin <branch>
Push and set upstream
git pull
Pull changes from remote
git pull --rebase
Pull with rebase
git fetch
Fetch changes without merging
git push --force-with-lease
Safe force push
Never use git push --force on shared branches. Use --force-with-lease instead.

GitHub Operations

Repository Management

gh repo create <name>
Create GitHub repository
gh repo clone <repo>
Clone GitHub repository
gh repo fork <repo>
Fork repository
gh repo view
View repository details

Pull Requests

gh pr create
Create pull request
gh pr list
List pull requests
gh pr checkout <number>
Checkout PR locally
gh pr merge <number>
Merge pull request

Issues

gh issue create
Create new issue
gh issue list
List issues
gh issue close <number>
Close issue
gh issue view <number>
View issue details

Advanced Git

Stashing

git stash
Stash changes
git stash pop
Apply and remove stash
git stash list
List stashes
git stash apply stash@{0}
Apply specific stash

Cherry Picking

git cherry-pick <commit-hash>
Apply specific commit
git cherry-pick <hash1>..<hash2>
Cherry-pick range of commits

Tagging

git tag <tag-name>
Create lightweight tag
git tag -a <tag> -m "message"
Create annotated tag
git push origin <tag-name>
Push tag to remote
git push origin --tags
Push all tags

Searching

git grep "search term"
Search in repository
git log --grep="pattern"
Search commit messages
git log -S "code"
Search for code changes
git blame <filename>
See who changed what

Common Workflows

Feature Development Workflow

  1. git checkout main
  2. git pull origin main
  3. git checkout -b feature/new-feature
  4. Make changes and commit
  5. git push -u origin feature/new-feature
  6. Create pull request on GitHub
  7. Code review and merge
  8. git checkout main && git pull
  9. git branch -d feature/new-feature

Hotfix Workflow

  1. git checkout main
  2. git checkout -b hotfix/critical-fix
  3. Make fix and test
  4. git add . && git commit -m "Fix critical issue"
  5. git checkout main
  6. git merge hotfix/critical-fix
  7. git push origin main
  8. git tag -a v1.0.1 -m "Hotfix release"
  9. git push origin v1.0.1

Sync Fork Workflow

  1. git remote add upstream <original-repo-url>
  2. git fetch upstream
  3. git checkout main
  4. git merge upstream/main
  5. git push origin main

Release Workflow

  1. git checkout -b release/v1.0.0
  2. Update version numbers and changelog
  3. git add . && git commit -m "Prepare v1.0.0 release"
  4. git checkout main
  5. git merge release/v1.0.0
  6. git tag -a v1.0.0 -m "Release v1.0.0"
  7. git push origin main --tags
  8. Create GitHub release

Useful Git Aliases

git config --global alias.st status
Short status
git config --global alias.co checkout
Short checkout
git config --global alias.br branch
Short branch
git config --global alias.cm "commit -m"
Quick commit
git config --global alias.lg "log --oneline --graph"
Pretty log
git config --global alias.unstage "reset HEAD --"
Unstage files

Common .gitignore Patterns

# Dependencies
node_modules/
vendor/
Ignore package directories
# Environment files
.env
.env.local
Environment variables
# Build output
dist/
build/
*.min.js
Generated files
# OS files
.DS_Store
Thumbs.db
System files
# IDE files
.vscode/
.idea/
*.swp
Editor configurations
# Logs
*.log
logs/
Log files
Visit GitHub
Copied to clipboard!