Git Cheat Sheet
Complete reference guide for Git with interactive examples and live playground links
Click on any section to jump directly to it
Basic Commands
Repository Setup
Basic repository commands
Git
# Initialize repository
git init
# Clone repository
git clone <url>
# Remote management
git remote add origin <url>
git remote -v
git remote remove origin
Basic Workflow
Basic Git workflow
Git
# Check status
git status
# Add files
git add <file>
git add .
# Commit changes
git commit -m "message"
git commit -am "message" # add and commit
# Push changes
git push origin <branch>
Branching
Branch management
Git
# List branches
git branch
git branch -a
# Create branch
git branch <name>
git checkout -b <name>
# Switch branch
git checkout <branch>
git switch <branch>
# Delete branch
git branch -d <branch>
git branch -D <branch> # force delete
Advanced Commands
Merging
Merge operations
Git
# Merge branch
git merge <branch>
# Merge with no-ff
git merge --no-ff <branch>
# Abort merge
git merge --abort
# Resolve conflicts
git status # check conflicts
# edit files
git add <resolved-files>
git commit # complete merge
Rebasing
Rebase operations
Git
# Rebase branch
git rebase <branch>
# Interactive rebase
git rebase -i HEAD~3
# Abort rebase
git rebase --abort
# Continue rebase
git rebase --continue
# Skip commit
git rebase --skip
Stashing
Stash management
Git
# Save changes
git stash
git stash save "message"
# List stashes
git stash list
# Apply stash
git stash apply
git stash pop
# Drop stash
git stash drop
git stash clear
History Management
Viewing History
History viewing commands
Git
# Log
git log
git log --oneline
git log --graph
git log --author="name"
# Show changes
git show <commit>
git diff <commit1> <commit2>
# Blame
git blame <file>
Undoing Changes
Undoing changes
Git
# Reset
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
# Revert
git revert <commit>
# Amend
git commit --amend
# Clean
git clean -n # dry run
git clean -fd # force delete
Tags
Tag management
Git
# Create tag
git tag <name>
git tag -a <name> -m "message"
# List tags
git tag
git tag -l "v1.*"
# Delete tag
git tag -d <name>
git push origin :refs/tags/<name>
Collaboration
Remote Operations
Remote operations
Git
# Fetch
git fetch origin
git fetch --all
# Pull
git pull origin <branch>
git pull --rebase origin <branch>
# Push
git push origin <branch>
git push -f origin <branch> # force
# Push tags
git push origin --tags
Pull Requests
Pull request workflow
Git
# Create PR
1. Push branch
2. Create PR on GitHub/GitLab
3. Add reviewers
4. Add description
5. Submit PR
# Review PR
1. Check changes
2. Add comments
3. Approve/Request changes
4. Merge PR
Code Review
Code review process
Git
# Review changes
git diff <branch>
# Checkout PR
git fetch origin pull/<id>/head:<branch>
git checkout <branch>
# Review commits
git log <branch>
Best Practices
Commit Messages
Commit message guidelines
Git
# Format
<type>(<scope>): <subject>
<body>
<footer>
# Types
- feat: new feature
- fix: bug fix
- docs: documentation
- style: formatting
- refactor: code change
- test: testing
- chore: maintenance
Branch Strategy
Branching strategy
Git
# Main branches
main/master # production
develop # development
# Supporting branches
feature/* # new features
bugfix/* # bug fixes
release/* # release prep
hotfix/* # urgent fixes
# Workflow
1. Create feature branch
2. Develop feature
3. Create PR
4. Review & merge
5. Delete branch
Git Hooks
Git hooks usage
Git
# Pre-commit
- Lint code
- Run tests
- Check formatting
# Pre-push
- Run full test suite
- Check coverage
- Build project
# Post-merge
- Update dependencies
- Generate docs
- Notify team
Git - Interactive Developer Reference
Hover over code blocks to copy or run in live playground