Modern Git Cheat Sheet

Essential commands using modern Git syntax

Repository Setup

git init
Initialize a new repository
git branch -M main
Rename branch to 'main' (standard modern practice)
git clone <repo-url>
Clone an existing repository
git config --global push.autoSetupRemote true
Run once to avoid needing git push -u for new branches

Remote Management

git remote add origin <repo-url>
Link to your remote repository (Create empty repo first)
git push -u origin HEAD
Push the first version and set the link
Only needed once if push.autoSetupRemote is not true
git remote -v
View remote repositories
git fetch
Download remote changes without merging

Basic Workflow

git status
Check current state
git add <file-name>
Stage specific file
git add .
Stage all changes
git commit -m "message"
Commit staged changes
✓ Good: "Fix login timeout bug"
✓ Good: "Add dark mode to settings"
✗ Bad: "fixes", "update", "changes"
git push
Push commits to remote
Attempts to upload to the same branch on remote as is currently active on local.
git pull
Fetch and merge remote changes

Branch Management

git branch
List branches
git branch <branch-name>
Create new branch
git switch <branch-name>
Switch to branch
git switch -c <branch-name>
Create and switch to new branch
git merge <branch-name>
Merge branch into current branch
git branch -d <branch-name>
Delete branch (safe - possible only if already merged)
git branch -D <branch-name>
Force delete branch (even if unmerged - discards changes)
⚠️ Use with caution: permanently deletes unmerged work

Time Navigation

git log --oneline --graph --all
View commit history with branch visualization
git log --oneline -10
View last 10 commits. The number can be changed

Viewing Old Commits Standardized Workflow

git switch --detach <commit-hash>
View specific commit (detached HEAD state)
You can do this from ANY branch. Make experimental changes safely.
git switch -f <branch>
Return to latest commit of ANY branch and discard all changes
Branch flexibility: You don't need to return to the branch you were on before viewing the old commit. You can switch to ANY branch (main, develop, feature branch, etc.). This command goes to the newest commit of the chosen branch.

-f flag: Forces the switch and discards any changes made while viewing the old commit. This is the recommended approach when exploring old code.

Working Directory Changes

git restore <file-name>
Discard changes to specific file (reverts to last commit)
git restore --staged <file-name>
Unstage file (keep changes in working directory)

Undoing Changes

Reset Options Moves branch pointer

git reset --soft <commit-hash>
Undo commits, keep changes staged
Use when you want to modify recent commits or combine them
git reset --hard <commit-hash>
Undo commits, discard all changes
⚠️ Warning: Permanently deletes uncommitted work
Tip: Use <commit-hash> to specify exactly which state you want to jump back to. You can find hashes using git log --oneline.

Git Stash

git stash
Save changes to a temporary stack (cleans working directory)
git stash pop
Apply the latest stash and delete it from the stack
Recommended Folder Structure

Clean Application Structure with Development Subfolder

my-application/
├── .git/                         # Git repository metadata
├── app.py                        # Main application entry point
├── modules/                      # Core application modules (required for running)
│   ├── __init__.py
│   ├── core.py
│   └── utils.py
├── resources/                    # Application resources (icons, configs, data files)
│   ├── icons/
│   └── config.json
├── development/                  # All development-related files
│   ├── tests/                    # Unit and integration tests (tracked by Git)
│   │   ├── test_core.py
│   │   └── test_utils.py
│   ├── docs/                     # Technical documentation (tracked by Git)
│   │   ├── architecture.md
│   │   └── api_reference.md
│   ├── build_tools/              # Deployment/packaging scripts (tracked by Git)
│   │   ├── deploy.py
│   │   └── package.py
│   ├── requirements-dev.txt      # Development dependencies (tracked by Git)
│   └── local/                    # Personal files, experiments (NOT tracked)
│       ├── my_notes.txt
│       ├── test_data.csv
│       └── scratch.py
├── requirements.txt              # Production dependencies only
├── README.md                     # User-facing documentation
└── .gitignore                    # Git ignore rules
Key Principles:
Personal files: Always put in development/local/ - they will never be committed
Shared development files: Put in development/tests/, development/docs/ - these ARE committed
Production files: Keep at root level for clean structure
.gitignore Configuration
Create .gitignore in the root directory:
# Python cache and compiled files
__pycache__/
*.py[cod]
*$py.class
*.so

# Virtual environments
env/
venv/
.venv/
ENV/

# IDE and editor files
.vscode/
.idea/
*.swp
*.swo
*~

# Operating system files
.DS_Store
Thumbs.db

# Application temporary files
*.log
*.tmp
cache/
temp/

# Build artifacts
dist/
build/
*.egg-info/

# Personal development files (NOT tracked by Git)
development/local/

# Any other generated files specific to your application
output/
The Solo AI Developer Workflow (Remote Enabled)
# === Initial Setup ===
# Run this once to start the project and link it to the cloud.

git init
# Create .gitignore FIRST (see .gitignore section)
git add .
git commit -m "Initial commit"

# Rename branch to 'main' (standard modern practice)
git branch -M main 

# Link to your remote repository (Create empty repo on GitHub/GitLab first)
git remote add origin <repo-url>

# Push the first version and set the link
git push -u origin HEAD

# === Routine Feature / Bug Fix ===
# For 95% of your work (adding features, fixing bugs), stay on main.

# 1. Update local code (REQUIRED if working from multiple machines)
git pull 

# 2. Paste AI code or make changes
# ... (AI generates code, you verify it works) ...

# 3. Save and Sync
git add .
git commit -m "Add dark theme to settings page"
git push

# === The "Risky Experiment" (Branching) ===
# Use this when asking the AI to do something destructive (e.g., "Refactor the entire database structure").

# 1. Create a sandbox branch
git switch -c experiment/database-refactor

# 2. Make changes
# ... (AI rewrites files) ...

# 3. Save to the sandbox
git add .
git commit -m "Attempt database refactor"

# 4. (Optional) Backup this experiment to cloud
git push

# === DECISION POINT ===

# Option A: It worked! Merge it.
git switch main
git merge experiment/database-refactor

git push     # (Uploads current local branch to matching remote branch)
git branch -d experiment/database-refactor
# (Optional) If you pushed the branch earlier, delete it from remote:
git push origin --delete experiment/database-refactor 

# Option B: It failed. Destroy it.
git switch -f main # Force switch, discarding uncommitted changes
git branch -D experiment/database-refactor # Warning: Permanently deletes unmerged branch with its commits
# (Optional) If you pushed this branch to remote earlier, clean it up:
git push origin --delete experiment/database-refactor
Copied to clipboard! ✓