Cheat Sheet

Git Cheat Sheet

Essential commands for daily Git workflow — commit, branch, merge, rebase, and fix mistakes

Basic Workflow

git init                                  # Start a repo
git clone https://github.com/user/repo    # Clone remote
git add .                                 # Stage all changes
git add -p                                # Stage interactively (hunks)
git commit -m "message"                   # Commit staged changes
git push origin main                      # Push to remote
git pull                                  # Pull latest changes

Branching

git branch                                # List branches
git branch feature-name                   # Create branch
git checkout feature-name                 # Switch to branch
git checkout -b feature-name              # Create + switch
git switch -                              # Switch to previous branch
git merge feature-name                    # Merge into current branch
git branch -d feature-name                # Delete merged branch
git push origin --delete feature-name     # Delete remote branch

Fixing Mistakes

# Unstage files (keep changes)
git reset HEAD file.txt
# Discard local changes
git checkout -- file.txt
git restore file.txt                      # Newer syntax
# Amend last commit (don't do if pushed)
git commit --amend -m "new message"
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Undo last commit (discard changes)
git reset --hard HEAD~1
# Revert a pushed commit (safe)
git revert <commit-hash>

Viewing History

git log --oneline --graph                 # Compact graph
git log --oneline -5                      # Last 5 commits
git log --author="name"                   # Filter by author
git log --since="2 weeks ago"             # Filter by time
git show <commit-hash>                    # Show commit details
git blame file.txt                        # Who changed each line
git diff                                  # Unstaged changes
git diff --staged                         # Staged changes

Stashing

git stash                                 # Save uncommitted work
git stash list                            # List stashes
git stash pop                             # Restore and remove
git stash apply                           # Restore but keep stash
git stash drop stash@{2}                  # Remove specific stash
git stash save "name"                     # Named stash

Remotes

git remote -v                             # List remotes
git remote add origin <url>               # Add remote
git remote remove origin                  # Remove remote
git fetch origin                          # Fetch without merging
git pull origin main --rebase             # Pull with rebase

Rebasing

# Interactive rebase last 3 commits
git rebase -i HEAD~3
# Abort if things go wrong
git rebase --abort
# Continue after resolving conflicts
git rebase --continue

.gitignore Basics

node_modules/
.env
*.log
dist/
.DS_Store
.vscode/
__pycache__/
*.pyc

Tags

git tag v1.0.0                            # Create tag
git tag                                   # List tags
git push origin v1.0.0                    # Push tag
git push --tags                           # Push all tags
git tag -d v1.0.0                         # Delete local tag