Git Cheat Sheet

Common Git workflows with step-by-step commands.

Start New Feature

Create feature branch from latest main

$ git checkout main
$ git pull origin main
$ git checkout -b feature/my-feature
# Make changes
$ git add .
$ git commit -m "feat: description"
$ git push -u origin feature/my-feature

Fix a Bug

Create bugfix branch

$ git checkout main
$ git pull
$ git checkout -b fix/bug-name
# Fix the bug
$ git add .
$ git commit -m "fix: description"
$ git push -u origin fix/bug-name

Merge Feature to Main

Merge completed feature

$ git checkout main
$ git pull origin main
$ git merge feature/my-feature
$ git push origin main
$ git branch -d feature/my-feature

Undo Last Commit

Keep changes but remove commit

$ git reset --soft HEAD~1
# Changes are now staged

Sync Fork with Upstream

Update forked repository

$ git remote add upstream https://github.com/original/repo.git
$ git fetch upstream
$ git checkout main
$ git merge upstream/main
$ git push origin main

Git Workflow Quick Reference

This cheat sheet provides common Git workflows with complete command sequences. Instead of remembering individual commands, follow these step-by-step guides for typical development tasks.

From starting new features to merging code and syncing forks, these workflows represent best practices used by development teams worldwide.

Feature Branch Workflow

Always create feature branches from an updated main branch. Name branches descriptively (feature/user-auth). Push branches for code review before merging. Delete branches after merging.

Commit Message Conventions

Use conventional commits: feat: for features, fix: for bugs, docs: for documentation. This enables automated changelog generation and semantic versioning.

Keeping Forks Updated

When contributing to open source, keep your fork synchronized with the upstream repository. Add upstream as a remote and merge regularly to avoid conflicts.