Modern Git Cheat Sheet

Essential commands using modern Git syntax

Repository Setup

git init
Initialize a new repository
git clone <url>
Clone an existing repository

Basic Workflow

git status
Check current state
git add <file>
Stage specific file
git add .
Stage all changes
git commit -m "message"
Commit staged changes
git push
Push commits to remote
git pull
Fetch and merge remote changes

Branch Management

git branch
List branches
git branch <name>
Create new branch
git switch <branch>
Switch to branch
git switch -c <name>
Create and switch to new branch
git merge <branch>
Merge branch into current branch
git branch -d <name>
Delete branch

Time Navigation

git log --oneline --graph --all
View commit history with branch visualization

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>
Discard changes to specific file (reverts to last commit)
git restore --staged <file>
Unstage file (keep changes in working directory)

Undoing Changes

Reset Options Moves branch pointer

git reset --soft HEAD~1
Undo last commit, keep changes staged
Use when you want to modify the commit or split it into multiple commits
git reset --mixed HEAD~1
Undo last commit, keep changes unstaged
Default option. Use when you want to re-organize what goes into the commit
git reset --hard HEAD~1
Undo last commit, discard all changes
⚠️ Warning: Permanently deletes uncommitted work
Replace HEAD~1 with HEAD~2, HEAD~3, etc. to go back multiple commits

Remote Management

git remote -v
View remote repositories
git fetch
Download remote changes without merging

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/

Git Tags for Version Management Recommended Practice

What are tags? Tags are markers that point to specific commits, typically used to mark release versions. They act as bookmarks in your project's history (e.g., v1.0.0, v2.1.3).
git tag -a v1.0.0 -m "Release version 1.0.0"
Create annotated tag (recommended - stores metadata)
git tag
List all tags
git show v1.0.0
Show tag details
git switch --detach v1.0.0
View code at tagged version
git tag -d v1.0.0
Delete local tag
git push origin v1.0.0
Push specific tag to remote
git push origin --tags
Push all tags to remote
Semantic Versioning (SemVer): Use vMAJOR.MINOR.PATCH format
v1.0.0 - Initial release
v1.1.0 - Added new feature (minor update)
v1.1.1 - Fixed bug (patch)
v2.0.0 - Major rewrite with breaking changes

Complete Development Workflow Example

# === Initial Setup ===
git init
git add .
git commit -m "Initial commit"
git switch -c develop

# === Week 1: Adding new feature ===
git switch -c feature/gui-improvements
# ... make changes ...
git add .
git commit -m "Add dark theme to GUI"
git switch develop
git merge feature/gui-improvements
git branch -d feature/gui-improvements

# === Week 2: Bug fix ===
git switch -c bugfix/calculation-error
# ... fix bug ...
git add .
git commit -m "Fix division by zero error"
git switch develop
git merge bugfix/calculation-error
git branch -d bugfix/calculation-error

# === Week 3: Ready for release ===
git switch main
git merge develop
git tag -a v1.0.0 -m "Release v1.0.0 - Stable version with GUI and fixes"

# === Critical production hotfix ===
git switch -c hotfix/critical-bug
# ... fix bug ...
git add .
git commit -m "Fix critical security issue"
git switch main
git merge hotfix/critical-bug
git tag -a v1.0.1 -m "Hotfix v1.0.1 - Security patch"
git switch develop
git merge hotfix/critical-bug        # Apply fix to develop too
git branch -d hotfix/critical-bug
Copied to clipboard! ✓